Skip to content

Commit

Permalink
approach to ignore nbt values for saplings (ldtteam#2377)
Browse files Browse the repository at this point in the history
Saplings should ignore nbt to avoid millions of saplings like for example of forestry
  • Loading branch information
Raycoms authored Apr 15, 2018
1 parent c98ba69 commit 729ca94
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public void discover(final World world)
@Override
public IBlockState getLeaveForSapling(final ItemStack stack)
{
if(leavesToSaplingMap.inverse().containsKey(new ItemStorage(stack)))
if(leavesToSaplingMap.inverse().containsKey(new ItemStorage(stack, false, true)))
{
return leavesToSaplingMap.inverse().get(new ItemStorage(stack));
return leavesToSaplingMap.inverse().get(new ItemStorage(stack, false, true));
}
return null;
}
Expand Down Expand Up @@ -148,7 +148,7 @@ public void readFromNBT(@NotNull final NBTTagCompound compound)
.map(CompatabilityManager::readLeaveSaplingEntryFromNBT)
.collect(Collectors.toMap(Tuple::getFirst, Tuple::getSecond)));
final List<ItemStorage> storages = NBTUtils.streamCompound(compound.getTagList(TAG_SAPLINGS, Constants.NBT.TAG_COMPOUND))
.map(tempCompound -> new ItemStorage(new ItemStack(tempCompound)))
.map(tempCompound -> new ItemStorage(new ItemStack(tempCompound), false, true))
.collect(Collectors.toList());

//Filter duplicated values.
Expand Down Expand Up @@ -178,9 +178,9 @@ public void connectLeaveToSapling(final IBlockState leave, final ItemStack stack
{
final ItemStack tempStack = new ItemStack(leave.getBlock(), 1, leave.getBlock().getMetaFromState(leave));
final IBlockState tempLeave = BlockLeaves.getBlockFromItem(tempStack.getItem()).getStateFromMeta(tempStack.getMetadata());
if(!leavesToSaplingMap.containsKey(tempLeave) && !leavesToSaplingMap.containsValue(new ItemStorage(stack)))
if(!leavesToSaplingMap.containsKey(tempLeave) && !leavesToSaplingMap.containsValue(new ItemStorage(stack, false, true)))
{
leavesToSaplingMap.put(tempLeave, new ItemStorage(stack));
leavesToSaplingMap.put(tempLeave, new ItemStorage(stack, false, true));
}
}

Expand Down Expand Up @@ -223,9 +223,9 @@ private void discoverSaplings()
for (final ItemStack stack : list)
{
//Just put it in if not in there already, don't mind the leave yet.
if(!ItemStackUtils.isEmpty(stack) && !leavesToSaplingMap.containsValue(new ItemStorage(stack)) && !saplings.contains(new ItemStorage(stack)))
if(!ItemStackUtils.isEmpty(stack) && !leavesToSaplingMap.containsValue(new ItemStorage(stack, false, true)) && !saplings.contains(new ItemStorage(stack, false, true)))
{
saplings.add(new ItemStorage(stack));
saplings.add(new ItemStorage(stack, false, true));
}
}
}
Expand All @@ -244,6 +244,6 @@ private static NBTTagCompound writeLeaveSaplingEntryToNBT(final IBlockState stat

private static Tuple<IBlockState, ItemStorage> readLeaveSaplingEntryFromNBT(final NBTTagCompound compound)
{
return new Tuple<>(NBTUtil.readBlockState(compound), new ItemStorage(new ItemStack(compound)));
return new Tuple<>(NBTUtil.readBlockState(compound), new ItemStorage(new ItemStack(compound), false, true));
}
}
29 changes: 26 additions & 3 deletions src/api/java/com/minecolonies/api/crafting/ItemStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public class ItemStorage
*/
private final boolean shouldIgnoreDamageValue;

/**
* Set this to ignore the damage value in comparisons.
*/
private final boolean shouldIgnoreNBTValue;

/**
* Amount of the storage.
*/
Expand All @@ -40,6 +45,22 @@ public ItemStorage(@NotNull final ItemStack stack, final int amount, final boole
{
this.stack = stack;
this.shouldIgnoreDamageValue = ignoreDamageValue;
this.shouldIgnoreNBTValue = ignoreDamageValue;
this.amount = amount;
}

/**
* Creates an instance of the storage.
*
* @param stack the stack.
* @param ignoreDamageValue should the damage value be ignored?
* @param shouldIgnoreNBTValue should the nbt value be ignored?
*/
public ItemStorage(@NotNull final ItemStack stack, final boolean ignoreDamageValue, final boolean shouldIgnoreNBTValue)
{
this.stack = stack;
this.shouldIgnoreDamageValue = ignoreDamageValue;
this.shouldIgnoreNBTValue = shouldIgnoreNBTValue;
this.amount = amount;
}

Expand All @@ -53,6 +74,7 @@ public ItemStorage(@NotNull final ItemStack stack, final boolean ignoreDamageVal
{
this.stack = stack;
this.shouldIgnoreDamageValue = ignoreDamageValue;
this.shouldIgnoreNBTValue = ignoreDamageValue;
this.amount = ItemStackUtils.getSize(stack);
}

Expand All @@ -65,6 +87,7 @@ public ItemStorage(@NotNull final ItemStack stack)
{
this.stack = stack;
this.shouldIgnoreDamageValue = false;
this.shouldIgnoreNBTValue = false;
this.amount = ItemStackUtils.getSize(stack);
}

Expand Down Expand Up @@ -131,8 +154,8 @@ public boolean ignoreDamageValue()
public int hashCode()
{
return Objects.hash(stack.getItem())
+ (shouldIgnoreDamageValue ? 0 : (stack.getItemDamage() * 31))
+ ((stack.getTagCompound() == null) ? 0 : stack.getTagCompound().hashCode());
+ (this.shouldIgnoreDamageValue ? 0 : (this.stack.getItemDamage() * 31))
+ (this.shouldIgnoreNBTValue ? 0 : ((this.stack.getTagCompound() == null) ? 0 : this.stack.getTagCompound().hashCode()));
}

@Override
Expand All @@ -152,7 +175,7 @@ public boolean equals(final Object o)

return stack.isItemEqual(that.getItemStack())
&& (this.shouldIgnoreDamageValue || that.getDamageValue() == this.getDamageValue())
&& that.getItemStack().getTagCompound() == this.getItemStack().getTagCompound();
&& (this.shouldIgnoreNBTValue || that.getItemStack().getTagCompound() == this.getItemStack().getTagCompound());
}

/**
Expand Down

0 comments on commit 729ca94

Please sign in to comment.