From 61ca28d520daa0ac294d0430c2ea6ee3b68fea00 Mon Sep 17 00:00:00 2001 From: mezz Date: Wed, 25 May 2016 02:02:03 -0700 Subject: [PATCH] Fix #1216 Items partially dragged from Alyzer input are not analyzed --- .../core/inventory/ItemInventory.java | 6 +- .../core/inventory/ItemInventoryAlyzer.java | 57 ++++++++----------- .../java/forestry/core/items/ItemAlyzer.java | 5 +- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/main/java/forestry/core/inventory/ItemInventory.java b/src/main/java/forestry/core/inventory/ItemInventory.java index e59283eb4..089037f27 100755 --- a/src/main/java/forestry/core/inventory/ItemInventory.java +++ b/src/main/java/forestry/core/inventory/ItemInventory.java @@ -10,6 +10,7 @@ ******************************************************************************/ package forestry.core.inventory; +import javax.annotation.Nonnull; import java.util.Random; import net.minecraft.entity.player.EntityPlayer; @@ -35,11 +36,12 @@ public abstract class ItemInventory implements IInventory, IFilterSlotDelegate, private static final Random rand = new Random(); private final IItemHandler itemHandler = new InvWrapper(this); - private final EntityPlayer player; + @Nonnull + protected final EntityPlayer player; private final ItemStack parent; private final ItemStack[] inventoryStacks; - public ItemInventory(EntityPlayer player, int size, ItemStack parent) { + public ItemInventory(@Nonnull EntityPlayer player, int size, ItemStack parent) { this.player = player; this.parent = parent; this.inventoryStacks = new ItemStack[size]; diff --git a/src/main/java/forestry/core/inventory/ItemInventoryAlyzer.java b/src/main/java/forestry/core/inventory/ItemInventoryAlyzer.java index 1e75e0c9d..91f43abe5 100644 --- a/src/main/java/forestry/core/inventory/ItemInventoryAlyzer.java +++ b/src/main/java/forestry/core/inventory/ItemInventoryAlyzer.java @@ -12,6 +12,7 @@ package forestry.core.inventory; import com.google.common.collect.ImmutableSet; +import javax.annotation.Nonnull; import java.util.Collections; import java.util.List; @@ -30,7 +31,6 @@ import forestry.api.genetics.IIndividual; import forestry.api.genetics.ISpeciesRoot; import forestry.apiculture.PluginApiculture; import forestry.apiculture.items.ItemRegistryApiculture; -import forestry.core.PluginCore; import forestry.core.errors.EnumErrorCode; import forestry.core.gui.IHintSource; import forestry.core.utils.GeneticsUtil; @@ -45,7 +45,7 @@ public class ItemInventoryAlyzer extends ItemInventory implements IErrorSource, public static final int SLOT_ANALYZE_4 = 5; public static final int SLOT_ANALYZE_5 = 6; - public ItemInventoryAlyzer(EntityPlayer player, ItemStack itemstack) { + public ItemInventoryAlyzer(@Nonnull EntityPlayer player, ItemStack itemstack) { super(player, 7, itemstack); } @@ -69,14 +69,14 @@ public class ItemInventoryAlyzer extends ItemInventory implements IErrorSource, return isAlyzingFuel(itemStack); } - itemStack = GeneticsUtil.convertToGeneticEquivalent(itemStack); - ISpeciesRoot speciesRoot = AlleleManager.alleleRegistry.getSpeciesRoot(itemStack); - if (speciesRoot == null) { + // only allow one slot to be used at a time + if (hasSpecimen() && getStackInSlot(slotIndex) == null) { return false; } - // only allow one slot to be used at a time - if (hasSpecimen() && getStackInSlot(slotIndex) == null) { + itemStack = GeneticsUtil.convertToGeneticEquivalent(itemStack); + ISpeciesRoot speciesRoot = AlleleManager.alleleRegistry.getSpeciesRoot(itemStack); + if (speciesRoot == null) { return false; } @@ -90,20 +90,13 @@ public class ItemInventoryAlyzer extends ItemInventory implements IErrorSource, @Override public void setInventorySlotContents(int slotIndex, ItemStack itemStack) { - if(!hasSpecimen() && slotIndex == SLOT_SPECIMEN){ - ItemStack ersatz = GeneticsUtil.convertToGeneticEquivalent(itemStack); - if(ersatz != null){ - super.setInventorySlotContents(slotIndex, ersatz); - return; - } - } super.setInventorySlotContents(slotIndex, itemStack); + if (slotIndex == SLOT_SPECIMEN) { + analyzeSpecimen(itemStack); + } } - - @Override - public void onSlotClick(int slotIndex, EntityPlayer player) { - // Source slot to analyze empty - ItemStack specimen = getStackInSlot(SLOT_SPECIMEN); + + private void analyzeSpecimen(ItemStack specimen) { if (specimen == null) { return; } @@ -125,32 +118,32 @@ public class ItemInventoryAlyzer extends ItemInventory implements IErrorSource, // Analyze if necessary if (!individual.isAnalyzed()) { - - if (ForestryAPI.enabledPlugins.contains(ForestryPluginUids.APICULTURE)) { + final boolean requiresEnergy = ForestryAPI.enabledPlugins.contains(ForestryPluginUids.APICULTURE); + if (requiresEnergy) { // Requires energy if (!isAlyzingFuel(getStackInSlot(SLOT_ENERGY))) { return; } } - - individual.analyze(); - if (player != null) { + + if (individual.analyze()) { IBreedingTracker breedingTracker = speciesRoot.getBreedingTracker(player.worldObj, player.getGameProfile()); breedingTracker.registerSpecies(individual.getGenome().getPrimary()); breedingTracker.registerSpecies(individual.getGenome().getSecondary()); + + NBTTagCompound nbttagcompound = new NBTTagCompound(); + individual.writeToNBT(nbttagcompound); + specimen.setTagCompound(nbttagcompound); + + if (requiresEnergy) { + // Decrease energy + decrStackSize(SLOT_ENERGY, 1); + } } - - NBTTagCompound nbttagcompound = new NBTTagCompound(); - individual.writeToNBT(nbttagcompound); - specimen.setTagCompound(nbttagcompound); - - // Decrease energy - decrStackSize(SLOT_ENERGY, 1); } setInventorySlotContents(SLOT_ANALYZE_1, specimen); setInventorySlotContents(SLOT_SPECIMEN, null); - PluginCore.items.portableAlyzer.openGui(player); } @Override diff --git a/src/main/java/forestry/core/items/ItemAlyzer.java b/src/main/java/forestry/core/items/ItemAlyzer.java index 7620532d1..f43f00e92 100644 --- a/src/main/java/forestry/core/items/ItemAlyzer.java +++ b/src/main/java/forestry/core/items/ItemAlyzer.java @@ -10,6 +10,7 @@ ******************************************************************************/ package forestry.core.items; +import javax.annotation.Nonnull; import java.util.List; import net.minecraft.entity.player.EntityPlayer; @@ -32,12 +33,12 @@ public class ItemAlyzer extends ItemWithGui { } @Override - public Object getGui(EntityPlayer player, ItemStack heldItem, int data) { + public Object getGui(@Nonnull EntityPlayer player, ItemStack heldItem, int data) { return new GuiAlyzer(player, new ItemInventoryAlyzer(player, heldItem)); } @Override - public Object getContainer(EntityPlayer player, ItemStack heldItem, int data) { + public Object getContainer(@Nonnull EntityPlayer player, ItemStack heldItem, int data) { return new ContainerAlyzer(new ItemInventoryAlyzer(player, heldItem), player); }