Skip to content

Commit

Permalink
Allow upgrading the Flux-Modifier, if the new battery is bigger
Browse files Browse the repository at this point in the history
  • Loading branch information
bonii-xx committed Aug 8, 2014
1 parent 2510cfb commit 02de829
Showing 1 changed file with 68 additions and 48 deletions.
116 changes: 68 additions & 48 deletions src/main/java/tconstruct/modifiers/tools/ModFlux.java
Expand Up @@ -13,8 +13,8 @@

public class ModFlux extends ModBoolean
{

public ArrayList<ItemStack> batteries = new ArrayList<ItemStack>();
public int modifiersRequired = 1; // LALALALA totally not hidden IguanaTweaks Support LALALALA

public ModFlux()
{
Expand All @@ -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)
Expand All @@ -40,67 +38,89 @@ 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
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));
}
}

0 comments on commit 02de829

Please sign in to comment.