diff --git a/build.gradle b/build.gradle index d331a6734..caea09956 100644 --- a/build.gradle +++ b/build.gradle @@ -3,9 +3,9 @@ buildscript { jcenter() maven { url = "http://files.minecraftforge.net/maven" } } - dependencies { + dependencies { classpath 'net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT' - } + } } repositories { @@ -14,6 +14,7 @@ repositories { url = "http://maven.k-4u.nl/" } maven { url = "http://maven.cil.li/" } + maven { url = "https://cc.crzd.me/maven/" } } apply plugin: 'net.minecraftforge.gradle.forge' @@ -75,6 +76,7 @@ dependencies { compile "igwmod:IGW-Mod-1.12.2:1.4.4-15:userdev" compile "li.cil.oc:OpenComputers:MC1.12.2-1.7.+:api" runtime "li.cil.oc:OpenComputers:MC1.12.2-1.7.+" + compile "dan200.computercraft:ComputerCraft:1.80pr1-build3" } processResources { diff --git a/gradle.properties b/gradle.properties index b8d1e8962..48b6b289b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,4 +4,4 @@ org.gradle.jvmargs=-Xmx3G version_major=1 version_minor=6 -version_patch=0 +version_patch=1 diff --git a/src/main/java/cam72cam/immersiverailroading/ImmersiveRailroading.java b/src/main/java/cam72cam/immersiverailroading/ImmersiveRailroading.java index b4638aedb..3cf8893d6 100644 --- a/src/main/java/cam72cam/immersiverailroading/ImmersiveRailroading.java +++ b/src/main/java/cam72cam/immersiverailroading/ImmersiveRailroading.java @@ -22,7 +22,7 @@ public class ImmersiveRailroading { public static final String MODID = "immersiverailroading"; - public static final String VERSION = "1.6.0"; + public static final String VERSION = "1.6.1"; public static final int ENTITY_SYNC_DISTANCE = 512; private static Logger logger; diff --git a/src/main/java/cam72cam/immersiverailroading/thirdparty/CommonAPI.java b/src/main/java/cam72cam/immersiverailroading/thirdparty/CommonAPI.java new file mode 100644 index 000000000..11fa0c021 --- /dev/null +++ b/src/main/java/cam72cam/immersiverailroading/thirdparty/CommonAPI.java @@ -0,0 +1,211 @@ +package cam72cam.immersiverailroading.thirdparty; + +import cam72cam.immersiverailroading.entity.*; +import cam72cam.immersiverailroading.physics.PhysicsAccummulator; +import cam72cam.immersiverailroading.registry.EntityRollingStockDefinition; +import cam72cam.immersiverailroading.registry.LocomotiveDefinition; +import cam72cam.immersiverailroading.tile.TileRailBase; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3d; +import net.minecraft.world.World; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.capability.CapabilityFluidHandler; +import net.minecraftforge.fluids.capability.IFluidHandler; + +import java.util.*; + +public class CommonAPI { + private final EntityRollingStock stock; + + public static CommonAPI create(World world, BlockPos pos) { + return create(world, pos, EntityRollingStock.class); + } + + public static CommonAPI create(World world, BlockPos pos, Class stockClass) { + TileRailBase te = TileRailBase.get(world, pos); + if (te != null) { + EntityRollingStock stock = te.getStockNearBy(stockClass, null); + if (stock != null) { + return new CommonAPI(stock); + } + } + return null; + } + + public CommonAPI(EntityRollingStock stock) { + this.stock = stock; + } + + public FluidStack getFluid() { + Capability capability = CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY; + IFluidHandler fh = stock.getCapability(capability, null); + if (fh != null) { + return fh.drain(Integer.MAX_VALUE, false); + } + return null; + } + + public Map info() { + if (stock != null) { + Map info = new HashMap<>(); + EntityRollingStockDefinition def = stock.getDefinition(); + + info.put("id", def.defID); + info.put("name", def.name()); + info.put("tag", stock.tag); + info.put("weight", stock.getWeight()); + + EnumFacing dir = EnumFacing.fromAngle(stock.rotationYaw); + if (stock instanceof EntityMoveableRollingStock) { + EntityMoveableRollingStock movable = (EntityMoveableRollingStock) stock; + info.put("speed", movable.getCurrentSpeed().metric()); + + if (movable.getCurrentSpeed().metric() < 0) { + dir = dir.getOpposite(); + } + } + info.put("direction", dir.toString()); + + if (stock instanceof EntityRidableRollingStock) { + EntityRidableRollingStock ridable = (EntityRidableRollingStock) stock; + info.put("passengers", stock.getPassengers().size() + ridable.staticPassengers.size()); + } + + if (stock instanceof Locomotive) { + Locomotive loco = (Locomotive) stock; + LocomotiveDefinition locoDef = loco.getDefinition(); + info.put("horsepower", locoDef.getHorsePower(loco.gauge)); + info.put("traction", locoDef.getStartingTractionNewtons(loco.gauge)); + info.put("max_speed", locoDef.getMaxSpeed(loco.gauge).metric()); + info.put("brake", loco.getAirBrake()); + info.put("throttle", loco.getThrottle()); + + if (loco instanceof LocomotiveSteam) { + LocomotiveSteam steam = (LocomotiveSteam) loco; + info.put("pressure", steam.getBoilerPressure()); + info.put("temperature", steam.getBoilerTemperature()); + } + if (loco instanceof LocomotiveDiesel) { + info.put("temperature", ((LocomotiveDiesel) loco).getEngineTemperature()); + } + } + + FluidStack fluid = getFluid(); + if (fluid != null) { + info.put("fluid_type", fluid.getFluid().getName()); + info.put("fluid_amount", fluid.amount); + } else { + info.put("fluid_type", null); + info.put("fluid_amount", 0); + } + if (stock instanceof FreightTank) { + info.put("fluid_max", ((FreightTank) stock).getTankCapacity().MilliBuckets()); + } + + if (stock instanceof Freight) { + Freight freight = ((Freight) stock); + info.put("cargo_percent", freight.getPercentCargoFull()); + info.put("cargo_size", freight.getInventorySize()); + } + return info; + } + return null; + } + + public Map consist(boolean supportsList) { + if (!(stock instanceof EntityCoupleableRollingStock)) { + return null; + } + EntityCoupleableRollingStock stock = (EntityCoupleableRollingStock) this.stock; + + int traction = 0; + PhysicsAccummulator acc = new PhysicsAccummulator(stock.getCurrentTickPosAndPrune()); + stock.mapTrain(stock, true, true, acc::accumulate); + Map info = new HashMap<>(); + List locos = new ArrayList<>(); + + info.put("cars", acc.count); + info.put("tractive_effort_N", acc.tractiveEffortNewtons); + info.put("weight_kg", acc.massToMoveKg); + info.put("speed_km", stock.getCurrentSpeed().metric()); + EnumFacing dir = EnumFacing.fromAngle(stock.rotationYaw); + if (stock.getCurrentSpeed().metric() < 0) { + dir = dir.getOpposite(); + } + info.put("direction", dir.toString()); + + for (EntityCoupleableRollingStock car : stock.getTrain()) { + if (car instanceof Locomotive) { + LocomotiveDefinition locoDef = ((Locomotive) car).getDefinition(); + traction += locoDef.getStartingTractionNewtons(car.gauge); + locos.add(new CommonAPI(car).info()); + } + } + if (supportsList) { + info.put("locomotives", locos); + } else { + Map locomotives = new HashMap<>(); + for (int i = 0; i < locos.size(); i++) { + locomotives.put("" + i, locos.get(i)); + } + info.put("locomotives", locomotives); + } + info.put("total_traction_N", traction); + return info; + } + + public String getTag() { + return stock.tag; + } + + public void setTag(String tag) { + stock.tag = tag; + } + + private float normalize(double val) { + if (Double.isNaN(val)) { + return 0; + } + if (val > 1) { + return 1; + } + if (val < -1) { + return -1; + } + return (float)val; + } + + public void setThrottle(double throttle) { + if (stock instanceof Locomotive) { + ((Locomotive)stock).setThrottle(normalize(throttle)); + } + } + public void setAirBrake(double brake) { + if (stock instanceof Locomotive) { + ((Locomotive)stock).setAirBrake(normalize(brake)); + } + } + + public void setHorn(int horn) { + if (stock instanceof Locomotive) { + ((Locomotive)stock).setHorn(horn, null); + } + } + + public void setBell(int bell) { + if (stock instanceof Locomotive) { + ((Locomotive)stock).setBell(bell); + } + } + + public Vec3d getPosition() { + return stock.getPositionVector(); + } + + public UUID getUniqueID() { + return stock.getUniqueID(); + } + +} diff --git a/src/main/java/cam72cam/immersiverailroading/thirdparty/CompatLoader.java b/src/main/java/cam72cam/immersiverailroading/thirdparty/CompatLoader.java index 0ef2a8cf1..86391ba90 100644 --- a/src/main/java/cam72cam/immersiverailroading/thirdparty/CompatLoader.java +++ b/src/main/java/cam72cam/immersiverailroading/thirdparty/CompatLoader.java @@ -19,5 +19,6 @@ public static Object invokeStatic(String modID, String cname, String method, Obj public static void load() { invokeStatic("opencomputers", "cam72cam.immersiverailroading.thirdparty.opencomputers.Compat", "init"); invokeStatic("immersiveengineering", "cam72cam.immersiverailroading.thirdparty.ImmersiveEngineering", "init"); + invokeStatic("computercraft", "cam72cam.immersiverailroading.thirdparty.ComputerCraft", "init"); } } diff --git a/src/main/java/cam72cam/immersiverailroading/thirdparty/ComputerCraft.java b/src/main/java/cam72cam/immersiverailroading/thirdparty/ComputerCraft.java new file mode 100644 index 000000000..f48973b0e --- /dev/null +++ b/src/main/java/cam72cam/immersiverailroading/thirdparty/ComputerCraft.java @@ -0,0 +1,155 @@ +package cam72cam.immersiverailroading.thirdparty; + +import cam72cam.immersiverailroading.ImmersiveRailroading; +import cam72cam.immersiverailroading.library.Augment; +import cam72cam.immersiverailroading.tile.TileRailBase; +import dan200.computercraft.api.ComputerCraftAPI; +import dan200.computercraft.api.lua.ILuaContext; +import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.peripheral.IComputerAccess; +import dan200.computercraft.api.peripheral.IPeripheral; +import dan200.computercraft.api.peripheral.IPeripheralProvider; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.LinkedHashMap; + +public class ComputerCraft { + public static void init() { + ComputerCraftAPI.registerPeripheralProvider(new IPeripheralProvider() { + @Nullable + @Override + public IPeripheral getPeripheral(@Nonnull World world, @Nonnull BlockPos blockPos, @Nonnull EnumFacing enumFacing) { + TileRailBase rail = TileRailBase.get(world, blockPos); + if (rail != null) { + if (rail.getAugment() == Augment.DETECTOR) { + return new DetectorPeripheral(world, blockPos); + } + if (rail.getAugment() == Augment.LOCO_CONTROL) { + return new LocoControlPeripheral(world, blockPos); + } + } + return null; + } + }); + } + + @FunctionalInterface + private interface APICall { + Object[] apply(CommonAPI api, Object[] params) throws LuaException; + } + + private static abstract class BasePeripheral implements IPeripheral { + private final World world; + private final BlockPos pos; + private final String[] fnNames; + private final APICall[] fnImpls; + + public BasePeripheral(World world, BlockPos blockPos, LinkedHashMap methods) { + this.world = world; + this.pos = blockPos; + this.fnNames = methods.keySet().toArray(new String[0]); + this.fnImpls = methods.values().toArray(new APICall[0]); + } + + @Nonnull + @Override + public String[] getMethodNames() { + return fnNames; + } + + @Nullable + @Override + public Object[] callMethod(@Nonnull IComputerAccess iComputerAccess, @Nonnull ILuaContext iLuaContext, int i, @Nonnull Object[] objects) throws LuaException, InterruptedException { + try { + CommonAPI api = CommonAPI.create(world, pos); + if (api != null && i < fnImpls.length) { + return fnImpls[i].apply(api, objects); + } + } catch (Exception ex) { + ImmersiveRailroading.catching(ex); + } + return null; + } + + @Override + public boolean equals(@Nullable IPeripheral iPeripheral) { + return iPeripheral == this; + } + } + + private static Object getObjParam(Object[] params, int id, String name) throws LuaException { + if (params.length > id) { + return params[id]; + } + throw new LuaException("Required parameter \"" + name +"\""); + } + + private static double getDoubleParam(Object[] params, int id, String name) throws LuaException { + Object obj = getObjParam(params, id, name); + try { + return Double.parseDouble(obj.toString()); + } catch (NumberFormatException ex) { + throw new LuaException("Required parameter \"" + name +"\" is not a number"); + } + } + + private static class DetectorPeripheral extends BasePeripheral { + private static LinkedHashMap methods = new LinkedHashMap<>(); + static { + methods.put("info", (CommonAPI api, Object[] params) -> new Object[]{api.info()}); + methods.put("consist", (CommonAPI api, Object[] params) -> new Object[]{api.consist(false)}); + methods.put("getTag", (CommonAPI api, Object[] params) -> new Object[]{api.getTag()}); + methods.put("setTag", (CommonAPI api, Object[] params) -> { + api.setTag(getObjParam(params, 0, "tag").toString()); + return null; + }); + } + + public DetectorPeripheral(World world, BlockPos blockPos) { + super(world, blockPos, methods); + } + + @Nonnull + @Override + public String getType() { + return "ir_augment_detector"; + } + } + + private static class LocoControlPeripheral extends BasePeripheral { + private static LinkedHashMap methods = new LinkedHashMap<>(); + static { + methods.putAll(DetectorPeripheral.methods); + methods.put("setThrottle", (CommonAPI api, Object[] params) -> { + api.setThrottle(getDoubleParam(params, 0, "throttle")); + return null; + }); + methods.put("setBrake", (CommonAPI api, Object[] params) -> { + api.setAirBrake(getDoubleParam(params, 0, "brake")); + return null; + }); + methods.put("setHorn", (CommonAPI api, Object[] params) -> { + api.setHorn((int) getDoubleParam(params, 0, "horn")); + return null; + }); + methods.put("setBell", (CommonAPI api, Object[] params) -> { + api.setBell((int) getDoubleParam(params, 0, "bell")); + return null; + }); + } + + public LocoControlPeripheral(World world, BlockPos blockPos) { + super(world, blockPos, methods); + } + + @Nonnull + @Override + public String getType() { + return "ir_augment_control"; + } + } +} diff --git a/src/main/java/cam72cam/immersiverailroading/thirdparty/opencomputers/AugmentDriver.java b/src/main/java/cam72cam/immersiverailroading/thirdparty/opencomputers/AugmentDriver.java index 86bc1fe42..cf0c37a68 100644 --- a/src/main/java/cam72cam/immersiverailroading/thirdparty/opencomputers/AugmentDriver.java +++ b/src/main/java/cam72cam/immersiverailroading/thirdparty/opencomputers/AugmentDriver.java @@ -1,20 +1,10 @@ package cam72cam.immersiverailroading.thirdparty.opencomputers; -import java.util.*; - import cam72cam.immersiverailroading.Config; -import cam72cam.immersiverailroading.entity.EntityCoupleableRollingStock; -import cam72cam.immersiverailroading.entity.EntityMoveableRollingStock; import cam72cam.immersiverailroading.entity.EntityRollingStock; -import cam72cam.immersiverailroading.entity.Freight; -import cam72cam.immersiverailroading.entity.FreightTank; import cam72cam.immersiverailroading.entity.Locomotive; -import cam72cam.immersiverailroading.entity.LocomotiveDiesel; -import cam72cam.immersiverailroading.entity.LocomotiveSteam; import cam72cam.immersiverailroading.library.Augment; -import cam72cam.immersiverailroading.physics.PhysicsAccummulator; -import cam72cam.immersiverailroading.registry.EntityRollingStockDefinition; -import cam72cam.immersiverailroading.registry.LocomotiveDefinition; +import cam72cam.immersiverailroading.thirdparty.CommonAPI; import cam72cam.immersiverailroading.tile.TileRailBase; import li.cil.oc.api.Network; import li.cil.oc.api.driver.DriverBlock; @@ -29,10 +19,8 @@ import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -import net.minecraftforge.common.capabilities.Capability; -import net.minecraftforge.fluids.FluidStack; -import net.minecraftforge.fluids.capability.CapabilityFluidHandler; -import net.minecraftforge.fluids.capability.IFluidHandler; + +import java.util.UUID; public class AugmentDriver implements DriverBlock { @@ -141,152 +129,42 @@ public String preferredName() { return "ir_augment_detector"; } - private FluidStack getFluid() { - TileRailBase te = TileRailBase.get(world, pos); - Capability capability = CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY; - EntityMoveableRollingStock stock = te.getStockNearBy(capability); - if (stock != null) { - IFluidHandler fh = stock.getCapability(capability, null); - return fh.drain(Integer.MAX_VALUE, false); - } - return null; - } - - public void loco_info(Locomotive car, Map info) { - TileRailBase te = TileRailBase.get(world, pos); - EntityMoveableRollingStock stock = te.getStockNearBy(null); - EntityRollingStockDefinition def = car.getDefinition(); - - info.put("id", def.defID); - info.put("name", def.name()); - info.put("tag", car.tag); - EnumFacing dir = EnumFacing.fromAngle(car.rotationYaw); - if (car.getCurrentSpeed().metric() < 0) { - dir = dir.getOpposite(); - } - info.put("direction", dir.toString()); - - info.put("passengers", car.getPassengers().size() + stock.staticPassengers.size()); - info.put("speed", car.getCurrentSpeed().metric()); - info.put("weight", car.getWeight()); - LocomotiveDefinition locoDef = car.getDefinition(); - info.put("horsepower", locoDef.getHorsePower(car.gauge)); - info.put("traction", locoDef.getStartingTractionNewtons(car.gauge)); - info.put("max_speed", locoDef.getMaxSpeed(car.gauge).metric()); - info.put("brake", car.getAirBrake()); - info.put("throttle", car.getThrottle()); - - if (car instanceof LocomotiveSteam) { - LocomotiveSteam steam = (LocomotiveSteam) car; - info.put("pressure", steam.getBoilerPressure()); - info.put("temperature", steam.getBoilerTemperature()); - } - if (car instanceof LocomotiveDiesel) { - info.put("temperature", ((LocomotiveDiesel) car).getEngineTemperature()); - } - } - @Callback(doc = "function():table -- returns an info dump about the current car") public Object[] info(Context context, Arguments arguments) { - TileRailBase te = TileRailBase.get(world, pos); - EntityMoveableRollingStock stock = te.getStockNearBy(null); - if (stock != null) { - Map info = new HashMap(); - EntityRollingStockDefinition def = stock.getDefinition(); - - info.put("id", def.defID); - info.put("name", def.name()); - info.put("tag", stock.tag); - EnumFacing dir = EnumFacing.fromAngle(stock.rotationYaw); - if (stock.getCurrentSpeed().metric() < 0) { - dir = dir.getOpposite(); - } - info.put("direction", dir.toString()); - - info.put("passengers", stock.getPassengers().size() + stock.staticPassengers.size()); - info.put("speed", stock.getCurrentSpeed().metric()); - info.put("weight", stock.getWeight()); - - if (stock instanceof Locomotive) { - loco_info((Locomotive)stock, info); - } - - FluidStack fluid = getFluid(); - if (fluid != null) { - info.put("fluid_type", fluid.getFluid().getName()); - info.put("fluid_amount", fluid.amount); - } else { - info.put("fluid_type", null); - info.put("fluid_amount", 0); - } - if (stock instanceof FreightTank) { - info.put("fluid_max", ((FreightTank) stock).getTankCapacity().MilliBuckets()); - } - - if (stock instanceof Freight) { - Freight freight = ((Freight) stock); - info.put("cargo_percent", freight.getPercentCargoFull()); - info.put("cargo_size", freight.getInventorySize()); - } - return new Object[] { info }; + CommonAPI api = CommonAPI.create(world, pos); + if (api != null) { + return new Object[] { + api.info() + }; } return null; } @Callback(doc = "function():table -- returns an info dump about the current consist") public Object[] consist(Context context, Arguments arguments) { - TileRailBase te = TileRailBase.get(world, pos); - EntityCoupleableRollingStock stock = te.getStockNearBy(EntityCoupleableRollingStock.class, null); - if (stock != null) { - int traction=0; - PhysicsAccummulator acc = new PhysicsAccummulator(stock.getCurrentTickPosAndPrune()); - stock.mapTrain(stock, true, true, acc::accumulate); - Map info = new HashMap(); - List locos = new ArrayList(); - - info.put("cars", acc.count); - info.put("tractive_effort_N", acc.tractiveEffortNewtons); - info.put("weight_kg", acc.massToMoveKg); - info.put("speed_km", stock.getCurrentSpeed().metric()); - EnumFacing dir = EnumFacing.fromAngle(stock.rotationYaw); - if (stock.getCurrentSpeed().metric() < 0) { - dir = dir.getOpposite(); - } - info.put("direction", dir.toString()); - - for (EntityCoupleableRollingStock car : stock.getTrain()) { - if (car instanceof Locomotive) { - LocomotiveDefinition locoDef = ((Locomotive)car).getDefinition(); - traction+=locoDef.getStartingTractionNewtons(car.gauge); - Map sub_info = new HashMap(); - loco_info((Locomotive) car, sub_info); - locos.add(sub_info); - } - } - info.put("locomotives", locos); - info.put("totoal_traction_N", traction); - - return new Object[] { info }; + CommonAPI api = CommonAPI.create(world, pos); + if (api != null) { + return new Object[] { + api.consist(true) + }; } return null; } @Callback(doc = "function():table -- gets the stock's tag") public Object[] getTag(Context context, Arguments arguments) { - TileRailBase te = TileRailBase.get(world, pos); - EntityMoveableRollingStock stock = te.getStockNearBy(null); - if (stock != null) { - return new Object[] {stock.tag}; + CommonAPI api = CommonAPI.create(world, pos); + if (api != null) { + return new Object[] { api.getTag() }; } return null; } @Callback(doc = "function():table -- sets the stock's tag") public Object[] setTag(Context context, Arguments arguments) { - TileRailBase te = TileRailBase.get(world, pos); - EntityMoveableRollingStock stock = te.getStockNearBy(null); - if (stock != null) { - stock.tag = arguments.checkString(0); + CommonAPI api = CommonAPI.create(world, pos); + if (api != null) { + api.setTag(arguments.checkString(0)); } return null; } @@ -319,52 +197,35 @@ public String preferredName() { @Callback(doc = "function(double) -- sets the locomotive throttle") public Object[] setThrottle(Context context, Arguments arguments) throws Exception { - TileRailBase te = TileRailBase.get(world, pos); - Locomotive stock = te.getStockNearBy(Locomotive.class, null); - if (stock != null) { - stock.setThrottle(normalize(arguments.checkDouble(0))); + CommonAPI api = CommonAPI.create(world, pos, Locomotive.class); + if (api != null) { + api.setThrottle(arguments.checkDouble(0)); } return null; } - - private float normalize(double val) { - if (Double.isNaN(val)) { - return 0; - } - if (val > 1) { - return 1; - } - if (val < -1) { - return -1; - } - return (float)val; - } @Callback(doc = "function(double) -- sets the locomotive brake") public Object[] setBrake(Context context, Arguments arguments) throws Exception { - TileRailBase te = TileRailBase.get(world, pos); - Locomotive stock = te.getStockNearBy(Locomotive.class, null); - if (stock != null) { - stock.setAirBrake(normalize(arguments.checkDouble(0))); + CommonAPI api = CommonAPI.create(world, pos, Locomotive.class); + if (api != null) { + api.setAirBrake(arguments.checkDouble(0)); } return null; } @Callback(doc = "function() -- fires the locomotive horn") public Object[] horn(Context context, Arguments arguments) throws Exception { - TileRailBase te = TileRailBase.get(world, pos); - Locomotive stock = te.getStockNearBy(Locomotive.class, null); - if (stock != null) { - stock.setHorn(arguments.optInteger(0, 40), null); + CommonAPI api = CommonAPI.create(world, pos, Locomotive.class); + if (api != null) { + api.setHorn(arguments.optInteger(0, 40)); } return null; } @Callback(doc = "function() -- sets the locomotive bell") public Object[] bell(Context context, Arguments arguments) throws Exception { - TileRailBase te = TileRailBase.get(world, pos); - Locomotive stock = te.getStockNearBy(Locomotive.class, null); - if (stock != null) { - stock.setBell(arguments.optInteger(0, 40)); + CommonAPI api = CommonAPI.create(world, pos, Locomotive.class); + if (api != null) { + api.setBell(arguments.optInteger(0, 40)); } return null; } diff --git a/src/main/java/cam72cam/immersiverailroading/thirdparty/opencomputers/RadioCtrlCardDriver.java b/src/main/java/cam72cam/immersiverailroading/thirdparty/opencomputers/RadioCtrlCardDriver.java index 31237033f..b7edaf1a4 100644 --- a/src/main/java/cam72cam/immersiverailroading/thirdparty/opencomputers/RadioCtrlCardDriver.java +++ b/src/main/java/cam72cam/immersiverailroading/thirdparty/opencomputers/RadioCtrlCardDriver.java @@ -4,6 +4,7 @@ import cam72cam.immersiverailroading.Config; import cam72cam.immersiverailroading.IRItems; import cam72cam.immersiverailroading.entity.Locomotive; +import cam72cam.immersiverailroading.thirdparty.CommonAPI; import li.cil.oc.api.Network; import li.cil.oc.api.driver.DriverItem; import li.cil.oc.api.driver.item.Slot; @@ -14,6 +15,7 @@ import li.cil.oc.api.prefab.AbstractManagedEnvironment; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; import li.cil.oc.api.network.ComponentConnector; import li.cil.oc.api.network.EnvironmentHost; @@ -62,15 +64,13 @@ public NBTTagCompound dataTag(ItemStack stack) { } public class RadioCtrlCardManager extends AbstractManagedEnvironment { - protected double[] cardPosition = {0,0,0}; - protected Locomotive linkedLoco; + protected Vec3d cardPosition; + protected CommonAPI api; protected ComponentConnector node; public RadioCtrlCardManager(Locomotive loco, double x, double y, double z) { - cardPosition[0] = x; - cardPosition[1] = y; - cardPosition[2] = z; - linkedLoco = loco; + cardPosition = new Vec3d(x, y, z); + api = new CommonAPI(loco); node = Network.newNode(this, Visibility.Network).withComponent("ir_remote_control", Visibility.Network).withConnector().create(); setNode(node); } @@ -86,39 +86,19 @@ public void update() { } @Callback(doc = "function(double) -- sets the locomotive throttle") - public Object[] setThrottle(Context context, Arguments arguments) throws Exception { + public Object[] setThrottle(Context context, Arguments arguments) { if (radioDrain()) { - linkedLoco.setThrottle(normalize(arguments.checkDouble(0))); + api.setThrottle(arguments.checkDouble(0)); } return null; } - private float normalize(double val) { - if (Double.isNaN(val)) { - return 0; - } - if (val > 1) { - return 1; - } - if (val < -1) { - return -1; - } - return (float) val; - } - - private boolean radioDrain() + private boolean radioDrain() { - if(linkedLoco == null) { + if(api == null) { return false; } - double distance = 0; - double[] currentLocoPosition = {linkedLoco.posX, linkedLoco.posY, linkedLoco.posZ}; - double[] relativeDistanceVector = {0,0,0}; - for(int i = 0; i <= 2; i++) { - relativeDistanceVector[i] = currentLocoPosition[i] - cardPosition[i]; - distance += (relativeDistanceVector[i] * relativeDistanceVector[i]); - } - distance = Math.sqrt( distance ); + double distance = api.getPosition().distanceTo(cardPosition); if( distance > Config.ConfigBalance.RadioRange) { return false; } @@ -129,24 +109,24 @@ private boolean radioDrain() } @Callback(doc = "function(double) -- sets the locomotive brake") - public Object[] setBrake(Context context, Arguments arguments) throws Exception { + public Object[] setBrake(Context context, Arguments arguments) { if (radioDrain()) { - linkedLoco.setAirBrake(normalize(arguments.checkDouble(0))); + api.setAirBrake(arguments.checkDouble(0)); } return null; } @Callback(doc = "function() -- fires the locomotive horn") - public Object[] horn(Context context, Arguments arguments) throws Exception { + public Object[] horn(Context context, Arguments arguments) { if (radioDrain()) { - linkedLoco.setHorn(arguments.optInteger(0, 40), null); + api.setHorn(arguments.optInteger(0, 40)); } return null; } @Callback(doc = "function() -- sets the locomotive bell") - public Object[] bell(Context context, Arguments arguments) throws Exception { + public Object[] bell(Context context, Arguments arguments) { if (radioDrain()) { - linkedLoco.setBell(arguments.optInteger(0, 40)); + api.setBell(arguments.optInteger(0, 40)); } return null; } @@ -154,7 +134,8 @@ public Object[] bell(Context context, Arguments arguments) throws Exception { @Callback(doc = "function():array -- returns the XYZ position of the locomotive") public Object[] getPos(Context context, Arguments args) { if (radioDrain()) { - return new Object[] { linkedLoco.posX, linkedLoco.posY, linkedLoco.posZ }; + Vec3d pos = api.getPosition(); + return new Object[] { pos.x, pos.y, pos.z }; } return null; } @@ -162,7 +143,7 @@ public Object[] getPos(Context context, Arguments args) { @Callback(doc = "function():araray -- returns the UUID of the bound loco") public Object[] getLinkUUID(Context context, Arguments args) { if (radioDrain()) { - return new Object[] { linkedLoco.getUniqueID() }; + return new Object[] { api.getUniqueID() }; } return new Object[] { null }; }