From a0676b6b974dfefe6a9cad48a44ad18258b4cc29 Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 24 Sep 2018 10:08:56 +0200 Subject: [PATCH] Refactored isAssemblyValid and hasReactor APIs (wip) --- .../api/computer/IEnanReactorLaser.java | 2 - .../cr0s/warpdrive/api/computer/IMachine.java | 4 +- .../computer/IMultiBlockCoreOrController.java | 3 +- ...eEntityAbstractEnergyCoreOrController.java | 38 ++++--------------- .../block/TileEntityAbstractLaser.java | 8 ++++ .../block/TileEntityAbstractMachine.java | 14 +++++++ .../detection/TileEntityCloakingCore.java | 36 +++++------------- .../energy/TileEntityEnanReactorLaser.java | 18 +++------ .../block/movement/TileEntityShipCore.java | 6 +-- 9 files changed, 51 insertions(+), 78 deletions(-) diff --git a/src/main/java/cr0s/warpdrive/api/computer/IEnanReactorLaser.java b/src/main/java/cr0s/warpdrive/api/computer/IEnanReactorLaser.java index a8b735de..01b1db35 100644 --- a/src/main/java/cr0s/warpdrive/api/computer/IEnanReactorLaser.java +++ b/src/main/java/cr0s/warpdrive/api/computer/IEnanReactorLaser.java @@ -2,8 +2,6 @@ package cr0s.warpdrive.api.computer; public interface IEnanReactorLaser extends IAbstractLaser { - Object[] hasReactor(); - Object[] stabilize(Object[] arguments); Object[] side(); diff --git a/src/main/java/cr0s/warpdrive/api/computer/IMachine.java b/src/main/java/cr0s/warpdrive/api/computer/IMachine.java index f63ed874..d9e96409 100644 --- a/src/main/java/cr0s/warpdrive/api/computer/IMachine.java +++ b/src/main/java/cr0s/warpdrive/api/computer/IMachine.java @@ -2,7 +2,9 @@ package cr0s.warpdrive.api.computer; public interface IMachine extends IInterfaced { + String[] name(final Object[] arguments); + Object[] enable(final Object[] arguments); - String[] name(final Object[] arguments); + Object[] isAssemblyValid(); } diff --git a/src/main/java/cr0s/warpdrive/api/computer/IMultiBlockCoreOrController.java b/src/main/java/cr0s/warpdrive/api/computer/IMultiBlockCoreOrController.java index f07c2a2d..89de147b 100644 --- a/src/main/java/cr0s/warpdrive/api/computer/IMultiBlockCoreOrController.java +++ b/src/main/java/cr0s/warpdrive/api/computer/IMultiBlockCoreOrController.java @@ -1,6 +1,5 @@ package cr0s.warpdrive.api.computer; public interface IMultiBlockCoreOrController extends IMultiBlock { - - Object[] isAssemblyValid(); + } diff --git a/src/main/java/cr0s/warpdrive/block/TileEntityAbstractEnergyCoreOrController.java b/src/main/java/cr0s/warpdrive/block/TileEntityAbstractEnergyCoreOrController.java index 1cbdd56b..e62dce7d 100644 --- a/src/main/java/cr0s/warpdrive/block/TileEntityAbstractEnergyCoreOrController.java +++ b/src/main/java/cr0s/warpdrive/block/TileEntityAbstractEnergyCoreOrController.java @@ -3,16 +3,8 @@ package cr0s.warpdrive.block; import cr0s.warpdrive.Commons; import cr0s.warpdrive.api.computer.IMultiBlockCoreOrController; import cr0s.warpdrive.api.computer.IMultiBlockCore; -import dan200.computercraft.api.lua.ILuaContext; -import dan200.computercraft.api.peripheral.IComputerAccess; -import li.cil.oc.api.machine.Arguments; -import li.cil.oc.api.machine.Callback; -import li.cil.oc.api.machine.Context; - import javax.annotation.Nonnull; -import net.minecraftforge.fml.common.Optional; - public abstract class TileEntityAbstractEnergyCoreOrController extends TileEntityAbstractEnergyConsumer implements IMultiBlockCoreOrController { // persistent properties @@ -25,40 +17,24 @@ public abstract class TileEntityAbstractEnergyCoreOrController extends TileEntit super(); // (abstract) peripheralName = "xxx"; - addMethods(new String[] { - "isAssemblyValid", - }); + // addMethods(new String[] { + // }); } + @Override public void onCoreUpdated(@Nonnull final IMultiBlockCore multiblockCore) { assert multiblockCore instanceof TileEntityAbstractEnergyCoreOrController; name = ((TileEntityAbstractEnergyCoreOrController) multiblockCore).name; } // Common OC/CC methods - @Override - abstract public Object[] isAssemblyValid(); + // (none) // OpenComputer callback methods - @Callback - @Optional.Method(modid = "opencomputers") - public Object[] isAssemblyValid(final Context context, final Arguments arguments) { - return isAssemblyValid(); - } + // (none) - // ComputerCraft IPeripheral methods implementation - @Override - @Optional.Method(modid = "computercraft") - public Object[] callMethod(@Nonnull final IComputerAccess computer, @Nonnull final ILuaContext context, final int method, @Nonnull final Object[] arguments) { - final String methodName = CC_getMethodNameAndLogCall(method, arguments); - - switch (methodName) { - case "isAssemblyValid": - return isAssemblyValid(); - } - - return super.callMethod(computer, context, method, arguments); - } + // ComputerCraft IPeripheral methods + // (none) @Override public String toString() { diff --git a/src/main/java/cr0s/warpdrive/block/TileEntityAbstractLaser.java b/src/main/java/cr0s/warpdrive/block/TileEntityAbstractLaser.java index 88f6642c..a6ee2d64 100644 --- a/src/main/java/cr0s/warpdrive/block/TileEntityAbstractLaser.java +++ b/src/main/java/cr0s/warpdrive/block/TileEntityAbstractLaser.java @@ -200,6 +200,14 @@ public abstract class TileEntityAbstractLaser extends TileEntityAbstractMachine return new Object[] { cache_laserMedium_count }; } + @Override + public Object[] isAssemblyValid() { + if (laserMedium_direction == null) { + return new Object[] { false, "No laser medium detected" }; + } + return super.isAssemblyValid(); + } + // OpenComputers callback methods @Callback @Optional.Method(modid = "opencomputers") diff --git a/src/main/java/cr0s/warpdrive/block/TileEntityAbstractMachine.java b/src/main/java/cr0s/warpdrive/block/TileEntityAbstractMachine.java index 4cd0742d..7bf742d4 100644 --- a/src/main/java/cr0s/warpdrive/block/TileEntityAbstractMachine.java +++ b/src/main/java/cr0s/warpdrive/block/TileEntityAbstractMachine.java @@ -33,6 +33,7 @@ public abstract class TileEntityAbstractMachine extends TileEntityAbstractInterf addMethods(new String[] { "name", "enable", + "isAssemblyValid" }); } @@ -135,6 +136,11 @@ public abstract class TileEntityAbstractMachine extends TileEntityAbstractInterf return new Object[] { isEnabled }; } + @Override + public Object[] isAssemblyValid() { + return new Object[] { true, "ok" }; + } + // OpenComputer callback methods @Callback @Optional.Method(modid = "opencomputers") @@ -149,6 +155,11 @@ public abstract class TileEntityAbstractMachine extends TileEntityAbstractInterf } @Callback + @Optional.Method(modid = "opencomputers") + public Object[] isAssemblyValid(final Context context, final Arguments arguments) { + return isAssemblyValid(); + } + // ComputerCraft IPeripheral methods @Override @Optional.Method(modid = "computercraft") @@ -161,6 +172,9 @@ public abstract class TileEntityAbstractMachine extends TileEntityAbstractInterf case "enable": return enable(arguments); + + case "isAssemblyValid": + return isAssemblyValid(); } return super.callMethod(computer, context, method, arguments); diff --git a/src/main/java/cr0s/warpdrive/block/detection/TileEntityCloakingCore.java b/src/main/java/cr0s/warpdrive/block/detection/TileEntityCloakingCore.java index 8eccbb7a..cd4da2dd 100644 --- a/src/main/java/cr0s/warpdrive/block/detection/TileEntityCloakingCore.java +++ b/src/main/java/cr0s/warpdrive/block/detection/TileEntityCloakingCore.java @@ -11,13 +11,7 @@ import cr0s.warpdrive.data.EnumComponentType; import cr0s.warpdrive.data.SoundEvents; import cr0s.warpdrive.data.Vector3; import cr0s.warpdrive.network.PacketHandler; -import dan200.computercraft.api.lua.ILuaContext; -import dan200.computercraft.api.peripheral.IComputerAccess; -import li.cil.oc.api.machine.Arguments; -import li.cil.oc.api.machine.Callback; -import li.cil.oc.api.machine.Context; -import javax.annotation.Nonnull; import java.util.Arrays; import net.minecraft.block.state.IBlockState; @@ -28,8 +22,6 @@ import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos.MutableBlockPos; -import net.minecraftforge.fml.common.Optional; - public class TileEntityCloakingCore extends TileEntityAbstractEnergyConsumer { private static final int CLOAKING_CORE_SOUND_UPDATE_TICKS = 40; @@ -469,31 +461,21 @@ public class TileEntityCloakingCore extends TileEntityAbstractEnergyConsumer { return new Object[] { energyRequired / updateRate }; } + @Override public Object[] isAssemblyValid() { - return new Object[] { isValid, Commons.removeFormatting(textValidityIssues.getUnformattedText()) }; + if (!isValid) { + return new Object[] { false, Commons.removeFormatting(textValidityIssues.getUnformattedText()) }; + } + return super.isAssemblyValid(); } // OpenComputer callback methods - @Callback - @Optional.Method(modid = "opencomputers") - public Object[] isAssemblyValid(final Context context, final Arguments arguments) { - return isAssemblyValid(); - } + // (none) - // ComputerCraft IPeripheral methods implementation - @Override - @Optional.Method(modid = "computercraft") - public Object[] callMethod(@Nonnull final IComputerAccess computer, @Nonnull final ILuaContext context, final int method, @Nonnull final Object[] arguments) { - final String methodName = CC_getMethodNameAndLogCall(method, arguments); - - switch (methodName) { - case "isAssemblyValid": - return isAssemblyValid(); - } - - return super.callMethod(computer, context, method, arguments); - } + // ComputerCraft IPeripheral methods + // (none) + // TileEntityAbstractEnergy methods @Override public int energy_getMaxStorage() { return WarpDriveConfig.CLOAKING_MAX_ENERGY_STORED; diff --git a/src/main/java/cr0s/warpdrive/block/energy/TileEntityEnanReactorLaser.java b/src/main/java/cr0s/warpdrive/block/energy/TileEntityEnanReactorLaser.java index 8acebe62..8f562147 100644 --- a/src/main/java/cr0s/warpdrive/block/energy/TileEntityEnanReactorLaser.java +++ b/src/main/java/cr0s/warpdrive/block/energy/TileEntityEnanReactorLaser.java @@ -43,7 +43,7 @@ public class TileEntityEnanReactorLaser extends TileEntityAbstractLaser implemen super(); addMethods(new String[] { - "hasReactor", + "isAssemblyValid", "side", "stabilize" }); @@ -202,8 +202,11 @@ public class TileEntityEnanReactorLaser extends TileEntityAbstractLaser implemen // Common OC/CC methods @Override - public Object[] hasReactor() { - return new Object[] { reactorFace != EnumReactorFace.UNKNOWN }; + public Object[] isAssemblyValid() { + if (reactorFace == EnumReactorFace.UNKNOWN) { + return new Object[] { false, "No reactor detected" }; + } + return super.isAssemblyValid(); } @Override @@ -230,12 +233,6 @@ public class TileEntityEnanReactorLaser extends TileEntityAbstractLaser implemen } // OpenComputers callback methods - @Callback - @Optional.Method(modid = "opencomputers") - public Object[] hasReactor(final Context context, final Arguments arguments) { - return hasReactor(); - } - @Callback @Optional.Method(modid = "opencomputers") public Object[] stabilize(final Context context, final Arguments arguments) { @@ -255,9 +252,6 @@ public class TileEntityEnanReactorLaser extends TileEntityAbstractLaser implemen final String methodName = CC_getMethodNameAndLogCall(method, arguments); switch (methodName) { - case "hasReactor": - return hasReactor(); - case "stabilize": return stabilize(arguments); diff --git a/src/main/java/cr0s/warpdrive/block/movement/TileEntityShipCore.java b/src/main/java/cr0s/warpdrive/block/movement/TileEntityShipCore.java index e3fee3f5..aaa42505 100644 --- a/src/main/java/cr0s/warpdrive/block/movement/TileEntityShipCore.java +++ b/src/main/java/cr0s/warpdrive/block/movement/TileEntityShipCore.java @@ -1212,10 +1212,10 @@ public class TileEntityShipCore extends TileEntityAbstractShipController impleme // Common OC/CC methods @Override public Object[] isAssemblyValid() { - if (isValid) { - return new Object[] { true, "ok" }; + if (!isValid) { + return new Object[] { false, reasonInvalid.getUnformattedText() }; } - return new Object[] { false, reasonInvalid.getUnformattedText() }; + return super.isAssemblyValid(); } @Override