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

Override method hashcode in ItemStack


f1rSt1kChannel
 Share

Recommended Posts

This is sadly not possible in an intuitive way. What do you want to be included in the hashCode? The ItemStack's NBT data? Only it's Item? It's Item and damage value? What about the stack size?

Everyone has a different use case and therefor it's not practical to override it.

The only meaningful way would be to check everything (including stack size). But then recipe lookups (which you are most likely referring to here) are no easier because an ItemStack with a stackSize of 5 is not equal to an ItemStack with a stackSize of 1, even though the recipe should still match. Same for NBT data and other things.

Link to comment
Share on other sites

I usually sort null -> item id -> metadata -> quantity -> having nbt -> nbt equals

 

You can sort by anything else afterwards.

 

It just takes a bit of thinking, it isn't too bad.

BEFORE ASKING FOR HELP READ THE EAQ!

 

I'll help if I can. Apologies if I do something obviously stupid. :D

 

If you don't know basic Java yet, go and follow these tutorials.

Link to comment
Share on other sites

Write a wrapper for the ItemStack, now everything is working fine.

/** 
* @author Agravaine
*/
public class ItemStackHash{

private ItemStack iStack;

private boolean doCheckNBT = false;
private boolean doCheckStackSize = false;
private boolean doCheckDamage = false;

public ItemStackHash(Block block){
	this(block, 1);
}

public ItemStackHash(Block block, int count){
	this(block, count, 0);
}

public ItemStackHash(Block block, int count, int damage){
	this(Item.getItemFromBlock(block), count, damage);
}

public ItemStackHash(Item item){
	this(item, 1);
}

public ItemStackHash(Item item, int count){
	this(item, count, 0);
}

public ItemStackHash(Item item, int count, int damage){
	iStack = new ItemStack(Preconditions.checkNotNull(item, "Item is null!"), count, damage);
}

public ItemStackHash(ItemStack itemStack){
	iStack = Preconditions.checkNotNull(itemStack, "ItemStack is null!");
}

private ItemStackHash(){
	throw new UnsupportedOperationException("Unsupported empty constructor!");
}

public ItemStackHash setDoCheckDamage(){
	doCheckDamage = true;
	return this;
}

public ItemStackHash setDoCheckStackSize(){
	doCheckStackSize = true;
	return this;
}

public ItemStackHash setDoCheckNBT(){
	doCheckNBT = true;
	return this;
}

public ItemStackHash setChecks(ItemStackHash parent){
	doCheckNBT = parent.doCheckNBT;
	doCheckDamage = parent.doCheckDamage;
	doCheckStackSize = parent.doCheckStackSize;
	return this;
}

public ItemStack getItemStack(){
	return iStack;
}

public int hashCode(){
	final int prime = 31;
	int result = 1;
	result = prime * result + Item.getIdFromItem(iStack.getItem());
	result = prime * result + (doCheckDamage ? iStack.getItemDamage() : 0);
	result = prime * result + (doCheckStackSize ? iStack.stackSize : 0);
	result = prime * result + (doCheckNBT && iStack.hasTagCompound() ? iStack.stackTagCompound.hashCode() : 0);
	return result;
}

public boolean equals(Object obj) {
	if(!(obj instanceof ItemStackHash)){
		return false;
	}

	ItemStackHash hashStack = (ItemStackHash)obj;

	if(Item.getIdFromItem(iStack.getItem()) != Item.getIdFromItem(hashStack.iStack.getItem())){
		return false;
	}

	if(doCheckDamage && iStack.getItemDamage() != hashStack.iStack.getItemDamage()){
		return false;
	}

	if(doCheckNBT && iStack.stackTagCompound != hashStack.iStack.stackTagCompound){
		return false;
	}

	if(doCheckStackSize && iStack.stackSize != hashStack.iStack.stackSize){
		return false;
	}

	return true;
}

public String toString(){
	return iStack.toString();
}
}

Link to comment
Share on other sites

Make a subclass of Equivalence<ItemStack>. In there you would define the hashCode and equality versions you want for ItemStack. Then make a single instance of this class which you can then use everywhere. Then you can make a wrapper for ItemStacks that uses the methods defined in your Equivalence using .wrap(stack) on with the ItemStack.

 

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Equivalence.html

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.

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

    • Hi i'm currently working on an entity. I just i finished it but it won't spawn and i don't know why. I got this error : java.lang.NullPointerException: Cannot invoke "net.minecraft.world.entity.ai.attributes.AttributeSupplier.getValue(net.minecraft.world.entity.ai.attributes.Attribute)" because "this.supplier" is null at net.minecraft.world.entity.ai.attributes.AttributeMap.getValue(AttributeMap.java:65) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.LivingEntity.getAttributeValue(LivingEntity.java:1806) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.LivingEntity.getMaxHealth(LivingEntity.java:1571) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.LivingEntity.<init>(LivingEntity.java:230) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.Mob.<init>(Mob.java:112) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at com.armanse.dreamy.entity.SleepingPlayerEntity.<init>(SleepingPlayerEntity.java:16) ~[%2379!:?] at net.minecraft.world.entity.EntityType.create(EntityType.java:460) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.EntityType.lambda$create$1(EntityType.java:470) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at java.util.Optional.map(Optional.java:260) ~[?:?] at net.minecraft.world.entity.EntityType.create(EntityType.java:469) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.EntityType.loadStaticEntity(EntityType.java:552) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.EntityType.loadEntityRecursive(EntityType.java:508) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.commands.SummonCommand.spawnEntity(SummonCommand.java:50) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.commands.SummonCommand.lambda$register$1(SummonCommand.java:34) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at com.mojang.brigadier.CommandDispatcher.execute(CommandDispatcher.java:263) ~[brigadier-1.0.18.jar%2341!:?] at net.minecraft.commands.Commands.performCommand(Commands.java:224) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.network.ServerGamePacketListenerImpl.handleCommand(ServerGamePacketListenerImpl.java:1130) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.network.ServerGamePacketListenerImpl.handleChat(ServerGamePacketListenerImpl.java:1109) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.network.ServerGamePacketListenerImpl.handleChat(ServerGamePacketListenerImpl.java:1095) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.network.protocol.game.ServerboundChatPacket.handle(ServerboundChatPacket.java:27) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.network.protocol.game.ServerboundChatPacket.handle(ServerboundChatPacket.java:6) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.network.protocol.PacketUtils.lambda$ensureRunningOnSameThread$0(PacketUtils.java:21) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.TickTask.run(TickTask.java:17) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.util.thread.BlockableEventLoop.doRunTask(BlockableEventLoop.java:139) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.util.thread.ReentrantBlockableEventLoop.doRunTask(ReentrantBlockableEventLoop.java:22) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:775) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:162) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.util.thread.BlockableEventLoop.pollTask(BlockableEventLoop.java:112) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.pollTaskInternal(MinecraftServer.java:758) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.pollTask(MinecraftServer.java:752) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.util.thread.BlockableEventLoop.managedBlock(BlockableEventLoop.java:122) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.waitUntilNextTick(MinecraftServer.java:738) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:687) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.lambda$spin$2(MinecraftServer.java:258) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at java.lang.Thread.run(Thread.java:831) [?:?] I was wondering if the problem was with the attributes of the entity since it is mention at the top but i couldn't find the error (i'm a noob so that's kinda normal). Here is the code related to the attributes: @EventBusSubscriber(modid = DreamyMain.MOD_ID, bus = Bus.FORGE) public class ClientInitEvent { @SubscribeEvent public static final void entityAttributCreation(EntityAttributeCreationEvent event) { event.put(EntityInit.Entity.get(), Entity.createAttributes().build()); } @SubscribeEvent public static final void entityClientRederer(EntityRenderersEvent.RegisterRenderers event) { event.registerEntityRenderer(EntityInit.Entity.get(), Entity::new); } }   //IN The entity class public static AttributeSupplier.Builder createAttributes() { return Mob.createMobAttributes().add(Attributes.MAX_HEALTH, 20D).add(Attributes.MOVEMENT_SPEED, 0D); }  
    • I removed a bunch of mods that said other version than 1.16.5 seems to be working. Still its kinda weird because it clearly said that the "game version" was 1.16.5. Still not happy bout it  
    • From the looks of it LoomContainer still doesn't call onCraftMatrixChanged... What am i missing seriously?
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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