From be93b9cc8c1bf2abb5d808dff69d29bdf561a53a Mon Sep 17 00:00:00 2001 From: Voxelstice <95834462+Voxelstice@users.noreply.github.com> Date: Fri, 9 Jan 2026 23:01:38 +0300 Subject: [PATCH] waiter! waiter! more lead acid batteries please! adds OC compat to new battery blocks --- .../storage/TileEntityBatteryBase.java | 91 ++++++++++++++--- .../storage/TileEntityBatteryREDD.java | 84 ++++++++++++---- .../storage/TileEntityBatterySocket.java | 97 +++++++++++++++---- .../storage/TileEntityMachineBattery.java | 14 +-- 4 files changed, 226 insertions(+), 60 deletions(-) diff --git a/src/main/java/com/hbm/tileentity/machine/storage/TileEntityBatteryBase.java b/src/main/java/com/hbm/tileentity/machine/storage/TileEntityBatteryBase.java index 34976ac9d..27e1bb726 100644 --- a/src/main/java/com/hbm/tileentity/machine/storage/TileEntityBatteryBase.java +++ b/src/main/java/com/hbm/tileentity/machine/storage/TileEntityBatteryBase.java @@ -1,5 +1,6 @@ package com.hbm.tileentity.machine.storage; +import com.hbm.handler.CompatHandler; import com.hbm.interfaces.IControlReceiver; import com.hbm.tileentity.IGUIProvider; import com.hbm.tileentity.TileEntityMachineBase; @@ -14,19 +15,25 @@ import api.hbm.energymk2.IEnergyProviderMK2; import api.hbm.energymk2.IEnergyReceiverMK2; import api.hbm.energymk2.Nodespace; import api.hbm.energymk2.Nodespace.PowerNode; +import cpw.mods.fml.common.Optional; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import io.netty.buffer.ByteBuf; +import li.cil.oc.api.machine.Arguments; +import li.cil.oc.api.machine.Callback; +import li.cil.oc.api.machine.Context; +import li.cil.oc.api.network.SimpleComponent; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.MathHelper; -public abstract class TileEntityBatteryBase extends TileEntityMachineBase implements IEnergyConductorMK2, IEnergyProviderMK2, IEnergyReceiverMK2, IControlReceiver, IGUIProvider { +@Optional.InterfaceList({@Optional.Interface(iface = "li.cil.oc.api.network.SimpleComponent", modid = "opencomputers")}) +public abstract class TileEntityBatteryBase extends TileEntityMachineBase implements IEnergyConductorMK2, IEnergyProviderMK2, IEnergyReceiverMK2, IControlReceiver, IGUIProvider, SimpleComponent, CompatHandler.OCComponent { public byte lastRedstone = 0; public long prevPowerState = 0; - + public static final int mode_input = 0; public static final int mode_buffer = 1; public static final int mode_output = 2; @@ -36,20 +43,20 @@ public abstract class TileEntityBatteryBase extends TileEntityMachineBase implem public ConnectionPriority priority = ConnectionPriority.LOW; protected PowerNode node; - + public TileEntityBatteryBase(int slotCount) { super(slotCount); } @Override public void updateEntity() { - + if(!worldObj.isRemote) { if(priority == null || priority.ordinal() == 0 || priority.ordinal() == 4) { priority = ConnectionPriority.LOW; } - + if(this.node == null || this.node.expired) { this.node = (PowerNode) UniNodespace.getNode(worldObj, xCoord, yCoord, zCoord, Nodespace.THE_POWER_PROVIDER); @@ -58,20 +65,20 @@ public abstract class TileEntityBatteryBase extends TileEntityMachineBase implem UniNodespace.createNode(worldObj, this.node); } } - + if(this.node != null && this.node.hasValidNet()) switch(this.getRelevantMode(false)) { case mode_input: this.node.net.removeProvider(this); this.node.net.addReceiver(this); break; case mode_output: this.node.net.addProvider(this); this.node.net.removeReceiver(this); break; case mode_buffer: this.node.net.addProvider(this); this.node.net.addReceiver(this); break; case mode_none: this.node.net.removeProvider(this); this.node.net.removeReceiver(this); break; } - + byte comp = this.getComparatorPower(); if(comp != this.lastRedstone) this.markDirty(); this.lastRedstone = comp; prevPowerState = this.getPower(); - + this.networkPackNT(100); } } @@ -80,7 +87,7 @@ public abstract class TileEntityBatteryBase extends TileEntityMachineBase implem double frac = (double) this.getPower() / (double) this.getMaxPower() * 15D; return (byte) (MathHelper.clamp_int((int) frac + 1, 0, 15)); //to combat eventual rounding errors with the FEnSU's stupid maxPower } - + @Override public PowerNode createNode() { return new PowerNode(this.getPortPos()).setConnections(this.getConPos()); @@ -105,7 +112,7 @@ public abstract class TileEntityBatteryBase extends TileEntityMachineBase implem @Override public void serialize(ByteBuf buf) { super.serialize(buf); - + buf.writeShort(redLow); buf.writeShort(redHigh); buf.writeByte(priority.ordinal()); @@ -147,7 +154,7 @@ public abstract class TileEntityBatteryBase extends TileEntityMachineBase implem public abstract DirPos[] getConPos(); private short modeCache = 0; - + public short getRelevantMode(boolean useCache) { if(useCache) return this.modeCache; boolean powered = false; @@ -155,7 +162,7 @@ public abstract class TileEntityBatteryBase extends TileEntityMachineBase implem this.modeCache = powered ? this.redHigh : this.redLow; return this.modeCache; } - + @Override public boolean hasPermission(EntityPlayer player) { return this.isUseableByPlayer(player); } @Override @@ -175,10 +182,68 @@ public abstract class TileEntityBatteryBase extends TileEntityMachineBase implem this.priority = EnumUtil.grabEnumSafely(ConnectionPriority.class, ordinal); } } - + @Override @SideOnly(Side.CLIENT) public double getMaxRenderDistanceSquared() { return 65536.0D; } + + // do some opencomputer stuff + @Override + @Optional.Method(modid = "OpenComputers") + public String getComponentName() { + return "ntm_energy_storage"; + } + + @Callback(direct = true) + @Optional.Method(modid = "OpenComputers") + public Object[] getEnergyInfo(Context context, Arguments args) { + return new Object[] {getPower(), getMaxPower()}; + } + + @Callback(direct = true) + @Optional.Method(modid = "OpenComputers") + public Object[] getModeInfo(Context context, Arguments args) { + return new Object[] {redLow, redHigh, getPriority().ordinal()}; + } + @Callback(direct = true) + @Optional.Method(modid = "OpenComputers") + public Object[] setModeLow(Context context, Arguments args) { + short newMode = (short) args.checkInteger(0); + if (newMode >= mode_input && newMode <= mode_none) { + redLow = newMode; + return new Object[] {}; + } else { + return new Object[] {"Invalid mode"}; + } + } + @Callback(direct = true) + @Optional.Method(modid = "OpenComputers") + public Object[] setModeHigh(Context context, Arguments args) { + short newMode = (short) args.checkInteger(0); + if (newMode >= mode_input && newMode <= mode_none) { + redHigh = newMode; + return new Object[] {}; + } else { + return new Object[] {"Invalid mode"}; + } + } + @Callback(direct = true) + @Optional.Method(modid = "OpenComputers") + public Object[] setPriority(Context context, Arguments args) { + int newPriority = args.checkInteger(0); + if (newPriority >= 0 && newPriority <= 2) { + priority = EnumUtil.grabEnumSafely(ConnectionPriority.class, newPriority+1); + return new Object[] {}; + } else { + return new Object[] {"Invalid mode"}; + } + } + + @Callback(direct = true) + @Optional.Method(modid = "OpenComputers") + public Object[] getInfo(Context context, Arguments args) { + return new Object[] {getPower(), getMaxPower(), redLow, redHigh, getPriority().ordinal()}; + } } diff --git a/src/main/java/com/hbm/tileentity/machine/storage/TileEntityBatteryREDD.java b/src/main/java/com/hbm/tileentity/machine/storage/TileEntityBatteryREDD.java index 13f07574f..e5293088f 100644 --- a/src/main/java/com/hbm/tileentity/machine/storage/TileEntityBatteryREDD.java +++ b/src/main/java/com/hbm/tileentity/machine/storage/TileEntityBatteryREDD.java @@ -10,7 +10,11 @@ import com.hbm.sound.AudioWrapper; import com.hbm.util.fauxpointtwelve.BlockPos; import com.hbm.util.fauxpointtwelve.DirPos; +import cpw.mods.fml.common.Optional; import io.netty.buffer.ByteBuf; +import li.cil.oc.api.machine.Arguments; +import li.cil.oc.api.machine.Callback; +import li.cil.oc.api.machine.Context; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.nbt.NBTTagCompound; @@ -25,28 +29,28 @@ public class TileEntityBatteryREDD extends TileEntityBatteryBase { public BigInteger[] log = new BigInteger[20]; public BigInteger delta = BigInteger.valueOf(0); - + public BigInteger power = BigInteger.valueOf(0); - + private AudioWrapper audio; public TileEntityBatteryREDD() { super(2); } - + @Override public String getName() { return "container.batteryREDD"; } @Override public void updateEntity() { BigInteger prevPower = new BigInteger(power.toByteArray()); - + super.updateEntity(); - + if(!worldObj.isRemote) { - + long toAdd = Library.chargeTEFromItems(slots, 0, 0, this.getMaxPower()); if(toAdd > 0) this.power = this.power.add(BigInteger.valueOf(toAdd)); - + long toRemove = this.getPower() - Library.chargeItemsFromTE(slots, 1, this.getPower(), this.getMaxPower()); if(toRemove > 0)this.power = this.power.subtract(BigInteger.valueOf(toRemove)); @@ -59,16 +63,16 @@ public class TileEntityBatteryREDD extends TileEntityBatteryBase { } this.log[19] = avg; - + } else { this.prevRotation = this.rotation; this.rotation += this.getSpeed(); - + if(rotation >= 360) { rotation -= 360; prevRotation -= 360; } - + float pitch = 0.5F + this.getSpeed() / 15F * 1.5F; if(this.prevRotation != this.rotation && MainRegistry.proxy.me().getDistanceSq(xCoord + 0.5, yCoord + 5.5, zCoord + 0.5) < 30 * 30) { @@ -76,11 +80,11 @@ public class TileEntityBatteryREDD extends TileEntityBatteryBase { this.audio = MainRegistry.proxy.getLoopedSound("hbm:block.fensuHum", xCoord, yCoord, zCoord, this.getVolume(1.5F), 25F, pitch, 5); this.audio.startSound(); } - + this.audio.updateVolume(this.getVolume(1.5F)); this.audio.updatePitch(pitch); this.audio.keepAlive(); - + } else { if(this.audio != null) { this.audio.stopSound(); @@ -89,7 +93,7 @@ public class TileEntityBatteryREDD extends TileEntityBatteryBase { } } } - + public float getSpeed() { return (float) Math.min(Math.pow(Math.log(this.power.doubleValue() * 0.05 + 1) * 0.05F, 5), 15F); } @@ -141,7 +145,7 @@ public class TileEntityBatteryREDD extends TileEntityBatteryBase { @Override public void readFromNBT(NBTTagCompound nbt) { super.readFromNBT(nbt); - + this.power = new BigInteger(nbt.getByteArray("power")); } @@ -149,7 +153,7 @@ public class TileEntityBatteryREDD extends TileEntityBatteryBase { @Override public void writeToNBT(NBTTagCompound nbt) { super.writeToNBT(nbt); - + nbt.setByteArray("power", this.power.toByteArray()); } @@ -186,7 +190,7 @@ public class TileEntityBatteryREDD extends TileEntityBatteryBase { public void usePower(long power) { this.power = this.power.subtract(BigInteger.valueOf(power)); } - + @Override public long transferPower(long power) { this.power = this.power.add(BigInteger.valueOf(power)); @@ -199,12 +203,12 @@ public class TileEntityBatteryREDD extends TileEntityBatteryBase { @Override public Container provideContainer(int ID, EntityPlayer player, World world, int x, int y, int z) { return new ContainerBatteryREDD(player.inventory, this); } @Override public Object provideGUI(int ID, EntityPlayer player, World world, int x, int y, int z) { return new GUIBatteryREDD(player.inventory, this); } - + AxisAlignedBB bb = null; - + @Override public AxisAlignedBB getRenderBoundingBox() { - + if(bb == null) { bb = AxisAlignedBB.getBoundingBox( xCoord - 4, @@ -215,7 +219,47 @@ public class TileEntityBatteryREDD extends TileEntityBatteryBase { zCoord + 5 ); } - + return bb; } + + // do some opencomputer stuff + @Callback(direct = true) + @Optional.Method(modid = "OpenComputers") + public Object[] getEnergyInfo(Context context, Arguments args) { + return new Object[] {this.power.doubleValue(), this.delta.longValue()}; + } + + @Callback(direct = true) + @Optional.Method(modid = "OpenComputers") + public Object[] getInfo(Context context, Arguments args) { + return new Object[] {this.power.doubleValue(), this.delta.longValue(), redLow, redHigh, getPriority().ordinal()}; + } + + @Override + @Optional.Method(modid = "OpenComputers") + public String[] methods() { + return new String[] { + "getEnergyInfo", + "getModeInfo", + "setModeLow", + "setModeHigh", + "setPriority", + "getInfo" + }; + } + + @Override + @Optional.Method(modid = "OpenComputers") + public Object[] invoke(String method, Context context, Arguments args) throws Exception { + switch (method) { + case "getEnergyInfo": return getEnergyInfo(context, args); + case "getModeInfo": return getModeInfo(context, args); + case "setModeLow": return setModeLow(context, args); + case "setModeHigh": return setModeHigh(context, args); + case "setPriority": return setPriority(context, args); + case "getInfo": return getInfo(context, args); + } + throw new NoSuchMethodException(); + } } diff --git a/src/main/java/com/hbm/tileentity/machine/storage/TileEntityBatterySocket.java b/src/main/java/com/hbm/tileentity/machine/storage/TileEntityBatterySocket.java index 5fd082740..e2f010565 100644 --- a/src/main/java/com/hbm/tileentity/machine/storage/TileEntityBatterySocket.java +++ b/src/main/java/com/hbm/tileentity/machine/storage/TileEntityBatterySocket.java @@ -12,7 +12,11 @@ import api.hbm.energymk2.IBatteryItem; import api.hbm.redstoneoverradio.IRORInteractive; import api.hbm.redstoneoverradio.IRORValueProvider; import api.hbm.tile.IInfoProviderEC; +import cpw.mods.fml.common.Optional; import io.netty.buffer.ByteBuf; +import li.cil.oc.api.machine.Arguments; +import li.cil.oc.api.machine.Callback; +import li.cil.oc.api.machine.Context; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.item.ItemStack; @@ -25,21 +29,21 @@ public class TileEntityBatterySocket extends TileEntityBatteryBase implements IR public long[] log = new long[20]; public long delta = 0; - + public int renderPack = -1; - + public TileEntityBatterySocket() { super(1); } - + @Override public String getName() { return "container.batterySocket"; } @Override public void updateEntity() { long prevPower = this.getPower(); - + super.updateEntity(); - + if(!worldObj.isRemote) { long avg = (this.getPower() + prevPower) / 2; @@ -56,7 +60,7 @@ public class TileEntityBatterySocket extends TileEntityBatteryBase implements IR @Override public void serialize(ByteBuf buf) { super.serialize(buf); - + int renderPack = -1; if(slots[0] != null && slots[0].getItem() == ModItems.battery_pack) { renderPack = slots[0].getItemDamage(); @@ -88,7 +92,7 @@ public class TileEntityBatterySocket extends TileEntityBatteryBase implements IR if(slots[0] == null || !(slots[0].getItem() instanceof IBatteryItem)) return 0; return ((IBatteryItem) slots[0].getItem()).getCharge(slots[0]); } - + @Override public void setPower(long power) { if(slots[0] == null || !(slots[0].getItem() instanceof IBatteryItem)) return; @@ -124,7 +128,7 @@ public class TileEntityBatterySocket extends TileEntityBatteryBase implements IR new BlockPos(xCoord - dir.offsetX + rot.offsetX, yCoord, zCoord - dir.offsetZ + rot.offsetZ) }; } - + @Override public DirPos[] getConPos() { ForgeDirection dir = ForgeDirection.getOrientation(this.getBlockMetadata() - 10); @@ -132,13 +136,13 @@ public class TileEntityBatterySocket extends TileEntityBatteryBase implements IR return new DirPos[] { new DirPos(xCoord + dir.offsetX, yCoord, zCoord + dir.offsetZ, dir), new DirPos(xCoord + dir.offsetX + rot.offsetX, yCoord, zCoord + dir.offsetZ + rot.offsetZ, dir), - + new DirPos(xCoord - dir.offsetX * 2, yCoord, zCoord - dir.offsetZ * 2, dir.getOpposite()), new DirPos(xCoord - dir.offsetX * 2 + rot.offsetX, yCoord, zCoord - dir.offsetZ * 2 + rot.offsetZ, dir.getOpposite()), - + new DirPos(xCoord + rot.offsetX * 2, yCoord, zCoord + rot.offsetZ * 2, rot), new DirPos(xCoord + rot.offsetX * 2 - dir.offsetX, yCoord, zCoord + rot.offsetZ * 2 - dir.offsetZ, rot), - + new DirPos(xCoord - rot.offsetX, yCoord, zCoord - rot.offsetZ, rot.getOpposite()), new DirPos(xCoord - rot.offsetX - dir.offsetX, yCoord, zCoord - rot.offsetZ - dir.offsetZ, rot.getOpposite()) }; @@ -146,12 +150,12 @@ public class TileEntityBatterySocket extends TileEntityBatteryBase implements IR @Override public Container provideContainer(int ID, EntityPlayer player, World world, int x, int y, int z) { return new ContainerBatterySocket(player.inventory, this); } @Override public Object provideGUI(int ID, EntityPlayer player, World world, int x, int y, int z) { return new GUIBatterySocket(player.inventory, this); } - + AxisAlignedBB bb = null; - + @Override public AxisAlignedBB getRenderBoundingBox() { - + if(bb == null) { bb = AxisAlignedBB.getBoundingBox( xCoord - 1, @@ -162,7 +166,7 @@ public class TileEntityBatterySocket extends TileEntityBatteryBase implements IR zCoord + 2 ); } - + return bb; } @@ -190,10 +194,10 @@ public class TileEntityBatterySocket extends TileEntityBatteryBase implements IR @Override public String runRORFunction(String name, String[] params) { - + if((PREFIX_FUNCTION + "setmode").equals(name) && params.length > 0) { int mode = IRORInteractive.parseInt(params[0], 0, 3); - + if(mode != this.redLow) { this.redLow = (short) mode; this.markChanged(); @@ -206,10 +210,10 @@ public class TileEntityBatterySocket extends TileEntityBatteryBase implements IR } return null; } - + if((PREFIX_FUNCTION + "setredmode").equals(name) && params.length > 0) { int mode = IRORInteractive.parseInt(params[0], 0, 3); - + if(mode != this.redHigh) { this.redHigh = (short) mode; this.markChanged(); @@ -222,7 +226,7 @@ public class TileEntityBatterySocket extends TileEntityBatteryBase implements IR } return null; } - + if((PREFIX_FUNCTION + "setpriority").equals(name) && params.length > 0) { int priority = IRORInteractive.parseInt(params[0], 0, 2) + 1; ConnectionPriority p = EnumUtil.grabEnumSafely(ConnectionPriority.class, priority); @@ -237,4 +241,57 @@ public class TileEntityBatterySocket extends TileEntityBatteryBase implements IR public void provideExtraInfo(NBTTagCompound data) { data.setLong(CompatEnergyControl.L_DIFF_HE, (log[0] - log[19]) / 20L); } + + // do some opencomputer stuff + @Callback(direct = true) + @Optional.Method(modid = "OpenComputers") + public Object[] getEnergyInfo(Context context, Arguments args) { + return new Object[] {getPower(), getMaxPower(), this.delta}; + } + + @Callback(direct = true) + @Optional.Method(modid = "OpenComputers") + public Object[] getPackInfo(Context context, Arguments args) { + if(slots[0] == null || !(slots[0].getItem() instanceof IBatteryItem)) return new Object[] {"", 0, 0}; + IBatteryItem bat = ((IBatteryItem) slots[0].getItem()); + return new Object[] {slots[0].getUnlocalizedName(), bat.getChargeRate(slots[0]), bat.getDischargeRate(slots[0])}; + } + + @Callback(direct = true) + @Optional.Method(modid = "OpenComputers") + public Object[] getInfo(Context context, Arguments args) { + Object[] energyInfo = getEnergyInfo(context, args); + Object[] packInfo = getPackInfo(context, args); + Object[] modeInfo = getModeInfo(context, args); + return new Object[] {energyInfo[0], energyInfo[1], energyInfo[2], modeInfo[0], modeInfo[1], modeInfo[2], packInfo[0], packInfo[1], packInfo[2]}; + } + + @Override + @Optional.Method(modid = "OpenComputers") + public String[] methods() { + return new String[] { + "getEnergyInfo", + "getPackInfo", + "getModeInfo", + "setModeLow", + "setModeHigh", + "setPriority", + "getInfo" + }; + } + + @Override + @Optional.Method(modid = "OpenComputers") + public Object[] invoke(String method, Context context, Arguments args) throws Exception { + switch (method) { + case "getEnergyInfo": return getEnergyInfo(context, args); + case "getPackInfo": return getPackInfo(context, args); + case "getModeInfo": return getModeInfo(context, args); + case "setModeLow": return setModeLow(context, args); + case "setModeHigh": return setModeHigh(context, args); + case "setPriority": return setPriority(context, args); + case "getInfo": return getInfo(context, args); + } + throw new NoSuchMethodException(); + } } diff --git a/src/main/java/com/hbm/tileentity/machine/storage/TileEntityMachineBattery.java b/src/main/java/com/hbm/tileentity/machine/storage/TileEntityMachineBattery.java index 3c0a726bb..23666eac8 100644 --- a/src/main/java/com/hbm/tileentity/machine/storage/TileEntityMachineBattery.java +++ b/src/main/java/com/hbm/tileentity/machine/storage/TileEntityMachineBattery.java @@ -129,7 +129,7 @@ public class TileEntityMachineBattery extends TileEntityMachineBase implements I nbt.setShort("redHigh", redHigh); nbt.setByte("lastRedstone", lastRedstone); nbt.setByte("priority", (byte)this.priority.ordinal()); - + if (customName != null) { nbt.setString("name", customName); } @@ -320,7 +320,7 @@ public class TileEntityMachineBattery extends TileEntityMachineBase implements I @Override @Optional.Method(modid = "OpenComputers") public String getComponentName() { - return "ntm_energy_storage"; //ok if someone else can figure out how to do this that'd be nice (change the component name based on the type of storage block) + return "ntm_energy_storage_legacy"; } @Callback(direct = true) @@ -396,10 +396,10 @@ public class TileEntityMachineBattery extends TileEntityMachineBase implements I @Override public String runRORFunction(String name, String[] params) { - + if((PREFIX_FUNCTION + "setmode").equals(name) && params.length > 0) { int mode = IRORInteractive.parseInt(params[0], 0, 3); - + if(mode != this.redLow) { this.redLow = (short) mode; this.markChanged(); @@ -412,10 +412,10 @@ public class TileEntityMachineBattery extends TileEntityMachineBase implements I } return null; } - + if((PREFIX_FUNCTION + "setredmode").equals(name) && params.length > 0) { int mode = IRORInteractive.parseInt(params[0], 0, 3); - + if(mode != this.redHigh) { this.redHigh = (short) mode; this.markChanged(); @@ -428,7 +428,7 @@ public class TileEntityMachineBattery extends TileEntityMachineBase implements I } return null; } - + if((PREFIX_FUNCTION + "setpriority").equals(name) && params.length > 0) { int priority = IRORInteractive.parseInt(params[0], 0, 2) + 1; ConnectionPriority p = EnumUtil.grabEnumSafely(ConnectionPriority.class, priority);