Jump to content

[1.11.2] onBlockActivated() Not Working


admiralmattbar

Recommended Posts

Hi all,

 

I'm having trouble with my mod getting the onBlockActivated() method to do anything. With the code I have right clicking on the block does nothing. I would like to make a bush that gives you brian_jerky when you right click on it and turns into an empty bush. I figured using onBlockActivated would be the best method to do this. I added the chat as a means of testing whether or not the method activates at all. So far nothing happens when I right click on the bush.

 

package org.educraft.brianface.blockclasses;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.text.TextComponentString;
import org.educraft.brianface.init.ModBlocks;
//import org.educraft.brianface.init.ModBlocks;
import org.educraft.brianface.init.ModItems;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import static sun.audio.AudioPlayer.player;

public class BlockBrianBushFull extends BlockBrianBushEmpty {
	public BlockBrianBushFull(){
		super();
		this.setTickRandomly(false); //Stops random growth
	}
	
	public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, ItemStack heldItem,
									EnumFacing side, float hitX, float hitY, float hitZ){
		
		//This makes it work on server side.
		if(!worldIn.isRemote) {

			return true;
		}
		else
		{
			EntityItem brian_jerky = new EntityItem(worldIn, playerIn.posX, playerIn.posY, playerIn.posZ,
					new ItemStack(ModItems.brian_jerky));
			brian_jerky.setNoPickupDelay();
			worldIn.spawnEntity(brian_jerky);
			playerIn.sendMessage(new TextComponentString("picking"));

			//Now the bush has no more jerky
			worldIn.setBlockState(pos, ModBlocks.brian_bush_empty.getDefaultState(), 2);
			
			return true;
		}
		
	}
}

 

Sorry if this is obvious, but I'm at a loss as to what I'm doing wrong.

Link to comment
Share on other sites

Your onBlockActivated method doesn't override a super method, so it will never be called unless you do so yourself. If you'd annotated the method with @Override, you would have gotten a compilation error telling you this.

 

Use your IDE to auto-generate override methods with the correct signature and annotation.

  • Like 2

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Thanks for the reply Choonster. The resources you've made available have been super helpful to me when I got into modding. I added the @Override method that IntelliJ recommended using ctrl-O and selecting the method from Block.java, tried the same thing in Eclipse with ctrl-space. I still have the same problem. Is this a problem with my IDE's? 

Edited by admiralmattbar
Link to comment
Share on other sites

10 minutes ago, admiralmattbar said:

Thanks for the reply Choonster. The resources you've made available have been super helpful to me when I got into modding.

I'm glad they've helped.

 

10 minutes ago, admiralmattbar said:

I added the @Override method that IntelliJ recommended using ctrl-O and selecting the method from Block.java, tried the same thing in Eclipse with ctrl-space. I still have the same problem. Is this a problem with my IDE's? 

Post your new code.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

i trust you'll fix the method signature so let me note some other details:

 

isRemote means client. just return true on client (invert the condition). create and spawn your item on the server side; call setBlockState on the server side.

 

you're clearly someone who knows this programming trade and appreciates elegance. but inheritance isn't always the way to go. you used two block ids for something that could be easily done with one block and a few states. be economical with those ids, at least for a while.

Link to comment
Share on other sites

Hello,

 

Thanks for the help. I completely took out the isRemote check and it started working. Here's my new code. I'm still working on getting the entity to disappear when I collect the item.

 

package org.educraft.brianface.blockclasses;

import net.minecraft.util.EnumHand;
import net.minecraft.util.text.TextComponentString;
import org.educraft.brianface.init.ModBlocks;
import org.educraft.brianface.init.ModItems;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class BlockBrianBushFull extends BlockBrianBushEmpty {
	public BlockBrianBushFull(){
		super();
		this.setTickRandomly(false); //Stops random growth
	}

	@Override
	public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
		EntityItem brian_jerky = new EntityItem(worldIn, playerIn.posX, playerIn.posY, playerIn.posZ,
				new ItemStack(ModItems.brian_jerky));
		brian_jerky.setNoPickupDelay();
		worldIn.spawnEntity(brian_jerky);
		playerIn.sendMessage(new TextComponentString("picking"));

		//Now the bush has no more jerky
		worldIn.setBlockState(pos, ModBlocks.brian_bush_empty.getDefaultState(), 2);

		return true;
	}

}

 

Link to comment
Share on other sites

Now you are spawning the item on both side. You shouldn't do that, only spawn it on server thread, check if World::isRemote is false for that.

(So basically invert World::isRemote check)

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

Hello,

 

Thanks for all the help. Here's the code that works properly without spawning on both sides.

 

package org.educraft.brianface.blockclasses;

import net.minecraft.util.EnumHand;
import org.educraft.brianface.init.ModBlocks;
import org.educraft.brianface.init.ModItems;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class BlockBrianBushFull extends BlockBrianBushEmpty {
	public BlockBrianBushFull(){
		super();
		this.setTickRandomly(false); //Stops random growth
	}

	@Override
	public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
        //This makes it work on server side.
        if(worldIn.isRemote) {

            return true;
        }
        else {
            EntityItem brapple = new EntityItem(worldIn, playerIn.posX, playerIn.posY, playerIn.posZ,
                    new ItemStack(ModItems.brapple));
            brapple.setNoPickupDelay();
            worldIn.spawnEntity(brapple);

            //Now the bush has no more jerky
            worldIn.setBlockState(pos, ModBlocks.brian_bush_empty.getDefaultState(), 2);

            return true;
        }
	}

}

 

And @MFMods, I'll start working on doing this with one blockID.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.