Merging OpenComputer support and API cleanup

Renamed energy to getEnergyLevel in ChunkLoader, Transporter and
PowerStore API
Updated PowerReactor API:
- active replaces getActive and setActive
This commit is contained in:
LemADEC 2014-10-05 19:33:50 +02:00
parent 17da2fb627
commit 9a8b4818b5
6 changed files with 218 additions and 299 deletions

View file

@ -447,8 +447,8 @@ function cloaking_page()
SetColorDefault() SetColorDefault()
Write("Cloaking core " .. cloaking_currentKey .. " of " .. #cloakingcores) Write("Cloaking core " .. cloaking_currentKey .. " of " .. #cloakingcores)
local isAssemblyValid = cloakingcore.isAssemblyValid() local isAssemblyValid = cloakingcore.isAssemblyValid()
local energy = cloakingcore.getEnergyLevel() local energy, energyMax = cloakingcore.getEnergyLevel()
local isEnabled = true -- cloakingcore.isEnabled() local isEnabled = cloakingcore.enable()
if not isAssemblyValid then if not isAssemblyValid then
SetColorWarning() SetColorWarning()
@ -508,19 +508,19 @@ end
function cloaking_start() function cloaking_start()
for key,cloakingcore in pairs(cloakingcores) do for key,cloakingcore in pairs(cloakingcores) do
cloakingcore.disableCloakingField() cloakingcore.enable(false)
if cloaking_highTier then if cloaking_highTier then
cloakingcore.setFieldTier(2) cloakingcore.tier(2)
else else
cloakingcore.setFieldTier(1) cloakingcore.tier(1)
end end
cloakingcore.enableCloakingField() cloakingcore.enable(true)
end end
end end
function cloaking_stop() function cloaking_stop()
for key,cloakingcore in pairs(cloakingcores) do for key,cloakingcore in pairs(cloakingcores) do
cloakingcore.disableCloakingField() cloakingcore.enable(false)
end end
end end
@ -720,7 +720,7 @@ function data_setName()
label = readInputText(label) label = readInputText(label)
os.setComputerLabel(label) os.setComputerLabel(label)
if warpcore ~= nil then if warpcore ~= nil then
warpcore.set_core_frequency(label) warpcore.coreFrequency(label)
end end
os.reboot() os.reboot()
end end
@ -753,15 +753,14 @@ function core_boot()
Write(".") Write(".")
core_front, core_right, core_up = warpcore.dim_getp() core_front, core_right, core_up = warpcore.dim_getp()
core_back, core_left, core_down = warpcore.dim_getn() core_back, core_left, core_down = warpcore.dim_getn()
core_isInHyper = warpcore.is_in_hyperspace() core_isInHyper = warpcore.isInHyperspace()
Write(".") Write(".")
repeat repeat
X = warpcore.get_x() pos = warpcore.pos()
sleep(0.3) sleep(0.3)
until X ~= nil until pos ~= nil
Y = warpcore.get_y() X, Y, Z = warpcore.pos()
Z = warpcore.get_z()
Write(".") Write(".")
repeat repeat
isAttached = warpcore.isAttached() isAttached = warpcore.isAttached()
@ -770,7 +769,7 @@ function core_boot()
Write(".") Write(".")
repeat repeat
core_shipSize = warpcore.get_ship_size() core_shipSize = warpcore.getShipSize()
sleep(0.3) sleep(0.3)
until core_shipSize ~= nil until core_shipSize ~= nil
@ -778,7 +777,7 @@ function core_boot()
core_computeRealDistance() core_computeRealDistance()
Write(".") Write(".")
warpcore.set_mode(1) warpcore.mode(1)
WriteLn("") WriteLn("")
end end
@ -823,8 +822,7 @@ function core_computeNewCoordinates(cx, cy, cz)
elseif data.core_direction == 2 then elseif data.core_direction == 2 then
res.y = res.y - core_realDistance res.y = res.y - core_realDistance
end end
local dx = warpcore.get_dx() local dx, dy, dz = warpcore.getOrientation()
local dz = warpcore.get_dz()
if dx ~= 0 then if dx ~= 0 then
if data.core_direction == 0 then if data.core_direction == 0 then
res.x = res.x + (core_realDistance * dx) res.x = res.x + (core_realDistance * dx)
@ -853,14 +851,14 @@ function core_warp()
rs.setOutput(alarm_side, true) rs.setOutput(alarm_side, true)
if readConfirmation() then if readConfirmation() then
rs.setOutput(alarm_side, false) rs.setOutput(alarm_side, false)
warpcore.set_direction(data.core_direction) warpcore.direction(data.core_direction)
warpcore.set_distance(data.core_distance) warpcore.distance(data.core_distance)
if core_isInHyper then if core_isInHyper then
warpcore.set_mode(2) warpcore.mode(2)
else else
warpcore.set_mode(1) warpcore.mode(1)
end end
warpcore.do_jump() warpcore.jump()
end end
rs.setOutput(alarm_side, false) rs.setOutput(alarm_side, false)
end end
@ -936,17 +934,17 @@ function core_page_setDimensions()
Write(" Down (".. core_down ..") : ") Write(" Down (".. core_down ..") : ")
core_down = readInputNumber(core_down) core_down = readInputNumber(core_down)
Write("Setting dimensions...") Write("Setting dimensions...")
warpcore.dim_setp(core_front, core_right, core_up) warpcore.dim_positive(core_front, core_right, core_up)
warpcore.dim_setn(core_back, core_left, core_down) warpcore.dim_negative(core_back, core_left, core_down)
core_shipSize = warpcore.get_ship_size() core_shipSize = warpcore.getShipSize()
if core_shipSize == nil then core_shipSize = 0 end if core_shipSize == nil then core_shipSize = 0 end
end end
function core_page_summon() function core_page_summon()
ShowTitle("<==== Summon players ====>") ShowTitle("<==== Summon players ====>")
local players = Explode(",", warpcore.get_attached_players()) local playersString, playersArray = warpcore.getAttachedPlayers()
for i = 1, #players do for i = 1, #playersArray do
Show(i..". "..players[i]) Show(i..". "..playersArray[i])
end end
SetColorTitle() SetColorTitle()
ShowMenu("Enter player number") ShowMenu("Enter player number")
@ -971,9 +969,9 @@ function core_page_jumpToBeacon()
rs.setOutput(alarm_side, true) rs.setOutput(alarm_side, true)
if readConfirmation() then if readConfirmation() then
rs.setOutput(alarm_side, false) rs.setOutput(alarm_side, false)
warpcore.set_mode(4) warpcore.mode(4)
warpcore.set_beacon_frequency(freq) warpcore.beaconFrequency(freq)
warpcore.do_jump() warpcore.jump()
end end
rs.setOutput(alarm_side, false) rs.setOutput(alarm_side, false)
end end
@ -986,9 +984,9 @@ function core_page_jumpToGate()
rs.setOutput(alarm_side, true) rs.setOutput(alarm_side, true)
if readConfirmation() then if readConfirmation() then
rs.setOutput(alarm_side, false) rs.setOutput(alarm_side, false)
warpcore.set_mode(6) warpcore.mode(6)
warpcore.set_target_jumpgate(name) warpcore.targetJumpgate(name)
warpcore.do_jump() warpcore.jump()
end end
rs.setOutput(alarm_side, false) rs.setOutput(alarm_side, false)
end end
@ -998,17 +996,13 @@ function core_page()
if warpcore ~= nil then if warpcore ~= nil then
WriteLn("Core:") WriteLn("Core:")
WriteLn(" x, y, z = " .. X .. ", " .. Y .. ", " .. Z) WriteLn(" x, y, z = " .. X .. ", " .. Y .. ", " .. Z)
local energy = warpcore.get_energy_level() local energy, energyMax = warpcore.getEnergyLevel()
if energy == nil then energy = 0 end if energy == nil then energy = 0 end
local energyMax = 100000000 if energyMax == nil then energyMax = 1 end
if warpcore.get_energy_max ~= nil then
energyMax = warpcore.get_energy_max()
if energyMax == nil then energyMax = 1 end
end
WriteLn(" Energy = " .. math.floor(energy / energyMax * 100) .. " % (" .. energy .. "EU)") WriteLn(" Energy = " .. math.floor(energy / energyMax * 100) .. " % (" .. energy .. "EU)")
local players = warpcore.get_attached_players() local playersString, playersArray = warpcore.getAttachedPlayers()
if players == nil then players = "-" end if playersString == "" then players = "-" end
WriteLn(" Attached players = " .. players) WriteLn(" Attached players = " .. playersString)
WriteLn("Dimensions:") WriteLn("Dimensions:")
WriteLn(" Front, Right, Up = " .. FormatInteger(core_front) .. ", " .. FormatInteger(core_right) .. ", " .. FormatInteger(core_up)) WriteLn(" Front, Right, Up = " .. FormatInteger(core_front) .. ", " .. FormatInteger(core_right) .. ", " .. FormatInteger(core_up))
WriteLn(" Back, Left, Down = " .. FormatInteger(core_back) .. ", " .. FormatInteger(core_left) .. ", " .. FormatInteger(core_down)) WriteLn(" Back, Left, Down = " .. FormatInteger(core_back) .. ", " .. FormatInteger(core_left) .. ", " .. FormatInteger(core_down))
@ -1069,8 +1063,8 @@ function core_key(keycode)
rs.setOutput(alarm_side, true) rs.setOutput(alarm_side, true)
if readConfirmation() then if readConfirmation() then
rs.setOutput(alarm_side, false) rs.setOutput(alarm_side, false)
warpcore.set_mode(5) warpcore.mode(5)
warpcore.do_jump() warpcore.jump()
end end
rs.setOutput(alarm_side, false) rs.setOutput(alarm_side, false)
return true return true
@ -1088,7 +1082,7 @@ reactor_output = 0
function reactor_boot() function reactor_boot()
if reactor ~= nil then if reactor ~= nil then
WriteLn("Booting Reactor...") WriteLn("Booting Reactor...")
local isActive, strMode, releaseRate = reactor.getActive() local isActive, strMode, releaseRate = reactor.active()
if strMode == "OFF" then if strMode == "OFF" then
data.reactor_mode = 0 data.reactor_mode = 0
elseif strMode == "MANUAL" then elseif strMode == "MANUAL" then
@ -1199,7 +1193,7 @@ function reactor_page()
SetColorDefault() SetColorDefault()
SetCursorPos(1, 9) SetCursorPos(1, 9)
Write("Activated: ") Write("Activated: ")
isActive = reactor.getActive() isActive = reactor.active()
if isActive then SetColorSuccess() else SetColorDefault() end if isActive then SetColorSuccess() else SetColorDefault() end
Write(boolToYesNo(isActive)) Write(boolToYesNo(isActive))
end end
@ -1304,13 +1298,13 @@ end
function reactor_start() function reactor_start()
if reactor ~= nil then if reactor ~= nil then
reactor_setMode() reactor_setMode()
reactor.setActive(true) reactor.active(true)
end end
end end
function reactor_stop() function reactor_stop()
if reactor ~= nil then if reactor ~= nil then
reactor.setActive(false) reactor.active(false)
end end
end end

View file

@ -11,9 +11,8 @@ import cr0s.WarpDrive.WarpDriveConfig;
import cr0s.WarpDrive.api.IUpgradable; import cr0s.WarpDrive.api.IUpgradable;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
public class TileEntityChunkLoader extends WarpChunkTE implements IPeripheral, IUpgradable public class TileEntityChunkLoader extends WarpChunkTE implements IUpgradable
{ {
private boolean canLoad = false; private boolean canLoad = false;
private boolean shouldLoad = false; private boolean shouldLoad = false;
@ -24,25 +23,25 @@ public class TileEntityChunkLoader extends WarpChunkTE implements IPeripheral, I
int negDX, posDX, negDZ, posDZ; int negDX, posDX, negDZ, posDZ;
int area = 1; int area = 1;
private String[] methodArray = { public TileEntityChunkLoader() {
"energy", super();
negDX = 0;
negDZ = 0;
posDX = 0;
posDZ = 0;
peripheralName = "warpdriveChunkloader";
methodsArray = new String[] {
"getEnergyLevel",
"radius", "radius",
"bounds", "bounds",
"active", "active",
"upgrades", "upgrades",
"help" "help"
}; };
{
negDX = 0;
negDZ = 0;
posDX = 0;
posDZ = 0;
} }
@Override @Override
public int getMaxEnergyStored() public int getMaxEnergyStored() {
{
return WarpDriveConfig.CL_MAX_ENERGY; return WarpDriveConfig.CL_MAX_ENERGY;
} }
@ -123,19 +122,11 @@ public class TileEntityChunkLoader extends WarpChunkTE implements IPeripheral, I
nbt.setInteger("posDX", posDX); nbt.setInteger("posDX", posDX);
nbt.setInteger("posDZ", posDZ); nbt.setInteger("posDZ", posDZ);
} }
@Override
public String getType()
{
return "warpdriveChunkloader";
}
@Override
public String[] getMethodNames()
{
return methodArray;
}
// OpenComputer callback methods
// FIXME: implement OpenComputers...
// ComputerCraft IPeripheral methods implementation
private String helpStr(Object[] args) private String helpStr(Object[] args)
{ {
if(args.length == 1) if(args.length == 1)
@ -156,13 +147,12 @@ public class TileEntityChunkLoader extends WarpChunkTE implements IPeripheral, I
} }
@Override @Override
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception {
{ String meth = methodsArray[method];
String meth = methodArray[method];
if(meth.equals("energy")) if(meth.equals("getEnergyLevel")) {
return getEnergyObject(); return getEnergyLevel();
else if(meth.equals("radius")) } else if(meth.equals("radius"))
{ {
if(arguments.length == 1) if(arguments.length == 1)
{ {
@ -205,23 +195,7 @@ public class TileEntityChunkLoader extends WarpChunkTE implements IPeripheral, I
return null; return null;
} }
@Override
public void attach(IComputerAccess computer)
{
}
@Override
public void detach(IComputerAccess computer)
{
}
@Override
public boolean equals(IPeripheral other)
{
return false;
}
@Override @Override
public boolean takeUpgrade(EnumUpgradeTypes upgradeType, boolean simulate) public boolean takeUpgrade(EnumUpgradeTypes upgradeType, boolean simulate)
{ {

View file

@ -9,9 +9,8 @@ import cr0s.WarpDrive.WarpDrive;
import cr0s.WarpDrive.WarpDriveConfig; import cr0s.WarpDrive.WarpDriveConfig;
import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.peripheral.IPeripheral;
public class TileEntityLaserTreeFarm extends TileEntityAbstractMiner implements IPeripheral { public class TileEntityLaserTreeFarm extends TileEntityAbstractMiner {
Boolean active = false; Boolean active = false;
private int mode = 0; private int mode = 0;
@ -26,14 +25,17 @@ public class TileEntityLaserTreeFarm extends TileEntityAbstractMiner implements
private int totalHarvested=0; private int totalHarvested=0;
private int scan=0; private int scan = 0;
private int xSize = defSize; private int xSize = defSize;
private int zSize = defSize; private int zSize = defSize;
LinkedList<Vector3> logs; LinkedList<Vector3> logs;
private int logIndex = 0; private int logIndex = 0;
private String[] methodsArray = { public TileEntityLaserTreeFarm() {
super();
peripheralName = "treefarmLaser";
methodsArray = new String[] {
"start", "start",
"stop", "stop",
"area", "area",
@ -42,10 +44,7 @@ public class TileEntityLaserTreeFarm extends TileEntityAbstractMiner implements
"silkTouchLeaves", "silkTouchLeaves",
"treetap", "treetap",
"state" "state"
}; };
public TileEntityLaserTreeFarm() {
super();
} }
@Override @Override
@ -233,16 +232,10 @@ public class TileEntityLaserTreeFarm extends TileEntityAbstractMiner implements
return active; return active;
} }
@Override // OpenComputer callback methods
public String getType() { // FIXME: implement OpenComputers...
return "treefarmLaser";
}
@Override
public String[] getMethodNames() {
return methodsArray;
}
// ComputerCraft IPeripheral methods implementation
@Override @Override
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception { public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception {
String methodName = methodsArray[method]; String methodName = methodsArray[method];
@ -311,14 +304,6 @@ public class TileEntityLaserTreeFarm extends TileEntityAbstractMiner implements
return null; return null;
} }
@Override
public void attach(IComputerAccess computer) {
}
@Override
public void detach(IComputerAccess computer) {
}
//ABSTRACT LASER IMPLEMENTATION //ABSTRACT LASER IMPLEMENTATION
@Override @Override
protected boolean silkTouch(int blockID) { protected boolean silkTouch(int blockID) {
@ -362,9 +347,4 @@ public class TileEntityLaserTreeFarm extends TileEntityAbstractMiner implements
protected float getColorB() { protected float getColorB() {
return 0.4f; return 0.4f;
} }
@Override
public boolean equals(IPeripheral other) {
return other == this;
}
} }

View file

@ -11,13 +11,12 @@ import cr0s.WarpDrive.api.IBlockUpdateDetector;
import dan200.computercraft.api.ComputerCraftAPI; import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
public class TileEntityPowerReactor extends WarpEnergyTE implements IPeripheral, IBlockUpdateDetector { public class TileEntityPowerReactor extends WarpEnergyTE implements IBlockUpdateDetector {
private int containedEnergy = 0; private int containedEnergy = 0;
// generation & instability is 'per tick' // generation & instability is 'per tick'
@ -58,18 +57,20 @@ public class TileEntityPowerReactor extends WarpEnergyTE implements IPeripheral,
private int releaseAbove = 0; private int releaseAbove = 0;
private boolean init = false; private boolean init = false;
private String[] methodArray = { public TileEntityPowerReactor() {
"getActive", super();
"setActive", // boolean peripheralName = "warpdriveReactor";
"energy", // returns energy, maxenergy methodsArray = new String[] {
"active",
"energy", // returns energy, max energy, energy rate
"instability", // returns ins0,1,2,3 "instability", // returns ins0,1,2,3
"release", // releases all energy "release", // releases all energy
"releaseRate", // releases energy when more than arg0 is produced "releaseRate", // releases energy when more than arg0 is produced
"releaseAbove", // releases any energy above arg0 amount "releaseAbove", // releases any energy above arg0 amount
"help" // returns help on arg0 function "help" // returns help on arg0 function
}; };
private HashMap<Integer,IComputerAccess> connectedComputers = new HashMap<Integer,IComputerAccess>(); }
private void increaseInstability(ForgeDirection from, boolean isNatural) { private void increaseInstability(ForgeDirection from, boolean isNatural) {
if (canOutputEnergy(from) || hold) { if (canOutputEnergy(from) || hold) {
@ -293,59 +294,11 @@ public class TileEntityPowerReactor extends WarpEnergyTE implements IPeripheral,
} }
} }
//COMPUTER INTERFACES // OpenComputer callback methods
@Override // FIXME: implement OpenComputers...
public String getType() {
return "warpdriveReactor";
}
@Override public Object[] active(Object[] arguments) throws Exception {
public String[] getMethodNames() { if (arguments.length == 1) {
return methodArray;
}
@Override
public void attach(IComputerAccess computer) {
int id = computer.getID();
connectedComputers.put(id, computer);
if (WarpDriveConfig.G_LUA_SCRIPTS != WarpDriveConfig.LUA_SCRIPTS_NONE) {
computer.mount("/power", ComputerCraftAPI.createResourceMount(WarpDrive.class, "warpdrive", "lua/power"));
computer.mount("/warpupdater", ComputerCraftAPI.createResourceMount(WarpDrive.class, "warpdrive", "lua/common/updater"));
if (WarpDriveConfig.G_LUA_SCRIPTS == WarpDriveConfig.LUA_SCRIPTS_ALL) {
computer.mount("/startup", ComputerCraftAPI.createResourceMount(WarpDrive.class, "warpdrive", "lua/power/startup"));
}
}
}
@Override
public void detach(IComputerAccess computer) {
int id = computer.getID();
if (connectedComputers.containsKey(id)) {
connectedComputers.remove(id);
}
}
@Override
public boolean equals(IPeripheral other) {
return other == this;
}
@Override
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception {
// computer is alive => start updating reactor
hold = false;
String methodName = methodArray[method];
if (methodName.equals("getActive")) {
if (releaseMode == MODE_DONT_RELEASE || releaseMode == MODE_MANUAL_RELEASE) {
return new Object[] { active, MODE_STRING[releaseMode], 0 };
} else if (releaseMode == MODE_RELEASE_ABOVE) {
return new Object[] { active, MODE_STRING[releaseMode], releaseAbove };
} else {
return new Object[] { active, MODE_STRING[releaseMode], releaseRate };
}
} else if (methodName.equals("setActive")) {
boolean activate = false; boolean activate = false;
try { try {
activate = toBool(arguments[0]); activate = toBool(arguments[0]);
@ -358,82 +311,125 @@ public class TileEntityPowerReactor extends WarpEnergyTE implements IPeripheral,
sendEvent("reactorActivation", null); sendEvent("reactorActivation", null);
} }
active = activate; active = activate;
}
if (releaseMode == MODE_DONT_RELEASE || releaseMode == MODE_MANUAL_RELEASE) {
return new Object[] { active, MODE_STRING[releaseMode], 0 };
} else if (releaseMode == MODE_RELEASE_ABOVE) {
return new Object[] { active, MODE_STRING[releaseMode], releaseAbove };
} else {
return new Object[] { active, MODE_STRING[releaseMode], releaseRate };
}
}
private Object[] release(Object[] arguments) throws Exception {
boolean doRelease = false;
if (arguments.length > 0) {
try {
doRelease = toBool(arguments[0]);
} catch(Exception e) {
throw new Exception("Function expects an boolean value");
}
releaseMode = doRelease ? MODE_MANUAL_RELEASE : MODE_DONT_RELEASE;
releaseAbove = 0;
releaseRate = 0;
}
return new Object[] { releaseMode != MODE_DONT_RELEASE };
}
private Object[] releaseRate(Object[] arguments) throws Exception {
int rate = -1;
try {
rate = toInt(arguments[0]);
} catch(Exception e) {
throw new Exception("Function expects an integer value");
}
if (rate <= 0) {
releaseMode = MODE_DONT_RELEASE;
releaseRate = 0;
} else {
/* releaseAbove = (int)Math.ceil(Math.pow(rate, 1.0 / 0.6));
WarpDrive.debugPrint("releaseAbove " + releaseAbove);
releaseMode = MODE_RELEASE_ABOVE;/**/
// player has to adjust it
releaseRate = rate;
releaseMode = MODE_RELEASE_AT_RATE;
}
return new Object[] { MODE_STRING[releaseMode], releaseRate };
}
private Object[] releaseAbove(Object[] arguments) throws Exception {
int above = -1;
try {
above = toInt(arguments[0]);
} catch(Exception e) {
throw new Exception("Function expects an integer value");
}
if (above <= 0) {
releaseMode = 0;
releaseAbove = MODE_DONT_RELEASE;
} else {
releaseMode = MODE_RELEASE_ABOVE;
releaseAbove = above;
}
return new Object[] { MODE_STRING[releaseMode], releaseAbove };
}
// ComputerCraft IPeripheral methods implementation
@Override
public void attach(IComputerAccess computer) {
super.attach(computer);
int id = computer.getID();
connectedComputers.put(id, computer);
if (WarpDriveConfig.G_LUA_SCRIPTS != WarpDriveConfig.LUA_SCRIPTS_NONE) {
computer.mount("/power", ComputerCraftAPI.createResourceMount(WarpDrive.class, "warpdrive", "lua/power"));
computer.mount("/warpupdater", ComputerCraftAPI.createResourceMount(WarpDrive.class, "warpdrive", "lua/common/updater"));
if (WarpDriveConfig.G_LUA_SCRIPTS == WarpDriveConfig.LUA_SCRIPTS_ALL) {
computer.mount("/startup", ComputerCraftAPI.createResourceMount(WarpDrive.class, "warpdrive", "lua/power/startup"));
}
}
}
@Override
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception {
// computer is alive => start updating reactor
hold = false;
String methodName = methodsArray[method];
if (methodName.equals("active")) {
return active(arguments);
} else if (methodName.equals("energy")) { } else if (methodName.equals("energy")) {
return new Object[] { containedEnergy, WarpDriveConfig.PR_MAX_ENERGY, releasedLastCycle / WarpDriveConfig.PR_TICK_TIME }; return new Object[] { containedEnergy, WarpDriveConfig.PR_MAX_ENERGY, releasedLastCycle / WarpDriveConfig.PR_TICK_TIME };
} else if (methodName.equals("instability")) { } else if (methodName.equals("instability")) {
Object[] retVal = new Object[4]; Object[] retVal = new Object[4];
for(int i = 0; i < 4; i++) { for(int i = 0; i < 4; i++) {
retVal[i] = instabilityValues[i]; retVal[i] = instabilityValues[i];
} }
return retVal; return retVal;
} else if(methodName.equals("release")) {
boolean doRelease = false;
if (arguments.length > 0) {
try {
doRelease = toBool(arguments[0]);
} catch(Exception e) {
throw new Exception("Function expects an boolean value");
}
releaseMode = doRelease ? MODE_MANUAL_RELEASE : MODE_DONT_RELEASE;
releaseAbove = 0;
releaseRate = 0;
}
return new Object[] { releaseMode != MODE_DONT_RELEASE };
} else if(methodName.equals("releaseRate")) {
int rate = -1;
try {
rate = toInt(arguments[0]);
} catch(Exception e) {
throw new Exception("Function expects an integer value");
}
if (rate <= 0) { } else if (methodName.equals("release")) {
releaseMode = MODE_DONT_RELEASE; return release(arguments);
releaseRate = 0;
} else {
/* releaseAbove = (int)Math.ceil(Math.pow(rate, 1.0 / 0.6));
WarpDrive.debugPrint("releaseAbove " + releaseAbove);
releaseMode = MODE_RELEASE_ABOVE;/**/
// player has to adjust it
releaseRate = rate;
releaseMode = MODE_RELEASE_AT_RATE;
}
return new Object[] { MODE_STRING[releaseMode], releaseRate }; } else if (methodName.equals("releaseRate")) {
} else if(methodName.equals("releaseAbove")) { return releaseRate(arguments);
int above = -1;
try {
above = toInt(arguments[0]);
} catch(Exception e) {
throw new Exception("Function expects an integer value");
}
if (above <= 0) { } else if (methodName.equals("releaseAbove")) {
releaseMode = 0; return releaseAbove(arguments);
releaseAbove = MODE_DONT_RELEASE;
} else {
releaseMode = MODE_RELEASE_ABOVE;
releaseAbove = above;
}
return new Object[] { MODE_STRING[releaseMode], releaseAbove };
} else if (methodName.equals("help")) { } else if (methodName.equals("help")) {
return new Object[] { helpStr(arguments) }; return new Object[] { helpStr(arguments) };
} }
return null; return null;
} }
private void sendEvent(String eventName, Object[] arguments) {
// WarpDrive.debugPrint("" + this + " Sending event '" + eventName + "'");
Set<Integer> keys = connectedComputers.keySet();
for(Integer key:keys) {
IComputerAccess comp = connectedComputers.get(key);
comp.queueEvent(eventName, arguments);
}
}
// POWER INTERFACES // POWER INTERFACES
@Override @Override
public int getPotentialEnergyOutput() { public int getPotentialEnergyOutput() {

View file

@ -1,15 +1,20 @@
package cr0s.WarpDrive.machines; package cr0s.WarpDrive.machines;
import cpw.mods.fml.common.Optional;
import cr0s.WarpDrive.WarpDriveConfig; import cr0s.WarpDrive.WarpDriveConfig;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
public class TileEntityPowerStore extends WarpEnergyTE implements IPeripheral { public class TileEntityPowerStore extends WarpEnergyTE {
private String[] methodArray = {
"energy" public TileEntityPowerStore() {
}; super();
peripheralName = "warpdrivePowerStore";
methodsArray = new String[] {
"getEnergyLevel"
};
}
@Override @Override
public int getPotentialEnergyOutput() { public int getPotentialEnergyOutput() {
@ -36,22 +41,12 @@ public class TileEntityPowerStore extends WarpEnergyTE implements IPeripheral {
return true; return true;
} }
// ComputerCraft // ComputerCraft IPeripheral methods implementation
@Override
public String getType() {
return "warpdrivePowerStore";
}
@Override
public String[] getMethodNames() {
return methodArray;
}
@Override @Override
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception { public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception {
String methodName = methodArray[method]; String methodName = methodsArray[method];
if (methodName == "energy") { if (methodName == "getEnergyLevel") {
return getEnergyObject(); return getEnergyLevel();
} }
return null; return null;
} }
@ -65,9 +60,4 @@ public class TileEntityPowerStore extends WarpEnergyTE implements IPeripheral {
public void detach(IComputerAccess computer) { public void detach(IComputerAccess computer) {
// nothing to see here // nothing to see here
} }
@Override
public boolean equals(IPeripheral other) {
return this == other;
}
} }

View file

@ -21,9 +21,8 @@ import net.minecraft.util.DamageSource;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.peripheral.IPeripheral;
public class TileEntityTransporter extends WarpEnergyTE implements IPeripheral, IUpgradable public class TileEntityTransporter extends WarpEnergyTE implements IUpgradable
{ {
private double scanRange=2; private double scanRange=2;
@ -41,18 +40,22 @@ public class TileEntityTransporter extends WarpEnergyTE implements IPeripheral,
private TeleporterDamage teleDam = new TeleporterDamage("teleporter"); private TeleporterDamage teleDam = new TeleporterDamage("teleporter");
private String[] methodArray = { public TileEntityTransporter() {
super();
peripheralName = "transporter";
methodsArray = new String[] {
"source", "source",
"dest", "dest",
"lock", "lock",
"release", "release",
"lockStrength", "lockStrength",
"energize", "energize",
"energy", "getEnergyLevel",
"powerBoost", "powerBoost",
"energyCost", "energyCost",
"upgrades", "upgrades",
"help" }; "help" };
}
@Override @Override
public void updateEntity() { public void updateEntity() {
@ -67,12 +70,10 @@ public class TileEntityTransporter extends WarpEnergyTE implements IPeripheral,
} }
} }
// IPeripheral overrides // OpenComputer callback methods
@Override // FIXME: implement OpenComputers...
public String getType() {
return "transporter"; // ComputerCraft IPeripheral methods implementation
}
private static String helpStr(Object[] function) { private static String helpStr(Object[] function) {
if (function != null && function.length > 0) { if (function != null && function.length > 0) {
String fun = function[0].toString().toLowerCase(); String fun = function[0].toString().toLowerCase();
@ -102,17 +103,12 @@ public class TileEntityTransporter extends WarpEnergyTE implements IPeripheral,
return "energyCost(): returns the amount of energy it will take for a single entity to transport with the current settings"; return "energyCost(): returns the amount of energy it will take for a single entity to transport with the current settings";
} else if(fun.equals("upgrades")) { } else if(fun.equals("upgrades")) {
return WarpDrive.defUpgradeStr; return WarpDrive.defUpgradeStr;
} else if(fun.equals("energy")) { } else if(fun.equals("getEnergyLevel")) {
return WarpDrive.defEnergyStr; return WarpDrive.defEnergyStr;
} }
} }
return WarpDrive.defHelpStr; return WarpDrive.defHelpStr;
} }
@Override
public String[] getMethodNames() {
return methodArray;
}
private Object[] setVec3(boolean src,Object... arguments) { private Object[] setVec3(boolean src,Object... arguments) {
Vector3 vec = src ? sourceVec : destVec; Vector3 vec = src ? sourceVec : destVec;
@ -152,8 +148,8 @@ public class TileEntityTransporter extends WarpEnergyTE implements IPeripheral,
@Override @Override
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception { public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception {
String methodName = methodArray[method]; String methodName = methodsArray[method];
if (methodName.equals("energy")) { if (methodName.equals("getEnergyLevel")) {
return new Object[] { getEnergyStored(), getMaxEnergyStored() }; return new Object[] { getEnergyStored(), getMaxEnergyStored() };
} else if (methodName.equals("source")) { } else if (methodName.equals("source")) {
return setVec3(true,arguments); return setVec3(true,arguments);
@ -396,12 +392,6 @@ public class TileEntityTransporter extends WarpEnergyTE implements IPeripheral,
} }
return output; return output;
} }
@Override
public void attach(IComputerAccess computer) {}
@Override
public void detach(IComputerAccess computer) {}
@Override @Override
public int getMaxEnergyStored() { public int getMaxEnergyStored() {
@ -456,11 +446,6 @@ public class TileEntityTransporter extends WarpEnergyTE implements IPeripheral,
} }
} }
@Override
public boolean equals(IPeripheral other) {
return other == this;
}
@Override @Override
public boolean takeUpgrade(EnumUpgradeTypes upgradeType, boolean simulate) public boolean takeUpgrade(EnumUpgradeTypes upgradeType, boolean simulate)
{ {