From 02de82951d65c5b964b112460cf598af81bbcc2b Mon Sep 17 00:00:00 2001 From: Bernhard Bonigl Date: Fri, 8 Aug 2014 12:58:29 +0200 Subject: [PATCH] Allow upgrading the Flux-Modifier, if the new battery is bigger --- .../tconstruct/modifiers/tools/ModFlux.java | 116 ++++++++++-------- 1 file changed, 68 insertions(+), 48 deletions(-) diff --git a/src/main/java/tconstruct/modifiers/tools/ModFlux.java b/src/main/java/tconstruct/modifiers/tools/ModFlux.java index 0ec8d7e8ab5..c89d3a38940 100644 --- a/src/main/java/tconstruct/modifiers/tools/ModFlux.java +++ b/src/main/java/tconstruct/modifiers/tools/ModFlux.java @@ -13,8 +13,8 @@ public class ModFlux extends ModBoolean { - public ArrayList batteries = new ArrayList(); + public int modifiersRequired = 1; // LALALALA totally not hidden IguanaTweaks Support LALALALA public ModFlux() { @@ -24,11 +24,9 @@ public ModFlux() @Override public boolean matches (ItemStack[] input, ItemStack tool) { - // check if the regular limitations apply - if(!canModify(tool, input)) - return false; + NBTTagCompound tags = tool.getTagCompound().getCompoundTag("InfiTool"); - boolean foundBattery = false; + ItemStack foundBattery = null; // try to find the battery in the input for(ItemStack stack : input) for(ItemStack battery : batteries) @@ -40,15 +38,32 @@ public boolean matches (ItemStack[] input, ItemStack tool) if(!(stack.getItem() instanceof IEnergyContainerItem)) continue; // we don't allow multiple batteries to be added - if(foundBattery) + if(foundBattery != null) return false; // battery found, gogogo - foundBattery = true; + foundBattery = stack; } - // no battery found :( - return foundBattery; + // no battery present + if(foundBattery == null) + return false; + + // check if we already have a flux modifier + if(tags.getBoolean(key)) + { + // only allow if it's an upgrade + // remark: we use the ToolCores function here instead of accessing the tag directly, to achieve backwards compatibility with tools without tags. + int a = ((IEnergyContainerItem)foundBattery.getItem()).getMaxEnergyStored(foundBattery); + int b = ((ToolCore)tool.getItem()).getMaxEnergyStored(tool); + return ((IEnergyContainerItem)foundBattery.getItem()).getMaxEnergyStored(foundBattery) > ((ToolCore)tool.getItem()).getMaxEnergyStored(tool); + } + // otherwise check if we have enough modfiers + else if(tags.getInteger("Modifiers") < modifiersRequired) + return false; + + // all requirements satisfied! + return true; } @Override @@ -56,51 +71,56 @@ public void modify (ItemStack[] input, ItemStack tool) { NBTTagCompound tags = tool.getTagCompound(); - if (!tags.hasKey(key)) - { - tags.setBoolean(key, true); - + // update modifiers (only applies if it's not an upgrade) + if(!tags.hasKey(key)) { int modifiers = tags.getCompoundTag("InfiTool").getInteger("Modifiers"); - modifiers -= 1; + modifiers -= modifiersRequired; tags.getCompoundTag("InfiTool").setInteger("Modifiers", modifiers); + addModifierTip(tool, "\u00a7eFlux"); + } + tags.getCompoundTag("InfiTool").setBoolean(key, true); - // find the battery in the input - ItemStack inputBattery = null; - for(ItemStack stack : input) - for(ItemStack battery : batteries) - { - if(stack == null) - continue; - if(stack.getItem() != battery.getItem()) - continue; - if(!(stack.getItem() instanceof IEnergyContainerItem)) - continue; - - // we're guaranteed to only find one battery because more are prevented above - inputBattery = stack; - } - - // get the energy interface - IEnergyContainerItem energyContainer = (IEnergyContainerItem)inputBattery.getItem(); - - // set the charge values - int charge = energyContainer.getEnergyStored(inputBattery); - int maxCharge = energyContainer.getMaxEnergyStored(inputBattery); - // simulate transferring maximum amount of POWER to obtain the maximum receive-limit of the battery ;_; - int maxExtract = energyContainer.extractEnergy(inputBattery, Integer.MAX_VALUE, true); - int maxReceive = energyContainer.receiveEnergy(inputBattery, Integer.MAX_VALUE, true); - tags.setInteger("Energy", charge); - tags.setInteger("EnergyMax", maxCharge); - tags.setInteger("EnergyExtractionRate", maxExtract); - tags.setInteger("EnergyReceiveRate", maxReceive); + // find the battery in the input + ItemStack inputBattery = null; + for(ItemStack stack : input) + for(ItemStack battery : batteries) + { + if(stack == null) + continue; + if(stack.getItem() != battery.getItem()) + continue; + if(!(stack.getItem() instanceof IEnergyContainerItem)) + continue; - tags.setInteger(key, 1); + // we're guaranteed to only find one battery because more are prevented above + inputBattery = stack; + } - addModifierTip(tool, "\u00a7eFlux"); - ToolCore toolcore = (ToolCore) tool.getItem(); - tool.setItemDamage(1 + (toolcore.getMaxEnergyStored(tool) - charge) * (tool.getMaxDamage() - 1) / toolcore.getMaxEnergyStored(tool)); - } + // get the energy interface + IEnergyContainerItem energyContainer = (IEnergyContainerItem)inputBattery.getItem(); + + // set the charge values + int charge = energyContainer.getEnergyStored(inputBattery); + // add already present charge in the tool + if(tags.hasKey("Energy")) + charge += tags.getInteger("Energy"); + int maxCharge = energyContainer.getMaxEnergyStored(inputBattery); + // simulate transferring maximum amount of POWER to obtain the maximum receive-limit of the battery ;_; + int maxExtract = energyContainer.extractEnergy(inputBattery, Integer.MAX_VALUE, true); + int maxReceive = energyContainer.receiveEnergy(inputBattery, Integer.MAX_VALUE, true); + + // make sure we don't overcharge + charge = Math.min(charge, maxCharge); + + tags.setInteger("Energy", charge); + tags.setInteger("EnergyMax", maxCharge); + tags.setInteger("EnergyExtractionRate", maxExtract); + tags.setInteger("EnergyReceiveRate", maxReceive); + + tags.setInteger(key, 1); + ToolCore toolcore = (ToolCore) tool.getItem(); + tool.setItemDamage(1 + (toolcore.getMaxEnergyStored(tool) - charge) * (tool.getMaxDamage() - 1) / toolcore.getMaxEnergyStored(tool)); } }