Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

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


Mr. Tinkerman
 Share

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
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.

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Been there done that 😂 The words Forge & Fabric just look too similar if you're dyslexic, I guess 😅
    • My TileEntity implements IInventory (I know it's outdated, but I don't think IItemHandler would solve my problem) and I want to change things in the inventory if setItem is called. However, the container only shows the change after one action delay or if I reopen the container. Code (not finished): @Override public void setItem(int index, ItemStack stack) { this.items.set(index, stack); if (stack.getCount() > this.getMaxStackSize()) { stack.setCount(this.getMaxStackSize()); } //This is were the changes are not recognised instantaneously //Important is that I change the list that stores the items if(index == 9 || index == 10) { ItemStack input = this.items.get(9); int in = input.getCount(); if(in > 0) { ItemStack output = this.items.get(10); int out = output.getCount(); Item item; if(input.getItem().equals(Items.BUCKET)) item = ItemInit.CREOSOTE_BUCKET.get(); else item = ItemInit.CREOSOTE_GLASS.get(); if(output.getItem().equals(item) || output.isEmpty()) { ItemStack filledItem = new ItemStack(item); int amount = Math.min(filledItem.getMaxStackSize() - out, in); input.shrink(amount); filledItem.setCount(out + amount); items.set(10, filledItem); } } } this.setChanged(); } I also did this: //-----------------------------------------------------Synchronization----------------------------------------------------- @Override public SUpdateTileEntityPacket getUpdatePacket() { return new SUpdateTileEntityPacket(this.getBlockPos(), 1, getUpdateTag()); } @Override public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) { this.load(this.level.getBlockState(this.getBlockPos()), pkt.getTag()); } @Override public CompoundNBT getUpdateTag() { return this.save(new CompoundNBT()); } @Override public void handleUpdateTag(BlockState state, CompoundNBT tag) { this.load(state, tag); } What can I do to solve this problem? I wanted to use packets to sync server and client, but I don't have a player in this context.
    • So I'm trying to make an ore generate in the end, and the veins are generating in huge sizes which are not desired. The first screenshot refers to a test where I set the max vein size to 3, and the second screenshot when I set it to 1. Screenshots   public class ModOreGeneration { public static void generateOres(final BiomeLoadingEvent event){ for(OreType ore: OreType.values()){ OreFeatureConfig oreFeatureConfig = new OreFeatureConfig( new BlockMatchRuleTest(Blocks.END_STONE), ore.getBlock().get().defaultBlockState(), ore.getMaxHeight() ); ConfiguredPlacement<TopSolidRangeConfig> configuredPlacement = Placement.RANGE.configured( new TopSolidRangeConfig(ore.getMinHeight(),ore.getMinHeight(), ore.getMaxHeight()) ); ConfiguredFeature<?,?> oreFeature = registerOreFeature(ore, oreFeatureConfig,configuredPlacement); event.getGeneration().addFeature(GenerationStage.Decoration.UNDERGROUND_ORES, oreFeature); } } private static ConfiguredFeature<?,?> registerOreFeature(OreType ore, OreFeatureConfig oreFeatureConfig, ConfiguredPlacement configuredPlacement){ return Registry.register(WorldGenRegistries.CONFIGURED_FEATURE, ore.getBlock().get().getRegistryName(), Feature.ORE.configured(oreFeatureConfig).decorated(configuredPlacement).squared().count(ore.getMaxVeinSize())); } } public enum OreType { RUMBANITE(Lazy.of(ModBlocks.RUMBANITE_ORE),1,15,40); private final Lazy<Block> block; private final int maxVeinSize; private final int minHeight; private final int maxHeight; OreType(Lazy<Block> block, int maxVeinSize, int minHeight, int maxHeight) { this.block = block; this.maxVeinSize = maxVeinSize; this.minHeight = minHeight; this.maxHeight = maxHeight; } public Lazy<Block> getBlock() { return block; } public int getMaxVeinSize() { return maxVeinSize; } public int getMinHeight() { return minHeight; } public int getMaxHeight() { return maxHeight; } public static OreType get(Block block){ for(OreType ore : values()){ if(block == ore.block){ return ore; } } return null; } }  
    • What diesieben07 says, except the error's caused by Xaero's World Map, not the mini map   You're running XaerosWorldMap_1.19.0_Forge_1.18.jar which was released on Dec 28, 2021. They've since updated this mod a lot since they're currently at XaerosWorldMap_1.22.0_Forge_1.18.2.jar If I counted correctly, you're 11 updates behind on their releases, so it may be wise to either start a regular updating schedule or figure out a way to setup automated updates 😅 When you're waiting with updates until issues/errors arise, you're technically putting the server's data integrity at risk. Granted, this won't often result in permanent damages, but it could 😛  Also, there could be vulnerability patches that you're missing out on, which by the time they're patched become public-record, thus also known amongst any hacker that would want to exploit it. tl;dr: Updating the Xaero's mod will fix the issue, but given the severity of the outdated-ness, making changes as to how/when you're updating the software would be strongly advisable.
    • I wanted to make a block that melts like ice. I use the tick method in the BlockEntity to periodically check the light level. However  level.getBrightness(LightLayer.BLOCK,pos); always returns 0.
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.