Skip to content

Commit

Permalink
Merge pull request #588 from cam72cam/computercraft_support
Browse files Browse the repository at this point in the history
Add support for ComputerCraft and refactor common access API
  • Loading branch information
cam72cam authored May 2, 2019
2 parents 7b63209 + eb3b8b2 commit 82da1f8
Show file tree
Hide file tree
Showing 8 changed files with 424 additions and 213 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ buildscript {
jcenter()
maven { url = "http://files.minecraftforge.net/maven" }
}
dependencies {
dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT'
}
}
}

repositories {
Expand All @@ -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'
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ org.gradle.jvmargs=-Xmx3G

version_major=1
version_minor=6
version_patch=0
version_patch=1
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
211 changes: 211 additions & 0 deletions src/main/java/cam72cam/immersiverailroading/thirdparty/CommonAPI.java
Original file line number Diff line number Diff line change
@@ -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<? extends EntityRollingStock> 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<IFluidHandler> 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<String, Object> info() {
if (stock != null) {
Map<String, Object> 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<String, Object> 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<String, Object> info = new HashMap<>();
List<Object> 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<String, Object> 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Loading

0 comments on commit 82da1f8

Please sign in to comment.