Jump to content

[Solved] [1.12.2] override methods (ex. onItemUse) not called when extending ItemTool. Does anybody have a solution?


Recommended Posts

Posted

I have been trying to create a chisel tool that can be used to break chunks off of bedrock.  The problem is that whenever I try to override the onItemUse or onItemRightClick the methods do not get called.  I have confirmed this by replacing my code with a simple message to tell me when the method has been called but receive no messages.  Is anyone willing to help me fix the issue.  I'll put the code below.  Thanks in advanced.

 

This is the base class I made for the chisels.

package com.mrtinkerman.modularmachines.items;

import java.util.Set;

import com.mrtinkerman.modularmachines.ModularMachines;
import com.mrtinkerman.modularmachines.creativetabs.CustomTabs;
import com.mrtinkerman.modularmachines.init.ModItems;
import com.mrtinkerman.modularmachines.util.IHasModel;

import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemTool;

public class ChiselBase extends ItemTool implements IHasModel
{
	public ChiselBase(String name, ToolMaterial materialIn, Set<Block> effectiveBlocks)
	{	
		super(materialIn, effectiveBlocks);
		
		setUnlocalizedName(name);
		setRegistryName(name);
		
		setMaxStackSize(1);
		
		ModItems.ITEMS.add(this);
	}

	@Override
	public void registerModels() 
	{
		ModularMachines.proxy.registerItemRenderer(this, 0, "inventory");
	}
}

 

This is the actual chisel class

package com.mrtinkerman.modularmachines.items.tools;

import java.util.Set;

import com.mrtinkerman.modularmachines.items.ChiselBase;

import net.minecraft.block.Block;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.minecraftforge.fml.client.FMLClientHandler;

public class HandChiselIron extends ChiselBase
{
	public HandChiselIron(String name, ToolMaterial materialIn, Set<Block> effectiveBlocks)
	{
		super(name, materialIn, effectiveBlocks);
	}

	@Override
	public EnumActionResult onItemUse(EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
	{
		playerIn.sendMessage(new TextComponentString("Block Right Clicked"));
		System.out.println("OnItemUse Called");
//		if(true)
//		{
//			//System.out.println("OnItemUse Called inside if loop");
//			playerIn.sendMessage(new TextComponentString("Block Right Clicked IF"));
//		}
		
		return super.onItemUse(playerIn, worldIn, pos, hand, facing, hitX, hitY, hitZ);
	}
	
	@Override
	public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
	{
		playerIn.sendMessage(new TextComponentString("Right Clicked!"));
		// TODO Auto-generated method stub
		return super.onItemRightClick(worldIn, playerIn, handIn);
	}
	
	@Override
	public double getDurabilityForDisplay(ItemStack stack)
	{
		return super.getDurabilityForDisplay(stack);
	}
	
	@Override
	public boolean hasContainerItem()
	{
		return true;
	}
	
	@Override
	public Item getContainerItem()
	{
		return super.getContainerItem();
	}
}

 

This is the ModItems class

package com.mrtinkerman.modularmachines.init;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.Item.ToolMaterial;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.mrtinkerman.modularmachines.items.ChiselBase;
import com.mrtinkerman.modularmachines.items.ItemBase;
import com.mrtinkerman.modularmachines.items.tools.TinkerWrench;
import com.google.common.collect.Sets;
import com.mrtinkerman.modularmachines.init.ModMaterials;

public class ModItems
{

	public static final List<Item> ITEMS = new ArrayList<Item>();

	//Block Sets
	public static final Set<Block> CHISEL_EFFECTIVE = Sets.newHashSet(new Block[]{ Blocks.BEDROCK });
	
	//Tools
	public static final Item HAND_CHISEL_IRON = new ChiselBase("hand_chisel_iron", ModMaterials.IRON_CHISEL, CHISEL_EFFECTIVE);
      
}

 

Registry class


package com.mrtinkerman.modularmachines.util.handlers;

import com.mrtinkerman.modularmachines.blocks.BlockTileEntityBase;
import com.mrtinkerman.modularmachines.blocks.functional.Machine_Frame_Block;
import com.mrtinkerman.modularmachines.entity.tile.Machine_Frame_Data;
import com.mrtinkerman.modularmachines.init.ModBlocks;
import com.mrtinkerman.modularmachines.init.ModItems;
import com.mrtinkerman.modularmachines.init.ModTileEntities;
import com.mrtinkerman.modularmachines.util.IHasModel;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.item.Item;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;

@EventBusSubscriber
public class RegistryHandler
{
	@SubscribeEvent
	public static void onItemRegister(RegistryEvent.Register<Item> event)
	{
		event.getRegistry().registerAll(ModItems.ITEMS.toArray(new Item[0]));
	}

	@SubscribeEvent
	public static void onBlockRegister(RegistryEvent.Register<Block> event)
	{
		event.getRegistry().registerAll(ModBlocks.BLOCKS.toArray(new Block[0]));
		event.getRegistry().registerAll(ModTileEntities.TILE_ENTITIES.toArray(new Block[0]));

		for (BlockTileEntityBase blockTileEntity : ModTileEntities.TILE_ENTITIES)
		{
			GameRegistry.registerTileEntity(blockTileEntity.getTileEntityClass(), blockTileEntity.getRegistryName());
		}
	}

	@SubscribeEvent
	public static void onModelRegister(ModelRegistryEvent event)
	{
		for (Item item : ModItems.ITEMS)
		{
			if (item instanceof IHasModel)
			{
				((IHasModel) item).registerModels();
			}
		}

		for (Block block : ModBlocks.BLOCKS)
		{
			if (block instanceof IHasModel)
			{
				((IHasModel) block).registerModels();
			}
		}
		
		for (BlockTileEntityBase tileEntity : ModTileEntities.TILE_ENTITIES)
		{
			if (tileEntity instanceof IHasModel)
			{
				((IHasModel) tileEntity).registerModels();
			}
		}
	}
}

 

I think that is all you'll need but please let me know otherwise.  Thanks again.

  • Mr. Tinkerman changed the title to [1.12.2] override methods (ex. onItemUse) not called when extending ItemTool. Does anybody have a solution?
Posted (edited)

I'm feeling pretty dumb right now...

 

I needed to create a new item from HandChiselIron instead of ChiselBase

	//From
	public static final Item HAND_CHISEL_IRON = new ChiselBase("hand_chisel_iron", ModMaterials.IRON_CHISEL, CHISEL_EFFECTIVE);

	//To
	public static final Item HAND_CHISEL_IRON = new HandChiselIron("hand_chisel_iron", ModMaterials.IRON_CHISEL, CHISEL_EFFECTIVE);

 

Sorry 'bout that.

 

EDIT: I'll leave the thread open for a little while in case anyone has any comments.

Edited by Mr. Tinkerman
Posted
1 hour ago, Mr. Tinkerman said:

public static final Item HAND_CHISEL_IRON = new ChiselBase

Problematic code #14

 

2 hours ago, Mr. Tinkerman said:

IHasModel

Code style #3

 

2 hours ago, Mr. Tinkerman said:

@Override
	public Item getContainerItem()
	{
		return super.getContainerItem();
	}

Pointless code. Just don't override it if you're going to do nothing.

 

2 hours ago, Mr. Tinkerman said:

public class ChiselBase

Code Style #4

  • Thanks 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted (edited)
14 hours ago, Draco18s said:

Problematic code #14

 

Code style #3

 

Pointless code. Just don't override it if you're going to do nothing.

 

Code Style #4

Thank you for the advice and showing the problematic code!  I'll update my code based on your comment.  I'm just learning so I wasn't aware about Code Styles 3 and 4 although I guess I should have known that getContainerItem() wasn't needed.

Edited by Mr. Tinkerman
nuthin' much
  • Mr. Tinkerman changed the title to [Solved] [1.12.2] override methods (ex. onItemUse) not called when extending ItemTool. Does anybody have a solution?
Posted (edited)
14 hours ago, Draco18s said:

Problematic code #14

 

Code style #3

 

Pointless code. Just don't override it if you're going to do nothing.

 

Code Style #4

I understand Code style #4 but I have a question about Code style #3.  Does this mean I should completely remove IHasModel and I register the model for all items in the registry?

 

I clicked the link but I'm not sure if I'm fully understanding what Code style #3 is suggesting I actually do.  Do I leave it in ItemBase or remove it entirely?  Sorry about all these questions.

Edited by Mr. Tinkerman
forgot a thing (again)
Posted (edited)
1 hour ago, Mr. Tinkerman said:

Does this mean I should completely remove IHasModel

Yes

 

Quote

and I register the model for all items in the registry?

No. You should only register models for all of YOUR items. You're already looping over all of your items, so don't change that.

 

Note that you do not need to loop over your block list or your tile entity list, they don't need models registered, only items do, and if your blocks have items, those items should already be in the list.

Edited by Draco18s
  • Thanks 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
2 hours ago, Draco18s said:

Yes

 

No. You should only register models for all of YOUR items. You're already looping over all of your items, so don't change that.

 

Note that you do not need to loop over your block list or your tile entity list, they don't need models registered, only items do, and if your blocks have items, those items should already be in the list.

OK, I think I understand now.  I meant to only register my items instead of all of them in general but I didn't know that I should remove the registry loops for the blocks and tile entities.  Thanks again for helping me.

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.