Integrated Computer interface upgrades
Fixed IPeripheralProvider overriding other mods Added CC-Tweak compatibility for 1.12.2 Improved LUA logging
This commit is contained in:
parent
9ad703fdf5
commit
f058f72c0a
3 changed files with 103 additions and 34 deletions
|
@ -1,5 +1,7 @@
|
||||||
package cr0s.warpdrive;
|
package cr0s.warpdrive;
|
||||||
|
|
||||||
|
import cr0s.warpdrive.block.TileEntityAbstractInterfaced;
|
||||||
|
import cr0s.warpdrive.config.WarpDriveConfig;
|
||||||
import dan200.computercraft.api.ComputerCraftAPI;
|
import dan200.computercraft.api.ComputerCraftAPI;
|
||||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||||
import dan200.computercraft.api.peripheral.IPeripheralProvider;
|
import dan200.computercraft.api.peripheral.IPeripheralProvider;
|
||||||
|
@ -16,6 +18,7 @@ import net.minecraft.world.World;
|
||||||
@Optional.Interface(iface = "dan200.computercraft.api.peripheral.IPeripheralProvider", modid = "computercraft")
|
@Optional.Interface(iface = "dan200.computercraft.api.peripheral.IPeripheralProvider", modid = "computercraft")
|
||||||
})
|
})
|
||||||
public class WarpDrivePeripheralHandler implements IPeripheralProvider {
|
public class WarpDrivePeripheralHandler implements IPeripheralProvider {
|
||||||
|
|
||||||
public void register() {
|
public void register() {
|
||||||
ComputerCraftAPI.registerPeripheralProvider(this);
|
ComputerCraftAPI.registerPeripheralProvider(this);
|
||||||
}
|
}
|
||||||
|
@ -23,8 +26,13 @@ public class WarpDrivePeripheralHandler implements IPeripheralProvider {
|
||||||
@Override
|
@Override
|
||||||
@Optional.Method(modid = "computercraft")
|
@Optional.Method(modid = "computercraft")
|
||||||
public IPeripheral getPeripheral(@Nonnull final World world, @Nonnull final BlockPos blockPos, @Nonnull final EnumFacing side) {
|
public IPeripheral getPeripheral(@Nonnull final World world, @Nonnull final BlockPos blockPos, @Nonnull final EnumFacing side) {
|
||||||
|
// ensure we only cover our own blocks
|
||||||
final TileEntity tileEntity = world.getTileEntity(new BlockPos(blockPos));
|
final TileEntity tileEntity = world.getTileEntity(new BlockPos(blockPos));
|
||||||
if (tileEntity instanceof IPeripheral) {
|
if (tileEntity instanceof TileEntityAbstractInterfaced) {
|
||||||
|
if (WarpDriveConfig.LOGGING_LUA) {
|
||||||
|
WarpDrive.logger.info(String.format("[CC] IPeripheralProvider.getPeripheral %s %s %s",
|
||||||
|
Commons.format(world, blockPos), side, tileEntity ));
|
||||||
|
}
|
||||||
return (IPeripheral) tileEntity;
|
return (IPeripheral) tileEntity;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -31,7 +31,9 @@ import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map.Entry;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.CopyOnWriteArraySet;
|
import java.util.concurrent.CopyOnWriteArraySet;
|
||||||
|
|
||||||
import net.minecraft.nbt.NBTTagCompound;
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
|
@ -39,7 +41,7 @@ import net.minecraft.nbt.NBTTagCompound;
|
||||||
import net.minecraftforge.fml.common.FMLCommonHandler;
|
import net.minecraftforge.fml.common.FMLCommonHandler;
|
||||||
import net.minecraftforge.fml.common.Optional;
|
import net.minecraftforge.fml.common.Optional;
|
||||||
|
|
||||||
// OpenComputer API: https://github.com/MightyPirates/OpenComputers/tree/master-MC1.7.10/src/main/java/li/cil/oc/api
|
// OpenComputers API: https://github.com/MightyPirates/OpenComputers/tree/master-MC1.7.10/src/main/java/li/cil/oc/api
|
||||||
|
|
||||||
@Optional.InterfaceList({
|
@Optional.InterfaceList({
|
||||||
@Optional.Interface(iface = "li.cil.oc.api.network.Environment", modid = "opencomputers"),
|
@Optional.Interface(iface = "li.cil.oc.api.network.Environment", modid = "opencomputers"),
|
||||||
|
@ -47,6 +49,9 @@ import net.minecraftforge.fml.common.Optional;
|
||||||
})
|
})
|
||||||
public abstract class TileEntityAbstractInterfaced extends TileEntityAbstractBase implements IPeripheral, Environment, cr0s.warpdrive.api.computer.IInterfaced {
|
public abstract class TileEntityAbstractInterfaced extends TileEntityAbstractBase implements IPeripheral, Environment, cr0s.warpdrive.api.computer.IInterfaced {
|
||||||
|
|
||||||
|
// global storage
|
||||||
|
private static final ConcurrentHashMap<String, Object> CC_mountGlobals = new ConcurrentHashMap<>(32);
|
||||||
|
|
||||||
// Common computer properties
|
// Common computer properties
|
||||||
protected String peripheralName = null;
|
protected String peripheralName = null;
|
||||||
private String[] methodsArray = {};
|
private String[] methodsArray = {};
|
||||||
|
@ -66,7 +71,7 @@ public abstract class TileEntityAbstractInterfaced extends TileEntityAbstractBas
|
||||||
private boolean OC_addedToNetwork = false;
|
private boolean OC_addedToNetwork = false;
|
||||||
|
|
||||||
// ComputerCraft specific properties
|
// ComputerCraft specific properties
|
||||||
private final CopyOnWriteArraySet<IComputerAccess> CC_connectedComputers = new CopyOnWriteArraySet<>();
|
private final ConcurrentHashMap<IComputerAccess, CopyOnWriteArraySet<String>> CC_connectedComputers = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public TileEntityAbstractInterfaced() {
|
public TileEntityAbstractInterfaced() {
|
||||||
super();
|
super();
|
||||||
|
@ -251,8 +256,9 @@ public abstract class TileEntityAbstractInterfaced extends TileEntityAbstractBas
|
||||||
}
|
}
|
||||||
if (WarpDriveConfig.LOGGING_LUA) {
|
if (WarpDriveConfig.LOGGING_LUA) {
|
||||||
final String methodName = Commons.getMethodName(1);
|
final String methodName = Commons.getMethodName(1);
|
||||||
WarpDrive.logger.info(String.format("[OC] LUA call from %s %s to %s.%s(%s)",
|
WarpDrive.logger.info(String.format("[OC] LUA call %s from %s to %s.%s(%s)",
|
||||||
context.node().address(), Commons.format(world, pos),
|
Commons.format(world, pos),
|
||||||
|
context.node().address(),
|
||||||
peripheralName, methodName, Commons.format(arguments)));
|
peripheralName, methodName, Commons.format(arguments)));
|
||||||
}
|
}
|
||||||
if (!isInterfaceEnabled()) {
|
if (!isInterfaceEnabled()) {
|
||||||
|
@ -265,8 +271,9 @@ public abstract class TileEntityAbstractInterfaced extends TileEntityAbstractBas
|
||||||
private String CC_getMethodNameAndLogCall(@Nonnull final IComputerAccess computerAccess, final int methodIndex, @Nonnull final Object[] arguments) {
|
private String CC_getMethodNameAndLogCall(@Nonnull final IComputerAccess computerAccess, final int methodIndex, @Nonnull final Object[] arguments) {
|
||||||
final String methodName = methodsArray[methodIndex];
|
final String methodName = methodsArray[methodIndex];
|
||||||
if (WarpDriveConfig.LOGGING_LUA) {
|
if (WarpDriveConfig.LOGGING_LUA) {
|
||||||
WarpDrive.logger.info(String.format("[CC] LUA call from %d:%s %s to %s.%s(%s)",
|
WarpDrive.logger.info(String.format("[CC] LUA call %s from %d:%s to %s.%s(%s)",
|
||||||
computerAccess.getID(), computerAccess.getAttachmentName(), Commons.format(world, pos),
|
Commons.format(world, pos),
|
||||||
|
computerAccess.getID(), computerAccess.getAttachmentName(),
|
||||||
peripheralName, methodName, Commons.format(arguments)));
|
peripheralName, methodName, Commons.format(arguments)));
|
||||||
}
|
}
|
||||||
if (!isInterfaceEnabled() && !methodName.equals("isInterfaced")) {
|
if (!isInterfaceEnabled() && !methodName.equals("isInterfaced")) {
|
||||||
|
@ -448,31 +455,43 @@ public abstract class TileEntityAbstractInterfaced extends TileEntityAbstractBas
|
||||||
@Override
|
@Override
|
||||||
@Optional.Method(modid = "computercraft")
|
@Optional.Method(modid = "computercraft")
|
||||||
public void attach(@Nonnull final IComputerAccess computerAccess) {
|
public void attach(@Nonnull final IComputerAccess computerAccess) {
|
||||||
CC_connectedComputers.add(computerAccess);
|
if (CC_connectedComputers.containsKey(computerAccess)) {
|
||||||
|
WarpDrive.logger.error(String.format("[CC] Already attached %s %s with %d:%s, ignoring...",
|
||||||
|
peripheralName, Commons.format(world, pos),
|
||||||
|
computerAccess.getID(), computerAccess.getAttachmentName() ));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (WarpDriveConfig.LOGGING_LUA) {
|
||||||
|
WarpDrive.logger.info(String.format("[CC] Attaching %s %s with %d:%s",
|
||||||
|
peripheralName, Commons.format(world, pos),
|
||||||
|
computerAccess.getID(), computerAccess.getAttachmentName() ));
|
||||||
|
}
|
||||||
|
final CopyOnWriteArraySet<String> mountedLocations = new CopyOnWriteArraySet<>();
|
||||||
|
CC_connectedComputers.put(computerAccess, mountedLocations);
|
||||||
if (isInterfaceEnabled()) {
|
if (isInterfaceEnabled()) {
|
||||||
CC_mount(computerAccess);
|
CC_mount(computerAccess, mountedLocations);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Optional.Method(modid = "computercraft")
|
@Optional.Method(modid = "computercraft")
|
||||||
private void CC_mount() {
|
private void CC_mount() {
|
||||||
for (final IComputerAccess computerAccess : CC_connectedComputers) {
|
for (final Entry<IComputerAccess, CopyOnWriteArraySet<String>> entry : CC_connectedComputers.entrySet()) {
|
||||||
CC_mount(computerAccess);
|
CC_mount(entry.getKey(), entry.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Optional.Method(modid = "computercraft")
|
@Optional.Method(modid = "computercraft")
|
||||||
private void CC_mount(@Nonnull final IComputerAccess computer) {
|
private void CC_mount(@Nonnull final IComputerAccess computerAccess, @Nonnull final CopyOnWriteArraySet<String> mountedLocations) {
|
||||||
if (CC_hasResource && WarpDriveConfig.G_LUA_SCRIPTS != WarpDriveConfig.LUA_SCRIPTS_NONE) {
|
if (CC_hasResource && WarpDriveConfig.G_LUA_SCRIPTS != WarpDriveConfig.LUA_SCRIPTS_NONE) {
|
||||||
try {
|
try {
|
||||||
CC_mount(computer, "lua.ComputerCraft/common", "/" + WarpDrive.MODID);
|
CC_mount(computerAccess, mountedLocations, "lua.ComputerCraft/common", "/" + WarpDrive.MODID);
|
||||||
|
|
||||||
final String folderPeripheral = peripheralName.replace(WarpDrive.MODID, WarpDrive.MODID + "/");
|
final String folderPeripheral = peripheralName.replace(WarpDrive.MODID, WarpDrive.MODID + "/");
|
||||||
CC_mount(computer, "lua.ComputerCraft/" + peripheralName, "/" + folderPeripheral);
|
CC_mount(computerAccess, mountedLocations, "lua.ComputerCraft/" + peripheralName, "/" + folderPeripheral);
|
||||||
|
|
||||||
if (WarpDriveConfig.G_LUA_SCRIPTS == WarpDriveConfig.LUA_SCRIPTS_ALL) {
|
if (WarpDriveConfig.G_LUA_SCRIPTS == WarpDriveConfig.LUA_SCRIPTS_ALL) {
|
||||||
for (final String script : CC_scripts) {
|
for (final String script : CC_scripts) {
|
||||||
CC_mount(computer, "lua.ComputerCraft/" + peripheralName + "/" + script, "/" + script);
|
CC_mount(computerAccess, mountedLocations, "lua.ComputerCraft/" + peripheralName + "/" + script, "/" + script);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (final Exception exception) {
|
} catch (final Exception exception) {
|
||||||
|
@ -486,30 +505,51 @@ public abstract class TileEntityAbstractInterfaced extends TileEntityAbstractBas
|
||||||
}
|
}
|
||||||
|
|
||||||
@Optional.Method(modid = "computercraft")
|
@Optional.Method(modid = "computercraft")
|
||||||
private void CC_mount(@Nonnull final IComputerAccess computer, final String pathAsset, final String pathLUA) {
|
private void CC_mount(@Nonnull final IComputerAccess computerAccess, @Nonnull final CopyOnWriteArraySet<String> mountedLocations,
|
||||||
final IMount mountCommon = ComputerCraftAPI.createResourceMount(WarpDrive.class, WarpDrive.MODID, pathAsset);
|
@Nonnull final String pathAsset, @Nonnull final String pathLUA) {
|
||||||
assert mountCommon != null;
|
IMount mountCommon = (IMount) CC_mountGlobals.get(pathAsset);
|
||||||
computer.mount(pathLUA, mountCommon);
|
if (mountCommon == null) {
|
||||||
}
|
mountCommon = ComputerCraftAPI.createResourceMount(WarpDrive.class, WarpDrive.MODID, pathAsset);
|
||||||
|
assert mountCommon != null;
|
||||||
@Optional.Method(modid = "computercraft")
|
CC_mountGlobals.put(pathAsset, mountCommon);
|
||||||
private void CC_unmount() {
|
}
|
||||||
for (final IComputerAccess computerAccess : CC_connectedComputers) {
|
final String pathLUA_actual = computerAccess.mount(pathLUA, mountCommon);
|
||||||
CC_unmount(computerAccess);
|
if (WarpDriveConfig.LOGGING_LUA) {
|
||||||
|
WarpDrive.logger.info(String.format("[CC] %s Mounted %s to %s as %s",
|
||||||
|
Commons.format(world, pos),
|
||||||
|
pathAsset, pathLUA, pathLUA_actual));
|
||||||
|
}
|
||||||
|
if (pathLUA_actual != null) {
|
||||||
|
mountedLocations.add(pathLUA_actual);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Optional.Method(modid = "computercraft")
|
@Optional.Method(modid = "computercraft")
|
||||||
private void CC_unmount(@Nonnull final IComputerAccess computer) {
|
private void CC_unmount() {
|
||||||
|
for (final Entry<IComputerAccess, CopyOnWriteArraySet<String>> entry : CC_connectedComputers.entrySet()) {
|
||||||
|
CC_unmount(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Optional.Method(modid = "computercraft")
|
||||||
|
private void CC_unmount(@Nonnull final IComputerAccess computerAccess) {
|
||||||
|
final CopyOnWriteArraySet<String> mountedLocations = CC_connectedComputers.get(computerAccess);
|
||||||
|
if (mountedLocations != null) {
|
||||||
|
CC_unmount(computerAccess, mountedLocations);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Optional.Method(modid = "computercraft")
|
||||||
|
private void CC_unmount(@Nonnull final IComputerAccess computerAccess, @Nonnull final CopyOnWriteArraySet<String> mountedLocation) {
|
||||||
if (CC_hasResource && WarpDriveConfig.G_LUA_SCRIPTS != WarpDriveConfig.LUA_SCRIPTS_NONE) {
|
if (CC_hasResource && WarpDriveConfig.G_LUA_SCRIPTS != WarpDriveConfig.LUA_SCRIPTS_NONE) {
|
||||||
try {
|
try {
|
||||||
final String folderPeripheral = peripheralName.replace(WarpDrive.MODID, WarpDrive.MODID + "/");
|
for (final String pathLUA : mountedLocation) {
|
||||||
computer.unmount("/" + WarpDrive.MODID);
|
if (WarpDriveConfig.LOGGING_LUA) {
|
||||||
computer.unmount("/" + folderPeripheral);
|
WarpDrive.logger.info(String.format("[CC] %s Unmounting %s",
|
||||||
if (WarpDriveConfig.G_LUA_SCRIPTS == WarpDriveConfig.LUA_SCRIPTS_ALL) {
|
Commons.format(world, pos),
|
||||||
for (final String script : CC_scripts) {
|
pathLUA ));
|
||||||
computer.unmount("/" + script);
|
|
||||||
}
|
}
|
||||||
|
computerAccess.unmount(pathLUA);
|
||||||
}
|
}
|
||||||
} catch (final Exception exception) {
|
} catch (final Exception exception) {
|
||||||
exception.printStackTrace();
|
exception.printStackTrace();
|
||||||
|
@ -517,6 +557,8 @@ public abstract class TileEntityAbstractInterfaced extends TileEntityAbstractBas
|
||||||
peripheralName,
|
peripheralName,
|
||||||
Commons.format(world, pos),
|
Commons.format(world, pos),
|
||||||
isFirstTick()));
|
isFirstTick()));
|
||||||
|
} finally {
|
||||||
|
mountedLocation.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -524,6 +566,17 @@ public abstract class TileEntityAbstractInterfaced extends TileEntityAbstractBas
|
||||||
@Override
|
@Override
|
||||||
@Optional.Method(modid = "computercraft")
|
@Optional.Method(modid = "computercraft")
|
||||||
public void detach(@Nonnull final IComputerAccess computerAccess) {
|
public void detach(@Nonnull final IComputerAccess computerAccess) {
|
||||||
|
if (!CC_connectedComputers.containsKey(computerAccess)) {
|
||||||
|
WarpDrive.logger.error(String.format("[CC] Already detached %s %s from %d:%s, ignoring...",
|
||||||
|
peripheralName, Commons.format(world, pos),
|
||||||
|
computerAccess.getID(), computerAccess.getAttachmentName() ));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (WarpDriveConfig.LOGGING_LUA) {
|
||||||
|
WarpDrive.logger.info(String.format("[CC] Detaching %s %s from %d:%s",
|
||||||
|
peripheralName, Commons.format(world, pos),
|
||||||
|
computerAccess.getID(), computerAccess.getAttachmentName() ));
|
||||||
|
}
|
||||||
if (isInterfaceEnabled()) {
|
if (isInterfaceEnabled()) {
|
||||||
CC_unmount(computerAccess);
|
CC_unmount(computerAccess);
|
||||||
}
|
}
|
||||||
|
@ -545,7 +598,7 @@ public abstract class TileEntityAbstractInterfaced extends TileEntityAbstractBas
|
||||||
WarpDrive.logger.info(this + " Sending event '" + eventName + "'");
|
WarpDrive.logger.info(this + " Sending event '" + eventName + "'");
|
||||||
}
|
}
|
||||||
if (WarpDriveConfig.isComputerCraftLoaded) {
|
if (WarpDriveConfig.isComputerCraftLoaded) {
|
||||||
for (final IComputerAccess computerAccess : CC_connectedComputers) {
|
for (final IComputerAccess computerAccess : CC_connectedComputers.keySet()) {
|
||||||
computerAccess.queueEvent(eventName, arguments);
|
computerAccess.queueEvent(eventName, arguments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,15 @@ public class CompatComputerCraft implements IBlockTransformer {
|
||||||
try {
|
try {
|
||||||
classBlockDirectional = Class.forName("dan200.computercraft.shared.common.BlockDirectional");
|
classBlockDirectional = Class.forName("dan200.computercraft.shared.common.BlockDirectional");
|
||||||
classBlockComputerBase = Class.forName("dan200.computercraft.shared.computer.blocks.BlockComputerBase");
|
classBlockComputerBase = Class.forName("dan200.computercraft.shared.computer.blocks.BlockComputerBase");
|
||||||
classBlockCable = Class.forName("dan200.computercraft.shared.peripheral.common.BlockCable");
|
|
||||||
|
// BlockCable changed location between original and CC-Tweaked fork
|
||||||
|
try {
|
||||||
|
classBlockCable = Class.forName("dan200.computercraft.shared.peripheral.common.BlockCable");
|
||||||
|
} catch(final ClassNotFoundException exception) {
|
||||||
|
classBlockCable = Class.forName("dan200.computercraft.shared.peripheral.modem.wired.BlockCable");
|
||||||
|
WarpDrive.logger.info("CC-Tweaked detected...");
|
||||||
|
}
|
||||||
|
|
||||||
classBlockPeripheral = Class.forName("dan200.computercraft.shared.peripheral.common.BlockPeripheral");
|
classBlockPeripheral = Class.forName("dan200.computercraft.shared.peripheral.common.BlockPeripheral");
|
||||||
classBlockTurtle = Class.forName("dan200.computercraft.shared.turtle.blocks.BlockTurtle");
|
classBlockTurtle = Class.forName("dan200.computercraft.shared.turtle.blocks.BlockTurtle");
|
||||||
WarpDriveConfig.registerBlockTransformer("computercraft", new CompatComputerCraft());
|
WarpDriveConfig.registerBlockTransformer("computercraft", new CompatComputerCraft());
|
||||||
|
|
Loading…
Reference in a new issue