Skip to content

Commit

Permalink
Added Support for OpenComputers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexatos committed Jul 25, 2014
1 parent 24aaf76 commit 8e8bae2
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/main/java/thut/reference/ThutTechReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public class ThutTechReference extends ThutCoreReference {
public static final String MOD_NAME = "Thut's Tech";
public static final String CLIENT_PROXY_CLASS = "thut.tech.client.ClientProxy";
public static final String COMMON_PROXY_CLASS = "thut.tech.common.CommonProxy";

public static final String MOD_OPENCOMPUTERS = "OpenComputers";
}
17 changes: 9 additions & 8 deletions src/main/java/thut/tech/common/blocks/technical/BlockLift.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,21 @@ public int onBlockPlaced(World worldObj, int x, int y, int z, int side, float hi
* @return The ForgeDirection the entity is facing
*/
private ForgeDirection getFacingfromEntity(EntityLivingBase e) {
ForgeDirection side = null;
double angle = Math.abs(e.getRotationYawHead()) % 360;
ForgeDirection side = ForgeDirection.NORTH;
double angle = e.rotationYaw % 360;
double angle2 = Math.abs(angle);

if(angle > 315 || angle <= 45) {
if(angle2 > 315 || angle2 <= 45) {
return ForgeDirection.SOUTH;
}
if(angle > 45 && angle <= 135) {
return ForgeDirection.EAST;
if((angle > 45 && angle <= 135) || (angle < -225 && angle >= -315)) {
return ForgeDirection.WEST;
}
if(angle > 135 && angle <= 225) {
if(angle2 > 135 && angle2 <= 225) {
return ForgeDirection.NORTH;
}
if(angle > 225 && angle <= 315) {
return ForgeDirection.WEST;
if((angle > 225 && angle <= 315) || (angle < -45 && angle >= -135)) {
return ForgeDirection.EAST;
}
return side;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package thut.tech.common.blocks.tileentity;

import cpw.mods.fml.common.Optional;
import li.cil.oc.api.network.Arguments;
import li.cil.oc.api.network.Callback;
import li.cil.oc.api.network.Context;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.init.Blocks;
Expand All @@ -12,6 +16,7 @@
import net.minecraftforge.common.util.ForgeDirection;
import thut.api.ThutBlocks;
import thut.api.maths.Vector3;
import thut.reference.ThutTechReference;
import thut.tech.common.entity.EntityLift;
import thut.util.LogHelper;

Expand All @@ -32,7 +37,11 @@
//import dan200.computer.api.IPeripheral;
//import universalelectricity.core.block.IElectricityStorage;

public class TileEntityLiftAccess extends TileEntity// implements IPeripheral//, IGridMachine, IDirectionalMETile
@Optional.InterfaceList({
@Optional.Interface(iface = "li.cil.oc.api.network.SimpleComponent", modid = ThutTechReference.MOD_OPENCOMPUTERS),
//@Optional.Interface(iface = "dan200.computer.api.IPeripheral", modid = "ComputerCraft")
})
public class TileEntityLiftAccess extends TileEntity implements li.cil.oc.api.network.SimpleComponent// implements IPeripheral//, IGridMachine, IDirectionalMETile
{

public int power = 0;
Expand Down Expand Up @@ -221,7 +230,8 @@ public boolean checkSides() {

public synchronized void setFloor(int floor) {
if(lift != null && floor <= 64 && floor > 0) {
lift.setFoor(this, floor);
lift.removeFloor(this.floor);
lift.setFloor(this, floor);
this.floor = floor;
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
Expand Down Expand Up @@ -251,15 +261,15 @@ public void readFromNBT(NBTTagCompound par1) {
side = par1.getInteger("side");
floor = par1.getInteger("floor");
liftID = par1.getInteger("lift");
root = root.readFromNBT(par1, "root");
root = Vector3.readFromNBT(par1, "root");
if(EntityLift.lifts.containsKey(liftID)) {
lift = EntityLift.lifts.get(liftID);
}
}

public void doButtonClick(int side, float hitX, float hitY, float hitZ) {
if(!worldObj.isRemote && lift != null) {
if(side == this.side) {
if(side == this.side && !lift.called) {
int button = getButtonFromClick(side, hitX, hitY, hitZ);
buttonPress(button);
calledFloor = lift.destinationFloor;
Expand Down Expand Up @@ -396,6 +406,82 @@ public void setBlock(ForgeDirection side, Block id, int meta) {
worldObj.setBlock(xCoord + side.offsetX, yCoord + side.offsetY, zCoord + side.offsetZ, id, meta, 3);
}

@Optional.Method(modid = ThutTechReference.MOD_OPENCOMPUTERS)
@Override
public String getComponentName() {
return "elevator";
}

@Optional.Method(modid = ThutTechReference.MOD_OPENCOMPUTERS)
@Callback
public Object[] call(Context context, Arguments args) {
if(args.count() >= 1 && args.checkInteger(0) >= 1 && args.checkInteger(0) <= 16) {
int lFloor = args.checkInteger(0);
if(!worldObj.isRemote && lift != null) {
if(!lift.called && doesFloorExist(lFloor)) {
buttonPress(lFloor);
calledFloor = lift.destinationFloor;
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
LogHelper.debug(calledFloor + " " + lFloor + " " + lift);
} else {
return new Object[] { false, "floor does not exist." };
}
}
} else {
args.checkInteger(0);
}
return new Object[] { true };
}

@Optional.Method(modid = ThutTechReference.MOD_OPENCOMPUTERS)
@Callback
public Object[] isReady(Context context, Arguments args) {
return new Object[] { !lift.called };
}

@Optional.Method(modid = ThutTechReference.MOD_OPENCOMPUTERS)
@Callback
public Object[] getLocalFloor(Context context, Arguments args) {
return new Object[] { this.floor };
}

@Optional.Method(modid = ThutTechReference.MOD_OPENCOMPUTERS)
@Callback
public Object[] getElevatorFloor(Context context, Arguments args) {
if(lift.destinationFloor >= 1) {
return new Object[] { lift.currentFloor, lift.destinationFloor };
}
return new Object[] { lift.currentFloor };
}

@Optional.Method(modid = ThutTechReference.MOD_OPENCOMPUTERS)
private boolean doesFloorExist(int lFloor) {
if(lift.floorArray[lFloor - 1] != null) {
for(int j = 0; j < 4; j++) {
if(lift.floorArray[lFloor - 1][j] != null && lift.floorArray[lFloor - 1][j].length == 3) {
int x = lift.floorArray[lFloor - 1][j][0];
int y = lift.floorArray[lFloor - 1][j][1];
int z = lift.floorArray[lFloor - 1][j][2];
if(worldObj.getTileEntity(x, y, z) != null && worldObj.getTileEntity(x, y, z) instanceof TileEntityLiftAccess) {
return true;
}
}
}
}
return false;
}

@Optional.Method(modid = ThutTechReference.MOD_OPENCOMPUTERS)
@Callback
public Object[] doesFloorExist(Context context, Arguments args) {
if(args.count() >= 1 && args.checkInteger(0) >= 1 && args.checkInteger(0) <= 16) {
return new Object[] { doesFloorExist(args.checkInteger(0)) };
} else {
args.checkInteger(0);
}
return new Object[] { false, "floor is not a number between 1 and 16" };
}

//////////////////////////////////////////////////////////ComputerCraft Stuff/////////////////////////////////////////////////////////////////
// @Override
// public String getType() {
Expand Down
43 changes: 42 additions & 1 deletion src/main/java/thut/tech/common/entity/EntityLift.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package thut.tech.common.entity;

import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint;
import cpw.mods.fml.common.registry.IEntityAdditionalSpawnData;
import io.netty.buffer.ByteBuf;
Expand All @@ -23,6 +24,7 @@
import thut.api.maths.Matrix3;
import thut.api.maths.Vector3;
import thut.api.network.PacketPipeline;
import thut.reference.ThutTechReference;
import thut.tech.common.blocks.tileentity.TileEntityLiftAccess;
import thut.tech.common.handlers.ConfigHandler;
import thut.tech.common.items.ItemLinker;
Expand Down Expand Up @@ -72,6 +74,9 @@ public class EntityLift extends EntityLivingBase implements IEntityAdditionalSpa
public double prevFloorY = 0;
public double prevFloor = 0;

public double currentFloor = 0;
private boolean hasJustMoved = false;

public boolean called = false;
TileEntityLiftAccess current;
public int id;
Expand Down Expand Up @@ -162,8 +167,27 @@ public void onUpdate() {
accelerate();
if(toMoveY) {
doMotion();
if(Loader.isModLoaded(ThutTechReference.MOD_OPENCOMPUTERS)) {
for(int j = 0; j < floorArray.length; j++) {
for(int i = 0; i < 4; i++) {
if(floorArray[j][i] != null) {
if(floorArray[j][i] != null && floorArray[j][i].length == 3) {
int y = floorArray[j][i][1];
if(y == ((int) this.posY) && (currentFloor != j + 1) && (j + 1 != 0)) {
currentFloor = j + 1;
hasJustMoved = true;
}
}
}
}
}
}
} else {
setPosition(posX, called && Math.abs(posY - destinationY) < 0.5 ? destinationY : Math.floor(posY), posZ);
if(hasJustMoved) {
currentFloor = destinationFloor + 1;
hasJustMoved = false;
}
called = false;
prevFloor = destinationFloor;
prevFloorY = destinationY;
Expand Down Expand Up @@ -276,6 +300,10 @@ public void doMotion() {
prevFloor = destinationFloor;
prevFloorY = destinationY;
destinationY = -1;
if(hasJustMoved) {
currentFloor = destinationFloor;
hasJustMoved = false;
}
destinationFloor = 0;
if(current != null) {
current.setCalled(false);
Expand Down Expand Up @@ -308,6 +336,10 @@ public void doMotion() {
prevFloor = destinationFloor;
prevFloorY = destinationY;
destinationY = -1;
if(hasJustMoved) {
currentFloor = destinationFloor;
hasJustMoved = false;
}
destinationFloor = 0;
if(current != null) {
current.setCalled(false);
Expand Down Expand Up @@ -543,7 +575,7 @@ public void readSpawnData(ByteBuf data) {
id = data.readInt();
}

public void setFoor(TileEntityLiftAccess te, int floor) {
public void setFloor(TileEntityLiftAccess te, int floor) {
if(te.floor == 0) {
int j = 0;
for(int i = 0; i < 4; i++) {
Expand Down Expand Up @@ -578,6 +610,15 @@ public void setFoor(TileEntityLiftAccess te, int floor) {
// super.entityInit();
// }

public void removeFloor(int floor) {
for(int i = 0; i < 4; i++) {
floors[floor - 1][i] = null;
for(int j = 0; j < 3; j++) {
floorArray[floor - 1][i][j] = 0;
}
}
}

@Override
public void readEntityFromNBT(NBTTagCompound nbt) {
axis = nbt.getBoolean("axis");
Expand Down

0 comments on commit 8e8bae2

Please sign in to comment.