use getters & setters for InfuseStorage (#5416)

* use getters & setters for InfuseStorage to prevent type/amount discrepancies

* Fix a few checks when reading from network/NBT

* Fix some spacing I screwed up by accident

* fix metallurgic infuser gui texture for dark gauge
This commit is contained in:
Xander authored and GitHub committed 2019-04-20 13:38:41 +08:00
1 parent bbcea85fc4
commit 0dba7cb584
14 files changed
+135 -87

No files matched your search

@@ -76,8 +76,8 @@ public class GuiFactory extends GuiMekanismTile<TileEntityFactory> {
+ ": " + tileEntity.gasTank.getStored() : LangUtils.localize("gui.none"), xAxis, yAxis);
} else if (tileEntity.getRecipeType() == RecipeType.INFUSING) {
drawHoveringText(
tileEntity.infuseStored.type != null ? tileEntity.infuseStored.type.getLocalizedName() + ": "
+ tileEntity.infuseStored.amount : LangUtils.localize("gui.empty"), xAxis, yAxis);
tileEntity.infuseStored.getType() != null ? tileEntity.infuseStored.getType().getLocalizedName() + ": "
+ tileEntity.infuseStored.getAmount() : LangUtils.localize("gui.empty"), xAxis, yAxis);
}
}
super.drawGuiContainerForegroundLayer(mouseX, mouseY);
@@ -116,7 +116,7 @@ public class GuiFactory extends GuiMekanismTile<TileEntityFactory> {
}
} else if (tileEntity.getRecipeType() == RecipeType.INFUSING) {
if (tileEntity.getScaledInfuseLevel(160) > 0) {
displayGauge(8, 78, tileEntity.getScaledInfuseLevel(160), 5, tileEntity.infuseStored.type.sprite);
displayGauge(8, 78, tileEntity.getScaledInfuseLevel(160), 5, tileEntity.infuseStored.getType().sprite);
}
}
super.drawGuiContainerBackgroundLayer(partialTick, mouseX, mouseY);
@@ -71,8 +71,8 @@ public class GuiMetallurgicInfuser extends GuiMekanismTile<TileEntityMetallurgic
int yAxis = (mouseY - (height - ySize) / 2);
if (xAxis >= 7 && xAxis <= 11 && yAxis >= 17 && yAxis <= 69) {
drawHoveringText(
tileEntity.infuseStored.type != null ? tileEntity.infuseStored.type.getLocalizedName() + ": "
+ tileEntity.infuseStored.amount : LangUtils.localize("gui.empty"), xAxis, yAxis);
tileEntity.infuseStored.getType() != null ? tileEntity.infuseStored.getType().getLocalizedName() + ": "
+ tileEntity.infuseStored.getAmount() : LangUtils.localize("gui.empty"), xAxis, yAxis);
}
super.drawGuiContainerForegroundLayer(mouseX, mouseY);
}
@@ -84,11 +84,11 @@ public class GuiMetallurgicInfuser extends GuiMekanismTile<TileEntityMetallurgic
int guiWidth = (width - xSize) / 2;
int guiHeight = (height - ySize) / 2;
drawTexturedModalRect(guiWidth, guiHeight, 0, 0, xSize, ySize);
if (tileEntity.infuseStored.type != null) {
if (tileEntity.infuseStored.getType() != null) {
mc.renderEngine.bindTexture(MekanismRenderer.getBlocksTexture());
int displayInt = tileEntity.getScaledInfuseLevel(52);
drawTexturedRectFromIcon(guiWidth + 7, guiHeight + 17 + 52 - displayInt,
tileEntity.infuseStored.type.sprite, 4, displayInt);
tileEntity.infuseStored.getType().sprite, 4, displayInt);
}
super.drawGuiContainerBackgroundLayer(partialTick, mouseX, mouseY);
}
@@ -65,7 +65,7 @@ public class MetallurgicInfuserRecipeCategory extends BaseRecipeCategory {
itemStacks.init(2, true, 11, 18);
itemStacks.set(0, tempRecipe.getInput().inputStack);
itemStacks.set(1, tempRecipe.getOutput().output);
itemStacks.set(2, getInfuseStacks(tempRecipe.getInput().infuse.type));
itemStacks.set(2, getInfuseStacks(tempRecipe.getInput().infuse.getType()));
}
}
}
@@ -23,7 +23,7 @@ public class MetallurgicInfuserRecipeWrapper implements IRecipeWrapper {
@Override
public void getIngredients(IIngredients ingredients) {
List<ItemStack> inputStacks = Collections.singletonList(recipe.recipeInput.inputStack);
List<ItemStack> infuseStacks = MetallurgicInfuserRecipeCategory.getInfuseStacks(recipe.getInput().infuse.type);
List<ItemStack> infuseStacks = MetallurgicInfuserRecipeCategory.getInfuseStacks(recipe.getInput().infuse.getType());
ingredients.setInput(VanillaTypes.ITEM, recipe.recipeInput.inputStack);
ingredients.setInputLists(VanillaTypes.ITEM, Arrays.asList(inputStacks, infuseStacks));
ingredients.setOutput(VanillaTypes.ITEM, recipe.recipeOutput.output);
@@ -37,7 +37,7 @@ public class MetallurgicInfuserRecipeWrapper implements IRecipeWrapper {
public void drawInfo(Minecraft mc, int recipeWidth, int recipeHeight, int mouseX, int mouseY) {
if (mc.currentScreen != null) {
mc.renderEngine.bindTexture(MekanismRenderer.getBlocksTexture());
mc.currentScreen.drawTexturedModalRect(2, 2, recipe.getInput().infuse.type.sprite, 4, 52);
mc.currentScreen.drawTexturedModalRect(2, 2, recipe.getInput().infuse.getType().sprite, 4, 52);
}
}
@@ -45,7 +45,7 @@ public class MetallurgicInfuserRecipeWrapper implements IRecipeWrapper {
public List<String> getTooltipStrings(int mouseX, int mouseY) {
if (mouseX >= 2 && mouseX < 6 && mouseY >= 2 && mouseY < 54) {
InfuseStorage infuse = recipe.getInput().infuse;
return Collections.singletonList(infuse.type.getLocalizedName() + ": " + infuse.amount);
return Collections.singletonList(infuse.getType().getLocalizedName() + ": " + infuse.getAmount());
}
return Collections.emptyList();
}
@@ -1,5 +1,6 @@
package mekanism.common;
import mekanism.api.infuse.InfuseObject;
import mekanism.api.infuse.InfuseRegistry;
import mekanism.api.infuse.InfuseType;
import mekanism.common.base.ISustainedData;
@@ -8,9 +9,9 @@ import net.minecraft.item.ItemStack;
public class InfuseStorage implements ISustainedData {
public InfuseType type;
private InfuseType type;
public int amount;
private int amount;
public InfuseStorage() {
}
@@ -30,6 +31,32 @@ public class InfuseStorage implements ISustainedData {
} else if (type == storage.type) {
amount = 0;
}
if (amount <= 0) {
type = null;
amount = 0;
}
}
public void increase(InfuseStorage input) {
if (type == null) {
type = input.type;
amount = input.amount;
} else if (type == input.type) {
amount += input.amount;
} else {
Mekanism.logger.error("Tried to increase infusion storage with an incompatible type", new Exception());
}
}
public void increase(InfuseObject input) {
if (type == null) {
type = input.type;
amount = input.stored;
} else if (type == input.type) {
amount += input.stored;
} else {
Mekanism.logger.error("Tried to increase infusion storage with an incompatible type", new Exception());
}
}
@Override
@@ -52,4 +79,30 @@ public class InfuseStorage implements ISustainedData {
amount = 0;
}
}
}
public InfuseType getType() {
return amount == 0 ? null : type;
}
public InfuseStorage setType(InfuseType type) {
this.type = type;
return this;
}
public int getAmount() {
return type == null ? 0 : amount;
}
public InfuseStorage setAmount(int amount) {
if (amount < 0) {
throw new IllegalArgumentException("Amount must be positive");
}
this.amount = amount;
return this;
}
public void setEmpty() {
this.amount = 0;
this.type = null;
}
}
@@ -168,7 +168,7 @@ public interface IFactory {
} else if (fuelType == MachineFuelType.CHANCE) {
return getChanceRecipe(slotStack);
} else if (this == INFUSING) {
if (infuse.type != null) {
if (infuse.getType() != null) {
return RecipeHandler.getMetallurgicInfuserRecipe(new InfusionInput(infuse, slotStack));
} else {
for (Entry<InfusionInput, MetallurgicInfuserRecipe> entry : Recipe.METALLURGIC_INFUSER.get()
@@ -173,8 +173,8 @@ public class MekRecipesCommand extends CraftTweakerCommand {
type = Recipe.METALLURGIC_INFUSER;
for (MetallurgicInfuserRecipe recipe : Recipe.METALLURGIC_INFUSER.get().values()) {
CraftTweakerAPI.logCommand(String.format("mods.mekanism.infuser.addRecipe(%s, %s, %s, %s)",
recipe.getInput().infuse.type,
recipe.getInput().infuse.amount,
recipe.getInput().infuse.getType(),
recipe.getInput().infuse.getAmount(),
RecipeInfoHelper.getItemName(recipe.getInput().inputStack),
RecipeInfoHelper.getItemName(recipe.getOutput().output)
));
@@ -110,7 +110,7 @@ public class IngredientHelper {
} else if (in instanceof InfusionInput) {
InfusionInput input = (InfusionInput) in;
return matches(input.inputStack, toMatch.getIngredient()) && (toMatch.getInfuseType().isEmpty() || toMatch
.getInfuseType().equalsIgnoreCase(input.infuse.type.name));
.getInfuseType().equalsIgnoreCase(input.infuse.getType().name));
} else if (in instanceof IntegerInput) {
IntegerInput input = (IntegerInput) in;
return input.ingredient == toMatch.getAmount();
@@ -109,7 +109,7 @@ public class ContainerFactory extends ContainerMekanism<TileEntityFactory> {
return ItemStack.EMPTY;
}
} else if (tileEntity.getRecipeType() == RecipeType.INFUSING && InfuseRegistry.getObject(slotStack) != null
&& (tileEntity.infuseStored.type == null || tileEntity.infuseStored.type == InfuseRegistry
&& (tileEntity.infuseStored.getType() == null || tileEntity.infuseStored.getType() == InfuseRegistry
.getObject(slotStack).type)) {
if (transferExtraSlot(slotID, slotStack)) {
return ItemStack.EMPTY;
@@ -31,8 +31,8 @@ public class ContainerMetallurgicInfuser extends ContainerMekanism<TileEntityMet
stack = slotStack.copy();
if (slotID != 0 && slotID != 1 && slotID != 2 && slotID != 3) {
if (InfuseRegistry.getObject(slotStack) != null && (tileEntity.infuseStored.type == null
|| tileEntity.infuseStored.type == InfuseRegistry.getObject(slotStack).type)) {
if (InfuseRegistry.getObject(slotStack) != null && (tileEntity.infuseStored.getType() == null
|| tileEntity.infuseStored.getType() == InfuseRegistry.getObject(slotStack).type)) {
if (!mergeItemStack(slotStack, 0, 1, false)) {
return ItemStack.EMPTY;
}
@@ -78,7 +78,7 @@ public class ContainerMetallurgicInfuser extends ContainerMekanism<TileEntityMet
}
public boolean isInputItem(ItemStack itemStack) {
if (tileEntity.infuseStored.type != null) {
if (tileEntity.infuseStored.getType() != null) {
return RecipeHandler.getMetallurgicInfuserRecipe(new InfusionInput(tileEntity.infuseStored, itemStack))
!= null;
} else {
@@ -25,7 +25,7 @@ public class InfusionInput extends MachineInput<InfusionInput> {
public ItemStack inputStack = ItemStack.EMPTY;
public InfusionInput(InfuseStorage storage, ItemStack itemStack) {
infuse = new InfuseStorage(storage.type, storage.amount);
infuse = new InfuseStorage(storage.getType(), storage.getAmount());
inputStack = itemStack;
}
@@ -47,12 +47,12 @@ public class InfusionInput extends MachineInput<InfusionInput> {
@Override
public InfusionInput copy() {
return new InfusionInput(infuse.type, infuse.amount, inputStack.copy());
return new InfusionInput(infuse.getType(), infuse.getAmount(), inputStack.copy());
}
@Override
public boolean isValid() {
return infuse.type != null && !inputStack.isEmpty();
return infuse.getType() != null && !inputStack.isEmpty();
}
public boolean use(NonNullList<ItemStack> inventory, int index, InfuseStorage infuseStorage, boolean deplete) {
@@ -70,7 +70,7 @@ public class InfusionInput extends MachineInput<InfusionInput> {
@Override
public int hashIngredients() {
return infuse.type.unlocalizedName.hashCode() << 8 | StackUtils.hashItemStack(inputStack);
return infuse.getType().unlocalizedName.hashCode() << 8 | StackUtils.hashItemStack(inputStack);
}
@Override
@@ -79,7 +79,7 @@ public class InfusionInput extends MachineInput<InfusionInput> {
return !other.isValid();
}
return infuse.type == other.infuse.type && MachineInput.inputItemMatches(inputStack, other.inputStack);
return infuse.getType() == other.infuse.getType() && MachineInput.inputItemMatches(inputStack, other.inputStack);
}
@Override
@@ -342,11 +342,6 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt
}
}
if (infuseStored.amount <= 0) {
infuseStored.amount = 0;
infuseStored.type = null;
}
lastUsage = prev - getEnergy();
prevEnergy = getEnergy();
}
@@ -479,7 +474,7 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt
}*/ else if (cached.recipeInput instanceof InfusionInput) {
InfusionInput infusionInput = (InfusionInput) cached.recipeInput;
recipeInput = infusionInput.inputStack;
secondaryMatch = infuseStored.amount == 0 || infuseStored.type == infusionInput.infuse.type;
secondaryMatch = infuseStored.getAmount() == 0 || infuseStored.getType() == infusionInput.infuse.getType();
}
//If there is no cached item input or it doesn't match our fallback
// then it is an out of date cache so we compare against the new one
@@ -536,13 +531,11 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt
inventory.get(4).shrink(1);
}
} else if (recipeType == RecipeType.INFUSING) {
if (InfuseRegistry.getObject(inventory.get(4)) != null) {
InfuseObject infuse = InfuseRegistry.getObject(inventory.get(4));
if (infuseStored.type == null || infuseStored.type == infuse.type) {
if (infuseStored.amount + infuse.stored <= maxInfuse) {
infuseStored.amount += infuse.stored;
infuseStored.type = infuse.type;
InfuseObject pendingInfusionInput = InfuseRegistry.getObject(inventory.get(4));
if (pendingInfusionInput != null) {
if (infuseStored.getType() == null || infuseStored.getType() == pendingInfusionInput.type) {
if (infuseStored.getAmount() + pendingInfusionInput.stored <= maxInfuse) {
infuseStored.increase(pendingInfusionInput);
inventory.get(4).shrink(1);
}
}
@@ -620,8 +613,8 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt
} else if (recipeType.getFuelType() == MachineFuelType.DOUBLE) {
return recipeType.hasRecipeForExtra(itemstack);
} else if (recipeType == RecipeType.INFUSING) {
return InfuseRegistry.getObject(itemstack) != null && (infuseStored.type == null
|| infuseStored.type == InfuseRegistry.getObject(itemstack).type);
return InfuseRegistry.getObject(itemstack) != null && (infuseStored.getType() == null
|| infuseStored.getType() == InfuseRegistry.getObject(itemstack).type);
}
}
@@ -633,7 +626,7 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt
}
public int getScaledInfuseLevel(int i) {
return infuseStored.amount * i / maxInfuse;
return infuseStored.getAmount() * i / maxInfuse;
}
public int getScaledGasLevel(int i) {
@@ -765,8 +758,7 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt
sorting = !sorting;
} else if (type == 1) {
gasTank.setGas(null);
infuseStored.amount = 0;
infuseStored.type = null;
infuseStored.setEmpty();
}
return;
@@ -782,9 +774,12 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt
sorting = dataStream.readBoolean();
upgraded = dataStream.readBoolean();
lastUsage = dataStream.readDouble();
infuseStored.amount = dataStream.readInt();
if (infuseStored.amount > 0) {
infuseStored.type = InfuseRegistry.get(PacketHandler.readString(dataStream));
int amount = dataStream.readInt();
if (amount > 0) {
infuseStored.setAmount(amount);
infuseStored.setType(InfuseRegistry.get(PacketHandler.readString(dataStream)));
} else {
infuseStored.setEmpty();
}
if (recipeType != oldRecipe) {
@@ -816,8 +811,11 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt
upgradeComponent.setSupported(Upgrade.GAS, recipeType.fuelEnergyUpgrades());
recipeTicks = nbtTags.getInteger("recipeTicks");
sorting = nbtTags.getBoolean("sorting");
infuseStored.amount = nbtTags.getInteger("infuseStored");
infuseStored.type = InfuseRegistry.get(nbtTags.getString("type"));
int amount = nbtTags.getInteger("infuseStored");
if (amount != 0) {
infuseStored.setAmount(amount);
infuseStored.setType(InfuseRegistry.get(nbtTags.getString("type")));
}
for (int i = 0; i < tier.processes; i++) {
progress[i] = nbtTags.getInteger("progress" + i);
@@ -834,10 +832,9 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt
nbtTags.setInteger("recipeType", recipeType.ordinal());
nbtTags.setInteger("recipeTicks", recipeTicks);
nbtTags.setBoolean("sorting", sorting);
nbtTags.setInteger("infuseStored", infuseStored.amount);
if (infuseStored.type != null) {
nbtTags.setString("type", infuseStored.type.name);
if (infuseStored.getType() != null) {
nbtTags.setString("type", infuseStored.getType().name);
nbtTags.setInteger("infuseStored", infuseStored.getAmount());
} else {
nbtTags.setString("type", "null");
}
@@ -861,9 +858,9 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt
data.add(upgraded);
data.add(lastUsage);
data.add(infuseStored.amount);
if (infuseStored.amount > 0) {
data.add(infuseStored.type.name);
data.add(infuseStored.getAmount());
if (infuseStored.getAmount() > 0) {
data.add(infuseStored.getType().name);
}
data.add(progress);
@@ -87,13 +87,11 @@ public class TileEntityMetallurgicInfuser extends TileEntityOperationalMachine i
ChargeUtils.discharge(4, this);
if (!inventory.get(1).isEmpty()) {
if (InfuseRegistry.getObject(inventory.get(1)) != null) {
InfuseObject infuse = InfuseRegistry.getObject(inventory.get(1));
if (infuseStored.type == null || infuseStored.type == infuse.type) {
if (infuseStored.amount + infuse.stored <= MAX_INFUSE) {
infuseStored.amount += infuse.stored;
infuseStored.type = infuse.type;
InfuseObject pendingInfuseInput = InfuseRegistry.getObject(inventory.get(1));
if (pendingInfuseInput != null) {
if (infuseStored.getType() == null || infuseStored.getType() == pendingInfuseInput.type) {
if (infuseStored.getAmount() + pendingInfuseInput.stored <= MAX_INFUSE) {
infuseStored.increase(pendingInfuseInput);
inventory.get(1).shrink(1);
}
}
@@ -122,11 +120,6 @@ public class TileEntityMetallurgicInfuser extends TileEntityOperationalMachine i
operatingTicks = 0;
}
if (infuseStored.amount <= 0) {
infuseStored.amount = 0;
infuseStored.type = null;
}
prevEnergy = getEnergy();
}
}
@@ -207,13 +200,13 @@ public class TileEntityMetallurgicInfuser extends TileEntityOperationalMachine i
if (slotID == 3) {
return false;
} else if (slotID == 1) {
return InfuseRegistry.getObject(itemstack) != null && (infuseStored.type == null
|| infuseStored.type == InfuseRegistry.getObject(itemstack).type);
return InfuseRegistry.getObject(itemstack) != null && (infuseStored.getType() == null
|| infuseStored.getType() == InfuseRegistry.getObject(itemstack).type);
} else if (slotID == 0) {
return itemstack.getItem() == MekanismItems.SpeedUpgrade
|| itemstack.getItem() == MekanismItems.EnergyUpgrade;
} else if (slotID == 2) {
if (infuseStored.type != null) {
if (infuseStored.getType() != null) {
return RecipeHandler.getMetallurgicInfuserRecipe(new InfusionInput(infuseStored, itemstack)) != null;
} else {
return Recipe.METALLURGIC_INFUSER.get().keySet().stream()
@@ -242,15 +235,18 @@ public class TileEntityMetallurgicInfuser extends TileEntityOperationalMachine i
}
public int getScaledInfuseLevel(int i) {
return infuseStored.amount * i / MAX_INFUSE;
return infuseStored.getAmount() * i / MAX_INFUSE;
}
@Override
public void readFromNBT(NBTTagCompound nbtTags) {
super.readFromNBT(nbtTags);
infuseStored.amount = nbtTags.getInteger("infuseStored");
infuseStored.type = InfuseRegistry.get(nbtTags.getString("type"));
int amount = nbtTags.getInteger("infuseStored");
if (amount != 0) {
infuseStored.setAmount(amount);
infuseStored.setType(InfuseRegistry.get(nbtTags.getString("type")));
}
}
@Nonnull
@@ -258,10 +254,9 @@ public class TileEntityMetallurgicInfuser extends TileEntityOperationalMachine i
public NBTTagCompound writeToNBT(NBTTagCompound nbtTags) {
super.writeToNBT(nbtTags);
nbtTags.setInteger("infuseStored", infuseStored.amount);
if (infuseStored.type != null) {
nbtTags.setString("type", infuseStored.type.name);
if (infuseStored.getType() != null) {
nbtTags.setString("type", infuseStored.getType().name);
nbtTags.setInteger("infuseStored", infuseStored.getAmount());
} else {
nbtTags.setString("type", "null");
}
@@ -274,16 +269,19 @@ public class TileEntityMetallurgicInfuser extends TileEntityOperationalMachine i
@Override
public void handlePacketData(ByteBuf dataStream) {
if (FMLCommonHandler.instance().getEffectiveSide().isServer()) {
infuseStored.amount = dataStream.readInt();
infuseStored.setAmount(dataStream.readInt());
return;
}
super.handlePacketData(dataStream);
if (FMLCommonHandler.instance().getEffectiveSide().isClient()) {
infuseStored.amount = dataStream.readInt();
if (infuseStored.amount > 0) {
infuseStored.type = InfuseRegistry.get(PacketHandler.readString(dataStream));
int amount = dataStream.readInt();
if (amount > 0) {
infuseStored.setAmount(amount);
infuseStored.setType(InfuseRegistry.get(PacketHandler.readString(dataStream)));
} else {
infuseStored.setEmpty();
}
}
}
@@ -292,9 +290,9 @@ public class TileEntityMetallurgicInfuser extends TileEntityOperationalMachine i
public TileNetworkList getNetworkedData(TileNetworkList data) {
super.getNetworkedData(data);
data.add(infuseStored.amount);
if (infuseStored.amount > 0) {
data.add(infuseStored.type.name);
data.add(infuseStored.getAmount());
if (infuseStored.getAmount() > 0) {
data.add(infuseStored.getType().name);
}
return data;
@@ -323,7 +321,7 @@ public class TileEntityMetallurgicInfuser extends TileEntityOperationalMachine i
case 6:
return new Object[]{infuseStored};
case 7:
return new Object[]{MAX_INFUSE - infuseStored.amount};
return new Object[]{MAX_INFUSE - infuseStored.getAmount()};
default:
throw new NoSuchMethodException();
}