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 6637e4658..081b1a946 100644 --- a/src/main/java/com/glodblock/github/common/tile/TileLevelMaintainer.java +++ b/src/main/java/com/glodblock/github/common/tile/TileLevelMaintainer.java @@ -14,9 +14,11 @@ 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; +import com.glodblock.github.util.ModAndClassUtil; import com.google.common.collect.ImmutableSet; import appeng.api.AEApi; @@ -169,19 +171,45 @@ private TickRateModulation doWork() { if (!isEnable || batchSize == 0) requests.updateState(i, State.None); if (batchSize > 0) { IAEItemStack craftItem = requests.getCraftItem(i); + + 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 (ModAndClassUtil.ThE) { + 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..7e2510d94 --- /dev/null +++ b/src/main/java/com/glodblock/github/crossmod/thaumcraft/ThaumicEnergisticsCrafting.java @@ -0,0 +1,111 @@ +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 com.glodblock.github.util.ModAndClassUtil; + +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 (!ModAndClassUtil.ThE || stack == null) return false; + + return stack.getItem() == neiAddonAspect || stack.getItem() == thaumicEnergisticsAspect; + } + + @Method(modid = "thaumicenergistics") + private static @Nullable Aspect getAspect(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.getAspect(aspect); + } + + if (stack.getItem() == thaumicEnergisticsAspect) { + return Aspect.getAspect(ItemCraftingAspect.getAspect(stack).getTag()); + } + + return null; + } + + @Method(modid = "thaumicenergistics") + 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; + + Aspect aspect = getAspect(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. + */ + public static long getEssentiaAmount(IAEItemStack stack, IGrid grid) { + if (ModAndClassUtil.ThE) { + return getEssentiaAmountImpl(stack, grid); + } else { + return 0; + } + } + + @Method(modid = "thaumicenergistics") + private static long getEssentiaAmountImpl(IAEItemStack stack, IGrid grid) { + Aspect aspect = getAspect(stack.getItemStack()); + + 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 8366fc3c9..39eb7864b 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);