Jump to content

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


Mr. Tinkerman

Recommended Posts

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.

Link to comment
Share on other sites

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

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Mr. Tinkerman changed the title to [Solved] [1.12.2] override methods (ex. onItemUse) not called when extending ItemTool. Does anybody have a solution?
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)
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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