From 0cd4f49d50c79f365ceaf42b80020de39ba3f452 Mon Sep 17 00:00:00 2001 From: RecursivePineapple Date: Tue, 19 Nov 2024 14:16:59 -0500 Subject: [PATCH] Fixed essentia level maintaining You could previously level maintain the thaumic energistics fake essentia item, but this wouldn't work properly due to the continuous buildup of the fake items. It would work initially, but once you added or removed any essentia, it would not make more. To fix this, this commit checks if the maintained item is an essentia stack and does two things. First, it will convert NEI addon aspects to thaumic energistics. This is because it's very difficult to get the thaumic energistics item (to configure maintainers). I tried to add some logic to AE's SlotFake that converts addon aspects to thaumic energistics aspects, but it broke level maintainers. It added very little, so I removed that. Second, it will check the essentia cache to get the stack size instead of the item cache. --- .../common/tile/TileLevelMaintainer.java | 33 ++++++- .../ThaumicEnergisticsCrafting.java | 97 +++++++++++++++++++ .../glodblock/github/proxy/CommonProxy.java | 4 + 3 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/glodblock/github/crossmod/thaumcraft/ThaumicEnergisticsCrafting.java diff --git a/src/main/java/com/glodblock/github/common/tile/TileLevelMaintainer.java b/src/main/java/com/glodblock/github/common/tile/TileLevelMaintainer.java index fb90d214..943888b4 100644 --- a/src/main/java/com/glodblock/github/common/tile/TileLevelMaintainer.java +++ b/src/main/java/com/glodblock/github/common/tile/TileLevelMaintainer.java @@ -14,6 +14,7 @@ import org.jetbrains.annotations.Nullable; import com.glodblock.github.api.registries.ILevelViewable; import com.glodblock.github.common.Config; import com.glodblock.github.common.item.ItemFluidDrop; +import com.glodblock.github.crossmod.thaumcraft.ThaumicEnergisticsCrafting; import com.glodblock.github.inventory.AeItemStackHandler; import com.glodblock.github.inventory.AeStackInventory; import com.glodblock.github.inventory.AeStackInventoryImpl; @@ -169,19 +170,41 @@ public class TileLevelMaintainer extends AENetworkTile if (!isEnable || batchSize == 0) requests.updateState(i, State.None); if (batchSize > 0) { IAEItemStack craftItem = requests.getCraftItem(i); + + if (ThaumicEnergisticsCrafting.isAspectStack(craftItem.getItemStack())) { + craftItem = ThaumicEnergisticsCrafting.convertAspectStack(craftItem); + } + IAEItemStack aeItem = inv.findPrecise(craftItem); + + long stackSize = aeItem == null ? 0 : aeItem.getStackSize(); + + if (aeItem != null && ThaumicEnergisticsCrafting.isAspectStack(aeItem.getItemStack())) { + stackSize = ThaumicEnergisticsCrafting.getEssentiaAmount(aeItem, grid); + } + boolean isDone = requests.isDone(i); boolean isCraftable = aeItem != null && aeItem.isCraftable(); - boolean shouldCraft = isCraftable && aeItem.getStackSize() < quantity; + boolean shouldCraft = isCraftable && stackSize < quantity; + if (isDone) requests.updateState(i, State.Idle); if (!isCraftable) requests.updateState(i, State.Error); - if (allBusy || !isDone - || !shouldCraft - || craftingGrid.canEmitFor(craftItem) - || craftingGrid.isRequesting(craftItem)) + + if (allBusy || !isDone || !shouldCraft) { continue; + } + + if (craftingGrid.canEmitFor(craftItem)) { + continue; + } + + if (craftingGrid.isRequesting(craftItem)) { + continue; + } + // do crafting Future jobTask = requests.getJob(i); + if (jobTask == null) { if (itemToBegin == null) { itemToBegin = craftItem; diff --git a/src/main/java/com/glodblock/github/crossmod/thaumcraft/ThaumicEnergisticsCrafting.java b/src/main/java/com/glodblock/github/crossmod/thaumcraft/ThaumicEnergisticsCrafting.java new file mode 100644 index 00000000..958705da --- /dev/null +++ b/src/main/java/com/glodblock/github/crossmod/thaumcraft/ThaumicEnergisticsCrafting.java @@ -0,0 +1,97 @@ +package com.glodblock.github.crossmod.thaumcraft; + +import java.util.Objects; + +import javax.annotation.Nullable; + +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; + +import appeng.api.networking.IGrid; +import appeng.api.storage.data.IAEItemStack; +import appeng.util.item.AEItemStack; +import cpw.mods.fml.common.Optional.Method; +import cpw.mods.fml.common.registry.GameRegistry; +import thaumcraft.api.aspects.Aspect; +import thaumicenergistics.api.grid.IEssentiaGrid; +import thaumicenergistics.common.items.ItemCraftingAspect; + +public class ThaumicEnergisticsCrafting { + + public static Item neiAddonAspect, thaumicEnergisticsAspect; + + public static void postInit() { + neiAddonAspect = GameRegistry.findItem("thaumcraftneiplugin", "Aspect"); + thaumicEnergisticsAspect = GameRegistry.findItem("thaumicenergistics", "crafting.aspect"); + } + + /** + * Checks if a stack is an aspect preview (nei addon or thaumic energistics). Does not mean that the stack contains + * an aspect. + */ + public static boolean isAspectStack(ItemStack stack) { + if (stack == null) return false; + + return stack.getItem() == neiAddonAspect || stack.getItem() == thaumicEnergisticsAspect; + } + + @Nullable + @Method(modid = "thaumicenergistics") + public static String getAspectName(ItemStack stack) { + if (stack == null) return null; + + if (stack.getItem() == neiAddonAspect) { + NBTTagCompound tag = stack.getTagCompound(); + if (tag == null || !(tag.getTag("Aspects") instanceof NBTTagList aspects)) return null; + if (aspects.tagCount() != 1) return null; + String aspect = aspects.getCompoundTagAt(0).getString("key"); + if (aspect.isEmpty()) return null; + + return aspect; + } + + if (stack.getItem() == thaumicEnergisticsAspect) { + return ItemCraftingAspect.getAspect(stack).getTag(); + } + + return null; + } + + @Method(modid = "thaumicenergistics") + public static ItemStack getAspectStack(String aspectName, int stackSize) { + return ItemCraftingAspect.createStackForAspect(Aspect.getAspect(aspectName), stackSize); + } + + /** + * Converts an aspect stack into a thaumic energistics stack. + */ + public static IAEItemStack convertAspectStack(IAEItemStack stack) { + if (stack == null) return null; + + String aspect = getAspectName(stack.getItemStack()); + + if (aspect == null) return stack; + + return Objects.requireNonNull(AEItemStack.create(getAspectStack(aspect, 1))).setStackSize(stack.getStackSize()); + } + + /** + * Gets the amount of essentia stored in a grid for a given aspect preview. + */ + @Method(modid = "thaumicenergistics") + public static long getEssentiaAmount(IAEItemStack stack, IGrid grid) { + String aspectName = getAspectName(stack.getItemStack()); + + if (aspectName == null) return 0; + + Aspect aspect = Aspect.getAspect(aspectName); + + if (aspect == null) return 0; + + IEssentiaGrid essentiaGrid = grid.getCache(IEssentiaGrid.class); + + return essentiaGrid.getEssentiaAmount(aspect); + } +} diff --git a/src/main/java/com/glodblock/github/proxy/CommonProxy.java b/src/main/java/com/glodblock/github/proxy/CommonProxy.java index 6de9f5cc..70555ea2 100644 --- a/src/main/java/com/glodblock/github/proxy/CommonProxy.java +++ b/src/main/java/com/glodblock/github/proxy/CommonProxy.java @@ -10,6 +10,7 @@ import com.glodblock.github.common.tile.TileFluidInterface; import com.glodblock.github.common.tile.TileWalrus; import com.glodblock.github.crossmod.extracells.EC2Replacer; import com.glodblock.github.crossmod.thaumcraft.AspectUtil; +import com.glodblock.github.crossmod.thaumcraft.ThaumicEnergisticsCrafting; import com.glodblock.github.inventory.external.AEFluidInterfaceHandler; import com.glodblock.github.loader.ItemAndBlockHolder; import com.glodblock.github.network.SPacketMEUpdateBuffer; @@ -46,6 +47,9 @@ public class CommonProxy { if (!ModAndClassUtil.EC2 && Config.replaceEC2) { EC2Replacer.initReplacer(); } + if (ModAndClassUtil.ThE) { + ThaumicEnergisticsCrafting.postInit(); + } if (ModAndClassUtil.isBigInterface) { Upgrades.PATTERN_CAPACITY.registerItem(new ItemStack(ItemAndBlockHolder.FLUID_INTERFACE), 3); Upgrades.PATTERN_CAPACITY.registerItem(new ItemStack(ItemAndBlockHolder.INTERFACE), 3);