From 0cd4f49d50c79f365ceaf42b80020de39ba3f452 Mon Sep 17 00:00:00 2001 From: RecursivePineapple Date: Tue, 19 Nov 2024 14:16:59 -0500 Subject: [PATCH 1/3] 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 fb90d2148..943888b45 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 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 @@ private TickRateModulation doWork() { 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 000000000..958705da8 --- /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 6de9f5ccf..70555ea2f 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.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 void postInit(FMLPostInitializationEvent event) { 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); From 40a086ce169ff2b2d43fc176e3f46b963695a987 Mon Sep 17 00:00:00 2001 From: lailani-f <41488821+lailani-f@users.noreply.github.com> Date: Sun, 12 Jan 2025 03:06:16 +0100 Subject: [PATCH 2/3] Fix NPE caused by Level Maintainer (#259) --- .../com/glodblock/github/common/tile/TileLevelMaintainer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 2de1d1649..d61958dcb 100644 --- a/src/main/java/com/glodblock/github/common/tile/TileLevelMaintainer.java +++ b/src/main/java/com/glodblock/github/common/tile/TileLevelMaintainer.java @@ -171,7 +171,7 @@ private TickRateModulation doWork() { if (batchSize > 0) { IAEItemStack craftItem = requests.getCraftItem(i); - if (ThaumicEnergisticsCrafting.isAspectStack(craftItem.getItemStack())) { + if (craftItem != null && ThaumicEnergisticsCrafting.isAspectStack(craftItem.getItemStack())) { craftItem = ThaumicEnergisticsCrafting.convertAspectStack(craftItem); } From 9ec4d6b65ec47594f1a26d7df158ff13665a0173 Mon Sep 17 00:00:00 2001 From: RecursivePineapple Date: Mon, 13 Jan 2025 18:11:36 -0500 Subject: [PATCH 3/3] cleaned up implicit mod deps --- .../common/tile/TileLevelMaintainer.java | 13 ++++-- .../ThaumicEnergisticsCrafting.java | 40 +++++++++++++------ 2 files changed, 36 insertions(+), 17 deletions(-) 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 d61958dcb..081b1a946 100644 --- a/src/main/java/com/glodblock/github/common/tile/TileLevelMaintainer.java +++ b/src/main/java/com/glodblock/github/common/tile/TileLevelMaintainer.java @@ -18,6 +18,7 @@ import com.glodblock.github.inventory.AeItemStackHandler; import com.glodblock.github.inventory.AeStackInventory; import com.glodblock.github.inventory.AeStackInventoryImpl; +import com.glodblock.github.util.ModAndClassUtil; import com.google.common.collect.ImmutableSet; import appeng.api.AEApi; @@ -171,16 +172,20 @@ private TickRateModulation doWork() { if (batchSize > 0) { IAEItemStack craftItem = requests.getCraftItem(i); - if (craftItem != null && ThaumicEnergisticsCrafting.isAspectStack(craftItem.getItemStack())) { - craftItem = ThaumicEnergisticsCrafting.convertAspectStack(craftItem); + if (ModAndClassUtil.ThE) { + if (craftItem != null && 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); + if (ModAndClassUtil.ThE) { + if (aeItem != null && ThaumicEnergisticsCrafting.isAspectStack(aeItem.getItemStack())) { + stackSize = ThaumicEnergisticsCrafting.getEssentiaAmount(aeItem, grid); + } } boolean isDone = requests.isDone(i); diff --git a/src/main/java/com/glodblock/github/crossmod/thaumcraft/ThaumicEnergisticsCrafting.java b/src/main/java/com/glodblock/github/crossmod/thaumcraft/ThaumicEnergisticsCrafting.java index 958705da8..7e2510d94 100644 --- a/src/main/java/com/glodblock/github/crossmod/thaumcraft/ThaumicEnergisticsCrafting.java +++ b/src/main/java/com/glodblock/github/crossmod/thaumcraft/ThaumicEnergisticsCrafting.java @@ -9,6 +9,8 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; +import com.glodblock.github.util.ModAndClassUtil; + import appeng.api.networking.IGrid; import appeng.api.storage.data.IAEItemStack; import appeng.util.item.AEItemStack; @@ -32,14 +34,13 @@ public static void postInit() { * an aspect. */ public static boolean isAspectStack(ItemStack stack) { - if (stack == null) return false; + if (!ModAndClassUtil.ThE || stack == null) return false; return stack.getItem() == neiAddonAspect || stack.getItem() == thaumicEnergisticsAspect; } - @Nullable @Method(modid = "thaumicenergistics") - public static String getAspectName(ItemStack stack) { + private static @Nullable Aspect getAspect(ItemStack stack) { if (stack == null) return null; if (stack.getItem() == neiAddonAspect) { @@ -49,28 +50,37 @@ public static String getAspectName(ItemStack stack) { String aspect = aspects.getCompoundTagAt(0).getString("key"); if (aspect.isEmpty()) return null; - return aspect; + return Aspect.getAspect(aspect); } if (stack.getItem() == thaumicEnergisticsAspect) { - return ItemCraftingAspect.getAspect(stack).getTag(); + return Aspect.getAspect(ItemCraftingAspect.getAspect(stack).getTag()); } return null; } @Method(modid = "thaumicenergistics") - public static ItemStack getAspectStack(String aspectName, int stackSize) { - return ItemCraftingAspect.createStackForAspect(Aspect.getAspect(aspectName), stackSize); + private static ItemStack getAspectStack(Aspect aspect, int stackSize) { + return ItemCraftingAspect.createStackForAspect(aspect, stackSize); } /** * Converts an aspect stack into a thaumic energistics stack. */ public static IAEItemStack convertAspectStack(IAEItemStack stack) { + if (ModAndClassUtil.ThE) { + return convertAspectStackImpl(stack); + } else { + return stack; + } + } + + @Method(modid = "thaumicenergistics") + private static IAEItemStack convertAspectStackImpl(IAEItemStack stack) { if (stack == null) return null; - String aspect = getAspectName(stack.getItemStack()); + Aspect aspect = getAspect(stack.getItemStack()); if (aspect == null) return stack; @@ -80,13 +90,17 @@ public static IAEItemStack convertAspectStack(IAEItemStack stack) { /** * 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; + if (ModAndClassUtil.ThE) { + return getEssentiaAmountImpl(stack, grid); + } else { + return 0; + } + } - Aspect aspect = Aspect.getAspect(aspectName); + @Method(modid = "thaumicenergistics") + private static long getEssentiaAmountImpl(IAEItemStack stack, IGrid grid) { + Aspect aspect = getAspect(stack.getItemStack()); if (aspect == null) return 0;