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

    • Please stop capitalizing every word... You need to call NetworkHooks.openGui on the server to open your GUI. You also need to register your ScreenFactory on the client.
    • 1.12 is no longer supported on this forum. Please update to a modern version of Minecraft to receive support.
    • Server Version: 1.17.1 Server IP: enhanced.snorkycraft.com Discord:http://discord.snorkycraft.com/ Snorky Craft World is a playground I have designed for myself and friends to enjoy minecraft, with just some difficult enhancing and extra Contents, i'm an active player on my own server, grinding side by side with players. I do have a TLDR down at the bottom, if you're too lazy to read ;P. Showcase:https://www.youtube.com/watch?v=is7JoucsK0s A short 6 minutes video we have showcasing someone fun time in 2 lairs! a mini-world boss and a world boss. Things we have! ● KeepInvetory - You keep inventory but whenever you die, all your tools and armours lose durability. ● GriefPrevention - Well just your good ol' land claiming area to protect base from griefers ● mcMMO - Well, most of you might know what plugin is this, it's the kind of plugin that help you grow stronger the more you play! ● EliteMobs - Enhancing the mobs around you the more stronger you grow! and also allowing us to replace, create contents for you to enjoy! ● PlaytimeRewards Some Fun Features! Ridable Certain Animals, 6-Rows Enderchest!, Bonus ClaimBlocks, and a very spicy rank! ● Some Funzies Feautures: Editable armor stands! want to decorate your home and make it more lovely? you can with editable armorstands!, 6-Rows barrels! just some extra storage for your home! ● Some...lore related features: Some kind of strange disease have infected the cows... they attack!, Chicken are very feisty! Gameplay Changes ● Disabled Spawners AI, what's the point of spawners, they just ruins the game and there's nothing fun about it, also spawners aren't very useful in my server due to how it's setup. ● Removed The End world exploration! replaced it with the final boss stage for a very long grinding journey, one of our grinder has exactly 60+ Hours of playtime and he only reached Prestige 2! there are 10 Prestiges in total in order to have the power to defeat the new Ender Dragon Boss ● Progression changes, The end goal is not just getting to the ender dragon, is ranking up a custom rank system and farming custom items in order to grow stronger, strong enough to challenge very hard bosses! ● Monsters grows stronger the better equipped you are! and the more stronger you grow they harder challenges/dungeons/lairs you can fight! TLDR: You Join, You Lookaround, You Mine, You Build a House, You farm and gear up, Realized Diamond gearset or netherite gearset full set is so weak!, you start to farm small low level dungeons, you gain great gear, you defeat low level boss, you rankup, you get stronger gear, you repeat this progress so many times you have to prestiges too!, it's pretty fun! i been grinding 10-12 hours a day because once you get going it's very enjoyable.
    • The inputs are just a template- in this case it's two strings, but they can be ints, chars, etc. or none at all, depending if you need to initially send info across sides. I'd suggest reading Forge's documentation on packets first if you haven't used them before:  Forge Docs Networking You usually send the packet by invoking handler methods: ModPacketHandler.sendToServer(new C2SPacketForContainer(input1, input2)); Where the handler class has packet registration and the methods: The register methods are called in common setup.
    • Yeah the VillagerUtil was used in conjunction with the POI Forge registry, but you're right it's superfluous and I should stick with the simpler way. Thank you Ash
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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