Skip to content

Commit

Permalink
More AE2FC workarounds for PMT mul/replace
Browse files Browse the repository at this point in the history
This should hopefully be fine
  • Loading branch information
NotMyWing committed Oct 19, 2023
1 parent d23cf76 commit a0975ae
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import co.neeve.nae2.common.items.patternmultitool.ObjPatternMultiTool;
import co.neeve.nae2.common.slots.SlotPatternMultiTool;
import co.neeve.nae2.common.slots.SlotPatternMultiToolUpgrade;
import com.glodblock.github.common.item.ItemFluidDrop;
import com.glodblock.github.common.item.ItemFluidEncodedPattern;
import com.google.common.collect.Lists;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.InventoryPlayer;
Expand All @@ -40,6 +42,7 @@
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidUtil;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
Expand Down Expand Up @@ -300,6 +303,9 @@ public void onSlotChange(Slot s) {
continue;
}

var ae2fc = Platform.isModLoaded("ae2fc") && is.getItem() instanceof ItemFluidEncodedPattern;
final String countTag = ae2fc ? "Cnt" : "Count"; // ¯\_(ツ)_/¯

var isCrafting = nbt.getBoolean("crafting");
ValidatonResult result = null;

Expand All @@ -310,6 +316,10 @@ public void onSlotChange(Slot s) {
lists.add(tagIn);
if (!isCrafting) lists.add(tagOut);

var fluidStackIn = FluidUtil.getFluidContained(itemA);
var fluidStackOut = FluidUtil.getFluidContained(itemB);
var fluidReplacement = ae2fc && fluidStackIn != null && fluidStackOut != null;

for (var list : lists) {
var idx = 0;
for (NBTBase tag : list.copy()) {
Expand All @@ -320,11 +330,18 @@ public void onSlotChange(Slot s) {

// If crafting, store validation for later.
if (isCrafting) {
var count = compound.getTag("Count").copy();
var count = compound.getTag(countTag).copy();
var data = itemBData.copy();
data.setTag("Count", count);
data.setTag(countTag, count);
list.set(idx, data);
} else continue;
} else if (fluidReplacement && stack.getItem() instanceof ItemFluidDrop) {
var fluidStack = ItemFluidDrop.getFluidStack(stack);

// This should never be a crafting pattern.
if (fluidStackIn.isFluidEqual(fluidStack)) {
result = ValidatonResult.OK;
}
}
idx++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@
import co.neeve.nae2.common.enums.PatternMultiToolTabs;
import co.neeve.nae2.common.interfaces.IContainerPatternMultiTool;
import co.neeve.nae2.common.interfaces.IPatternMultiToolHost;
import com.glodblock.github.common.item.ItemFluidDrop;
import com.glodblock.github.common.item.ItemFluidEncodedPattern;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.fluids.FluidUtil;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

import java.util.ArrayList;

public class HandlerPatternMultiTool implements IMessageHandler<PatternMultiToolPacket, IMessage> {

// Handles incoming messages from PatternMultiToolPacket
Expand Down Expand Up @@ -94,10 +99,10 @@ private void emptyPattern(ItemStack is, AEBaseContainer container) {
}

// Checks if each tag in the list can be divided evenly by the factor
private boolean canDivideTagList(NBTTagList tagList, int factor) {
private boolean canDivideTagList(NBTTagList tagList, int factor, String countTag) {
for (NBTBase tag : tagList) {
NBTTagCompound ntc = (NBTTagCompound) tag;
if (ntc.getInteger("Count") % factor != 0) {
if (ntc.getInteger(countTag) % factor != 0) {
return true;
}
}
Expand Down Expand Up @@ -126,20 +131,39 @@ private void searchAndReplace(IContainerPatternMultiTool host, EntityPlayerMP pl
continue;
}

var ae2fc = Platform.isModLoaded("ae2fc") && is.getItem() instanceof ItemFluidEncodedPattern;
final String countTag = ae2fc ? "Cnt" : "Count"; // ¯\_(ツ)_/¯

final NBTTagList tagIn = (NBTTagList) nbt.getTag("in").copy();
final NBTTagList tagOut = (NBTTagList) nbt.getTag("out").copy();

var fluidStackIn = FluidUtil.getFluidContained(itemA);
var fluidStackOut = FluidUtil.getFluidContained(itemB);
var fluidReplacement = ae2fc && fluidStackIn != null && fluidStackOut != null;

var lists = new NBTTagList[]{ tagIn, tagOut };
for (var list : lists) {
var idx = 0;
for (NBTBase tag : list.copy()) {
NBTTagCompound compound = (NBTTagCompound) tag;
var stack = ItemStackHelper.stackFromNBT(compound);
if (itemA.isItemEqual(stack)) {
var count = compound.getTag("Count").copy();
var count = compound.getTag(countTag).copy();
var data = itemBData.copy();
data.setTag("Count", count);
data.setTag(countTag, count);
list.set(idx, data);
} else if (fluidReplacement && stack.getItem() instanceof ItemFluidDrop) {
// ¯\_(ツ)_/¯
var fluidStack = ItemFluidDrop.getFluidStack(stack);
if (fluidStackIn.isFluidEqual(fluidStack)) {
var ifd = ItemFluidDrop.newStack(fluidStackOut);
NBTTagCompound ifdCompound;
if (ifd == null || (ifdCompound = ifd.getTagCompound()) == null) continue;

var data = compound.copy();
data.setTag("tag", ifdCompound);
list.set(idx, data);
}
}
idx++;
}
Expand Down Expand Up @@ -169,11 +193,19 @@ private void searchAndReplace(IContainerPatternMultiTool host, EntityPlayerMP pl

nbt.setTag("in", tagIn);
nbt.setTag("out", tagOut);

// ¯\_(ツ)_/¯
if (ae2fc) {
nbt.setTag("Inputs", tagIn);
nbt.setTag("Outputs", tagOut);
}
}
}

// Updates the count of a pattern based on the operation and factor
private void updatePatternCount(ItemStack is, int factor, Operation operation) {
var ae2fc = Platform.isModLoaded("ae2fc") && is.getItem() instanceof ItemFluidEncodedPattern;

NBTTagCompound nbt = is.getTagCompound();
if (nbt == null) {
// Skip this item if it has no NBT data
Expand All @@ -183,53 +215,75 @@ private void updatePatternCount(ItemStack is, int factor, Operation operation) {
final NBTTagList tagIn = (NBTTagList) nbt.getTag("in");
final NBTTagList tagOut = (NBTTagList) nbt.getTag("out");

final String countTag = ae2fc ? "Cnt" : "Count"; // ¯\_(ツ)_/¯

// If operation is DIVIDE, check if all counts are divisible by the factor
if (operation == Operation.DIVIDE && (canDivideTagList(tagIn, factor) || canDivideTagList(tagOut, factor))) {
if (operation == Operation.DIVIDE &&
(canDivideTagList(tagIn, factor, countTag) || canDivideTagList(tagOut, factor, countTag))) {
// If any count is not divisible by the factor, don't modify the pattern
return;
}

modifyTagList(tagIn, factor, operation);
modifyTagList(tagOut, factor, operation);
var toModify = new ArrayList<NBTTagList>(4);

// I don't know why AE2FC keeps a different set of tags.
if (ae2fc) {
final NBTTagList ae2fcTagIn = (NBTTagList) nbt.getTag("Inputs");
final NBTTagList ae2fcTagOut = (NBTTagList) nbt.getTag("Outputs");
if (operation == Operation.DIVIDE &&
(canDivideTagList(ae2fcTagIn, factor, countTag) || canDivideTagList(ae2fcTagOut, factor, countTag))) {
return;
}

toModify.add(ae2fcTagIn);
toModify.add(ae2fcTagOut);
}

toModify.add(tagIn);
toModify.add(tagOut);

for (var list : toModify) {
modifyTagList(list, factor, operation, countTag);
}

NBTTagCompound newNbt = is.getTagCompound();
newNbt.setByte("crafting", (byte) 0);
newNbt.setByte("substitute", (byte) 0);
}

// Modifies the count of each tag in the list based on the operation and factor
private void modifyTagList(NBTTagList tagList, int factor, Operation operation) {
private void modifyTagList(NBTTagList tagList, int factor, Operation operation, String countTag) {
for (NBTBase tag : tagList) {
NBTTagCompound ntc = (NBTTagCompound) tag;
int count = ntc.getInteger("Count");
int count = ntc.getInteger(countTag);
if (count == 0) {
continue;
}

switch (operation) {
case ADD -> {
if (count < Integer.MAX_VALUE) {
ntc.setInteger("Count", count + factor);
ntc.setInteger(countTag, count + factor);
}
}
case SUBTRACT -> {
if (count > 1) {
ntc.setInteger("Count", count - factor);
ntc.setInteger(countTag, count - factor);
}
}
case MULTIPLY -> {
if (count > 0 && count <= Integer.MAX_VALUE / factor) {
ntc.setInteger("Count", count * factor);
ntc.setInteger(countTag, count * factor);
}
}
case DIVIDE -> {
if (count >= factor) {
ntc.setInteger("Count", Math.max(1, count / factor));
ntc.setInteger(countTag, Math.max(1, count / factor));
}
}
}
if (count > 64) {
ntc.setInteger("stackSize", ntc.getInteger("Count"));
ntc.setInteger("stackSize", ntc.getInteger(countTag));
} else {
ntc.removeTag("stackSize");
}
Expand Down

0 comments on commit a0975ae

Please sign in to comment.