Forgot those 2 in previous commit...

This commit is contained in:
LemADEC 2015-08-10 03:41:52 +02:00
parent 5d60b93950
commit 3efb18741b
2 changed files with 61 additions and 28 deletions

View file

@ -1,5 +1,7 @@
package cr0s.warpdrive.machines;
import java.util.Arrays;
import li.cil.oc.api.machine.Arguments;
import li.cil.oc.api.machine.Callback;
import li.cil.oc.api.machine.Context;
@ -14,7 +16,6 @@ import cr0s.warpdrive.conf.WarpDriveConfig;
import cr0s.warpdrive.data.CloakedArea;
import cr0s.warpdrive.data.Vector3;
import cr0s.warpdrive.network.PacketHandler;
import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.peripheral.IComputerAccess;
@ -61,11 +62,13 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE {
"getEnergyLevel",
"enable" // set field enable state (true or false), return true if enabled
};
CC_scripts = Arrays.asList("cloak1", "cloak2", "uncloak");
}
@Override
public void updateEntity() {
super.updateEntity();
if (FMLCommonHandler.instance().getEffectiveSide().isClient()) {
return;
}
@ -365,21 +368,6 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE {
}
// ComputerCraft IPeripheral methods implementation
@Override
@Optional.Method(modid = "ComputerCraft")
public void attach(IComputerAccess computer) {
super.attach(computer);
if (WarpDriveConfig.G_LUA_SCRIPTS != WarpDriveConfig.LUA_SCRIPTS_NONE) {
computer.mount("/cloakingcore", ComputerCraftAPI.createResourceMount(WarpDrive.class, WarpDrive.MODID.toLowerCase(), "lua/warpdriveCloakingCore"));
computer.mount("/warpupdater", ComputerCraftAPI.createResourceMount(WarpDrive.class, WarpDrive.MODID.toLowerCase(), "lua/common/updater"));
if (WarpDriveConfig.G_LUA_SCRIPTS == WarpDriveConfig.LUA_SCRIPTS_ALL) {
computer.mount("/uncloak", ComputerCraftAPI.createResourceMount(WarpDrive.class, WarpDrive.MODID.toLowerCase(), "lua/warpdriveCloakingCore/uncloak"));
computer.mount("/cloak1", ComputerCraftAPI.createResourceMount(WarpDrive.class, WarpDrive.MODID.toLowerCase(), "lua/warpdriveCloakingCore/cloak1"));
computer.mount("/cloak2", ComputerCraftAPI.createResourceMount(WarpDrive.class, WarpDrive.MODID.toLowerCase(), "lua/warpdriveCloakingCore/cloak2"));
}
}
}
@Override
@Optional.Method(modid = "ComputerCraft")
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) {

View file

@ -1,20 +1,26 @@
package cr0s.warpdrive.machines;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import li.cil.oc.api.FileSystem;
import li.cil.oc.api.Network;
import li.cil.oc.api.machine.Arguments;
import li.cil.oc.api.machine.Callback;
import li.cil.oc.api.machine.Context;
import li.cil.oc.api.network.Environment;
import li.cil.oc.api.network.ManagedEnvironment;
import li.cil.oc.api.network.Message;
import li.cil.oc.api.network.Node;
import li.cil.oc.api.network.Visibility;
import net.minecraft.nbt.NBTTagCompound;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.Optional;
import cr0s.warpdrive.WarpDrive;
import cr0s.warpdrive.conf.WarpDriveConfig;
import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
@ -31,6 +37,12 @@ public abstract class WarpInterfacedTE extends WarpTE implements IPeripheral, En
protected String peripheralName = null;
protected String[] methodsArray = {};
// pre-loaded scripts support
private volatile ManagedEnvironment OC_fileSystem = null;
private volatile boolean CC_hasResource = false;
private volatile boolean OC_hasResource = false;
protected volatile List<String> CC_scripts = null;
// OpenComputer specific properties
protected Node OC_node = null;
protected boolean OC_addedToNetwork = false;
@ -38,11 +50,29 @@ public abstract class WarpInterfacedTE extends WarpTE implements IPeripheral, En
// ComputerCraft specific properties
protected HashMap<Integer, IComputerAccess> connectedComputers = new HashMap<Integer, IComputerAccess>();
private boolean assetExist(final String resourcePath) {
URL url = getClass().getResource(resourcePath);
return (url != null);
}
// TileEntity overrides, notably for OpenComputer
@Override
public void updateEntity() {
super.updateEntity();
if (interfacedFirstTick) {
WarpDrive.logger.info("interfacedFirstTick " + peripheralName);
String CC_path = "/assets/" + WarpDrive.MODID.toLowerCase() + "/lua.ComputerCraft/" + peripheralName;
String OC_path = "/assets/" + WarpDrive.MODID.toLowerCase() + "/lua.OpenComputers/" + peripheralName;
if (WarpDriveConfig.isCCLoaded) {
CC_hasResource = assetExist(CC_path);
WarpDrive.logger.info("CC_hasResource " + CC_hasResource);
}
if (Loader.isModLoaded("OpenComputers")) {
OC_hasResource = assetExist(OC_path);
WarpDrive.logger.info("OC_hasResource " + OC_hasResource);
}
// deferred constructor so the derived class can finish it's initialization first
if (Loader.isModLoaded("OpenComputers")) {
OC_constructor();
@ -145,6 +175,15 @@ public abstract class WarpInterfacedTE extends WarpTE implements IPeripheral, En
public void attach(IComputerAccess computer) {
int id = computer.getID();
connectedComputers.put(id, computer);
if (CC_hasResource && WarpDriveConfig.G_LUA_SCRIPTS != WarpDriveConfig.LUA_SCRIPTS_NONE) {
computer.mount("/" + peripheralName, ComputerCraftAPI.createResourceMount(WarpDrive.class, WarpDrive.MODID.toLowerCase(), "lua.ComputerCraft/" + peripheralName));
computer.mount("/warpupdater", ComputerCraftAPI.createResourceMount(WarpDrive.class, WarpDrive.MODID.toLowerCase(), "lua.ComputerCraft/common/updater"));
if (WarpDriveConfig.G_LUA_SCRIPTS == WarpDriveConfig.LUA_SCRIPTS_ALL) {
for(String script : CC_scripts) {
computer.mount("/" + script, ComputerCraftAPI.createResourceMount(WarpDrive.class, WarpDrive.MODID.toLowerCase(), "lua.ComputerCraft/" + peripheralName + "/" + script));
}
}
}
}
@Override
@ -179,13 +218,15 @@ public abstract class WarpInterfacedTE extends WarpTE implements IPeripheral, En
@Callback
@Optional.Method(modid = "OpenComputers")
public Object[] interfaced(Context context, Arguments arguments) {
return new String[] { "This is a WarpDrive interfaced tile entity." };
return new String[] { "This is a WarpDrive computer interfaced tile entity." };
}
@Optional.Method(modid = "OpenComputers")
public void OC_constructor() {
private void OC_constructor() {
OC_node = Network.newNode(this, Visibility.Network).withComponent(peripheralName).create();
// ManagedEnvironment fileSystem = FileSystem.asManagedEnvironment(FileSystem.fromClass(getClass(), WarpDrive.MODID, "lua"), "my_files");
if (OC_hasResource && WarpDriveConfig.G_LUA_SCRIPTS != WarpDriveConfig.LUA_SCRIPTS_NONE) {
OC_fileSystem = FileSystem.asManagedEnvironment(FileSystem.fromClass(getClass(), WarpDrive.MODID.toLowerCase(), "lua.OpenComputers/" + peripheralName), peripheralName);
}
}
@Override
@ -202,19 +243,23 @@ public abstract class WarpInterfacedTE extends WarpTE implements IPeripheral, En
// Note that this is also called for all already present computers
// when we're added to an already existing network, so we don't
// have to loop through the existing nodes manually.
// node.connect(fileSystem);
if (OC_fileSystem != null) {
node.connect(OC_fileSystem.node());
}
}
}
@Override
@Optional.Method(modid = "OpenComputers")
public void onDisconnect(Node node) {
if (node.host() instanceof Context) {
// Disconnecting from a single computer
// node.disconnect(fileSystem);
} else if (node == OC_node) {
// Disconnecting from the network
// fileSystem.node.remove();
if (OC_fileSystem != null) {
if (node.host() instanceof Context) {
// Disconnecting from a single computer
node.disconnect(OC_fileSystem.node());
} else if (node == OC_node) {
// Disconnecting from the network
OC_fileSystem.node().remove();
}
}
}