Replace wildcard with metadata 0 for display purposes, fixes #370

N.B. The output is still not visible for non-zero inputs when using wildcard trades, but for now this is good enough
This commit is contained in:
Electroblob77
2020-03-18 15:32:59 +00:00
parent 587ab1338e
commit 0b56d47022
3 changed files with 48 additions and 2 deletions
@@ -503,7 +503,6 @@ public class EntityWizard extends EntityCreature implements INpc, IMerchant, ISp
ItemStack anySpellBook = new ItemStack(WizardryItems.spell_book, 1, OreDictionary.WILDCARD_VALUE);
ItemStack crystalStack = new ItemStack(WizardryItems.magic_crystal, 5);
// NOTE: For wizardry 1.2, increase the number of uses of this trade. The default is 7, for reference.
this.trades.add(new MerchantRecipe(anySpellBook, crystalStack));
this.addRandomRecipes(3);
@@ -1,8 +1,10 @@
package electroblob.wizardry.misc;
import electroblob.wizardry.util.WizardryUtilities;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTUtil;
import net.minecraft.network.PacketBuffer;
import net.minecraft.village.MerchantRecipe;
import net.minecraft.village.MerchantRecipeList;
import net.minecraftforge.oredict.OreDictionary;
@@ -46,12 +48,42 @@ public class WildcardTradeList extends MerchantRecipeList {
}
private boolean areItemStacksExactlyEqual(ItemStack stack1, ItemStack stack2){
// Added to allow wildcards; this line is the only actual change.
// Added to allow wildcards
if((stack1.getItemDamage() == OreDictionary.WILDCARD_VALUE || stack2.getItemDamage() == OreDictionary.WILDCARD_VALUE)
// Can't use ItemStack.areItemsEqualIgnoreDurability because that only works for items with durability, not subtypes.
&& stack1.getItem() == stack2.getItem()) return true;
return ItemStack.areItemsEqual(stack1, stack2) && (!stack2.hasTagCompound() || stack1.hasTagCompound() && NBTUtil.areNBTEquals(stack2.getTagCompound(), stack1.getTagCompound(), false));
}
@Override
public void writeToBuf(PacketBuffer buffer){
buffer.writeByte((byte)(this.size() & 255));
// Trick the client into thinking this is a normal item
for(MerchantRecipe merchantrecipe : this){
ItemStack itemToBuy = merchantrecipe.getItemToBuy();
if(itemToBuy.getMetadata() == OreDictionary.WILDCARD_VALUE) itemToBuy = WizardryUtilities.copyWithMeta(itemToBuy, 0);
buffer.writeItemStack(itemToBuy);
ItemStack itemToSell = merchantrecipe.getItemToSell();
if(itemToSell.getMetadata() == OreDictionary.WILDCARD_VALUE) itemToSell = WizardryUtilities.copyWithMeta(itemToSell, 0);
buffer.writeItemStack(itemToSell);
ItemStack secondItemToBuy = merchantrecipe.getSecondItemToBuy();
buffer.writeBoolean(!secondItemToBuy.isEmpty());
if(!secondItemToBuy.isEmpty()){
if(secondItemToBuy.getMetadata() == OreDictionary.WILDCARD_VALUE) secondItemToBuy = WizardryUtilities.copyWithMeta(secondItemToBuy, 0);
buffer.writeItemStack(secondItemToBuy);
}
buffer.writeBoolean(merchantrecipe.isRecipeDisabled());
buffer.writeInt(merchantrecipe.getToolUses());
buffer.writeInt(merchantrecipe.getMaxTradeUses());
}
}
}
@@ -24,6 +24,7 @@ import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.inventory.EntityEquipmentSlot.Type;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.*;
@@ -795,6 +796,20 @@ public final class WizardryUtilities {
return false;
}
/**
* Returns a new {@link ItemStack} that is identical to the supplied one, except with the metadata changed to the
* new value given.
* @param toCopy The stack to copy
* @param newMetadata The new metadata value
* @return The resulting {@link ItemStack}
*/
public static ItemStack copyWithMeta(ItemStack toCopy, int newMetadata){
ItemStack copy = new ItemStack(toCopy.getItem(), toCopy.getCount(), newMetadata);
NBTTagCompound compound = toCopy.getTagCompound();
if(compound != null) copy.setTagCompound(compound.copy());
return copy;
}
/**
* Checks if the given player is opped on the given server. If the server is a singleplayer or LAN server, this
* means they have cheats enabled.