Add automation to EIO Enchanter (basically a copy-paste from my other two PR's) (#227)

Co-authored-by: koolkrafter5 <[email protected]>
Co-authored-by: Martin Robertz <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-06-12 15:45:50 +02:00
1 parent ed24e1e1f0
commit 7a61bb9dee
6 files changed
+178 -17

No files matched your search

@@ -215,6 +215,7 @@ public class EnderIO {
public static final boolean hasGT5 = Loader.isModLoaded("gregtech") && !Loader.isModLoaded("gregapi");
public static final boolean hasHP = Loader.isModLoaded("hodgepodge");
public static final boolean hasWailaPlugins = Loader.isModLoaded("wailaplugins");
public static final boolean hasAutomagy = Loader.isModLoaded("Automagy");
// Materials
public static ItemCapacitor itemBasicCapacitor;
@@ -5,6 +5,7 @@ import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
@@ -140,4 +141,25 @@ public class BlockEnchanter extends BlockEio implements IGuiHandler, IResourceTo
public String getUnlocalizedNameForTooltip(ItemStack itemStack) {
return getUnlocalizedName();
}
@Override
public boolean hasComparatorInputOverride() {
return true;
}
// 0, 1, 2, 3, 4, 5, 8, 12, 20, 24, 32, 36, 48, 60, 64 are 0-15 respectively
// the power values are all possible values of the second slot to make automation possible
// (it has to be able to differentiate from them all)
@Override
public int getComparatorInputOverride(World worldIn, int x, int y, int z, int side) {
if (worldIn.getTileEntity(x, y, z) instanceof IInventory inv) {
ItemStack stuff = inv.getStackInSlot(1);
int size;
return stuff == null ? 0
: (size = stuff.stackSize) < 6 ? size
: size < 40 ? size / 4 + (size < 28 ? 4 : 3)
: size < 48 ? 12 : size < 60 ? 13 : size < 64 ? 14 : 15;
}
return 0;
}
}
@@ -27,11 +27,6 @@ public class ContainerEnchanter extends ContainerEnderTileEntity<TileEnchanter>
addSlotToContainer(new Slot(te, 0, 27, 35) {
@Override
public int getSlotStackLimit() {
return 1;
}
@Override
public boolean isItemValid(ItemStack itemStack) {
return te.isItemValidForSlot(0, itemStack);
@@ -58,6 +53,11 @@ public class ContainerEnchanter extends ContainerEnderTileEntity<TileEnchanter>
addSlotToContainer(new Slot(te, 2, 134, 35) {
@Override
public ItemStack decrStackSize(int amt) {
return te.decrStackSize(2, amt, false);
}
@Override
public int getSlotStackLimit() {
return 1;
@@ -81,18 +81,19 @@ public class ContainerEnchanter extends ContainerEnderTileEntity<TileEnchanter>
|| enchData.enchantmentLevel >= curStack.stackSize) {
te.setInventorySlotContents(1, (ItemStack) null);
} else {
curStack = curStack.copy();
curStack.stackSize -= recipe.getItemsPerLevel() * enchData.enchantmentLevel;
if (curStack.stackSize > 0) {
te.setInventorySlotContents(1, curStack);
} else {
if (curStack.stackSize <= 0) {
te.setInventorySlotContents(1, null);
}
te.markDirty();
}
te.setInventorySlotContents(0, (ItemStack) null);
curStack = te.getStackInSlot(0);
if (curStack == null || curStack.stackSize <= 1) te.setInventorySlotContents(0, null);
else {
curStack.stackSize -= 1;
te.setInventorySlotContents(0, curStack);
}
if (!te.getWorldObj().isRemote) {
te.getWorldObj().playAuxSFX(1021, te.xCoord, te.yCoord, te.zCoord, 0);
}
@@ -1,5 +1,9 @@
package crazypants.enderio.machine.enchanter;
import static crazypants.enderio.EnderIO.hasAutomagy;
import java.util.ArrayList;
import net.minecraft.enchantment.EnchantmentData;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
@@ -9,13 +13,20 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.ForgeDirection;
import com.gtnewhorizon.gtnhlib.geometry.CubeIterator;
import crazypants.enderio.ModObject;
import crazypants.enderio.TileEntityEio;
import crazypants.enderio.config.Config;
import crazypants.enderio.machine.obelisk.xp.TileExperienceObelisk;
import crazypants.enderio.xp.ExperienceContainer;
import crazypants.enderio.xp.XpUtil;
import tuhljin.automagy.tiles.TileEntityJarXP;
public class TileEnchanter extends TileEntityEio implements ISidedInventory {
private final ItemStack[] inv = new ItemStack[3];
private byte[] stacksizes = new byte[2];
private short facing = (short) ForgeDirection.NORTH.ordinal();
@@ -27,6 +38,19 @@ public class TileEnchanter extends TileEntityEio implements ISidedInventory {
return facing;
}
// The *ACTUAL* updateEntity and canUpdate are final in TileEntityEnder. Great.
@Override
protected void doUpdate() {
if (!shouldUpdate_()) return;
if (inv[0].stackSize != stacksizes[0] || inv[1].stackSize != stacksizes[1]) updateOut();
}
// @Override
public boolean shouldUpdate_() {
return inv[0] != null && inv[1] != null;
}
@Override
protected void writeCustomNBT(NBTTagCompound root) {
NBTTagList itemList = new NBTTagList();
@@ -39,6 +63,7 @@ public class TileEnchanter extends TileEntityEio implements ISidedInventory {
}
}
root.setTag("Items", itemList);
root.setByteArray("SizeCache", stacksizes);
root.setShort("facing", facing);
}
@@ -54,6 +79,8 @@ public class TileEnchanter extends TileEntityEio implements ISidedInventory {
}
}
}
stacksizes = root.getByteArray("SizeCache");
if (stacksizes == null || stacksizes.length < 2) stacksizes = new byte[2];
facing = root.getShort("facing");
}
@@ -77,6 +104,10 @@ public class TileEnchanter extends TileEntityEio implements ISidedInventory {
@Override
public ItemStack decrStackSize(int slot, int amount) {
return decrStackSize(slot, amount, true);
}
public ItemStack decrStackSize(int slot, int amount, boolean auto) {
if (amount <= 0 || slot < 0 || slot >= inv.length || inv[slot] == null) {
return null;
}
@@ -84,8 +115,10 @@ public class TileEnchanter extends TileEntityEio implements ISidedInventory {
if (fromStack == null) {
return null;
}
if (slot == 2 && auto && checkAndDrainXP(Math.min(amount, fromStack.stackSize))) return null;
if (fromStack.stackSize <= amount) {
inv[slot] = null;
updateOut();
return fromStack;
}
ItemStack result = new ItemStack(fromStack.getItem(), amount, fromStack.getItemDamage());
@@ -93,9 +126,95 @@ public class TileEnchanter extends TileEntityEio implements ISidedInventory {
result.stackTagCompound = (NBTTagCompound) fromStack.stackTagCompound.copy();
}
fromStack.stackSize -= amount;
updateOut();
return result;
}
// part of the next method
public boolean absorbXP(int amt) {
if (inv[2] == null || amt <= 0 || inv[2].stackSize < amt) return true;
int LV = getCurrentEnchantmentCost();
if (LV == 0) return false;
int xpCost = XpUtil.getExperienceForLevel(LV * amt);
int xp;
absorb: {
ArrayList<ExperienceContainer> obelisksToEmpty = new ArrayList<>();
if (true) {
CubeIterator iter = new CubeIterator(8);
while (iter.hasNext()) {
iter.next();
if (worldObj.getTileEntity(
iter.n + xCoord,
iter.l + yCoord,
iter.m + zCoord) instanceof TileExperienceObelisk obelisk) {
ExperienceContainer cont = obelisk.getContainer();
xp = cont.getExperienceTotal();
if (xp >= xpCost) {
if (!worldObj.isRemote) {
obelisksToEmpty.forEach(o -> o.drain(null, Integer.MAX_VALUE, true));
cont.drain(null, Integer.MAX_VALUE, true);
cont.addExperience(Math.max(0, xp - xpCost));
}
return false;
}
xpCost -= xp;
obelisksToEmpty.add(cont);
}
}
}
if (hasAutomagy) {
ArrayList<TileEntityJarXP> jarsToEmpty = new ArrayList<>();
CubeIterator iter = new CubeIterator(8);
while (iter.hasNext()) {
iter.next();
if (worldObj.getTileEntity(
iter.n + xCoord,
iter.l + yCoord,
iter.m + zCoord) instanceof TileEntityJarXP jar) {
xp = jar.getXP();
if (xp >= xpCost) {
if (!worldObj.isRemote) {
obelisksToEmpty.forEach(o -> o.drain(null, Integer.MAX_VALUE, true));
jarsToEmpty.forEach(j -> j.setXP(0));
jar.setXP(xp - xpCost);
}
return false;
}
xpCost -= xp;
jarsToEmpty.add(jar);
}
}
}
return true;
}
}
// checks AND drains XP; returns true if xp is NOT sufficient
// also removes the items from the other two slots when automation does the recipe
public boolean checkAndDrainXP(int amt) {
if (absorbXP(amt)) return true;
EnchantmentData enchData = getCurrentEnchantmentData();
EnchanterRecipe recipe = getCurrentEnchantmentRecipe();
ItemStack curStack = inv[1];
if (recipe == null || enchData == null || curStack == null || enchData.enchantmentLevel >= curStack.stackSize) {
inv[1] = null;
} else {
curStack = curStack.copy();
curStack.stackSize -= recipe.getItemsPerLevel() * enchData.enchantmentLevel;
inv[1] = curStack.stackSize > 0 ? curStack : null;
markDirty();
}
curStack = inv[0];
if (curStack == null || curStack.stackSize <= 1) inv[0] = null;
else inv[0].stackSize -= 1;
if (!worldObj.isRemote) {
worldObj.playSoundEffect(xCoord + 0.5d, yCoord + 0.5d, zCoord + 0.5d, "random.anvil_land", 0.2f, 1.0f);
}
return false;
}
@Override
public ItemStack getStackInSlotOnClosing(int p_70304_1_) {
return null;
@@ -111,6 +230,20 @@ public class TileEnchanter extends TileEntityEio implements ISidedInventory {
if (contents != null && contents.stackSize > getInventoryStackLimit()) {
contents.stackSize = getInventoryStackLimit();
}
updateOut();
}
public void updateOut() {
ItemStack output = null;
EnchantmentData enchantment = getCurrentEnchantmentData();
if (enchantment != null) {
output = new ItemStack(Items.enchanted_book);
Items.enchanted_book.addEnchantment(output, enchantment);
}
setOutput(output);
if (inv[0] != null) stacksizes[0] = (byte) inv[0].stackSize;
if (inv[1] != null) stacksizes[1] = (byte) inv[1].stackSize;
}
@Override
@@ -226,7 +359,7 @@ public class TileEnchanter extends TileEntityEio implements ISidedInventory {
return getEnchantmentCost(getCurrentEnchantmentRecipe());
}
private int getEnchantmentCost(EnchanterRecipe currentEnchantment) {
public int getEnchantmentCost(EnchanterRecipe currentEnchantment) {
ItemStack item = inv[1];
if (item == null) {
return 0;
@@ -256,17 +389,17 @@ public class TileEnchanter extends TileEntityEio implements ISidedInventory {
}
@Override
public int[] getAccessibleSlotsFromSide(int p_94128_1_) {
return new int[0];
public int[] getAccessibleSlotsFromSide(int side) {
return side == 0 ? new int[] { 2 } : new int[] { 0, 1 };
}
@Override
public boolean canInsertItem(int p_102007_1_, ItemStack p_102007_2_, int p_102007_3_) {
return false;
return true;
}
@Override
public boolean canExtractItem(int p_102008_1_, ItemStack p_102008_2_, int p_102008_3_) {
return false;
return true;
}
}