diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerPeripheral.java b/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerPeripheral.java index c48c280dc..3b5469c1b 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerPeripheral.java @@ -9,7 +9,6 @@ package dan200.computercraft.shared.computer.blocks; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IPeripheral; -import dan200.computercraft.shared.computer.core.ServerComputer; import javax.annotation.Nonnull; @@ -17,9 +16,9 @@ public class ComputerPeripheral implements IPeripheral { private final String m_type; - private final ServerComputer m_computer; + private final ComputerProxy m_computer; - public ComputerPeripheral( String type, ServerComputer computer ) + public ComputerPeripheral( String type, ComputerProxy computer ) { m_type = type; m_computer = computer; diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerProxy.java b/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerProxy.java new file mode 100644 index 000000000..c9c2a7bca --- /dev/null +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerProxy.java @@ -0,0 +1,87 @@ +package dan200.computercraft.shared.computer.blocks; + +import dan200.computercraft.shared.computer.core.ServerComputer; + +/** + * A proxy object for computer objects, delegating to {@link ServerComputer} or {@link TileComputer} where appropriate. + */ +public abstract class ComputerProxy +{ + protected abstract TileComputerBase getTile(); + + public void turnOn() + { + TileComputerBase tile = getTile(); + ServerComputer computer = tile.getServerComputer(); + if( computer == null ) + { + tile.m_startOn = true; + } + else + { + computer.turnOn(); + } + } + + public void shutdown() + { + TileComputerBase tile = getTile(); + ServerComputer computer = tile.getServerComputer(); + if( computer == null ) + { + tile.m_startOn = false; + } + else + { + computer.shutdown(); + } + } + + public void reboot() + { + TileComputerBase tile = getTile(); + ServerComputer computer = tile.getServerComputer(); + if( computer == null ) + { + tile.m_startOn = true; + } + else + { + computer.reboot(); + } + } + + public int assignID() + { + TileComputerBase tile = getTile(); + ServerComputer computer = tile.getServerComputer(); + if( computer == null ) + { + return tile.m_computerID; + } + else + { + return computer.getID(); + } + } + + public boolean isOn() + { + ServerComputer computer = getTile().getServerComputer(); + return computer != null && computer.isOn(); + } + + public String getLabel() + { + TileComputerBase tile = getTile(); + ServerComputer computer = tile.getServerComputer(); + if( computer == null ) + { + return tile.m_label; + } + else + { + return computer.getLabel(); + } + } +} diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java index e8f61c79b..acb91567e 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java @@ -25,7 +25,8 @@ public class TileComputer extends TileComputerBase // Statics // Members - + private ComputerProxy m_proxy; + public TileComputer() { } @@ -47,6 +48,23 @@ public class TileComputer extends TileComputerBase return computer; } + @Override + public ComputerProxy createProxy() + { + if( m_proxy == null ) + { + m_proxy = new ComputerProxy() + { + @Override + protected TileComputerBase getTile() + { + return TileComputer.this; + } + }; + } + return m_proxy; + } + @Override public void getDroppedItems( @Nonnull NonNullList drops, boolean creative ) { diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java index f24988f27..2a3c41ced 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java @@ -389,6 +389,8 @@ public abstract class TileComputerBase extends TileGeneric protected abstract ServerComputer createComputer( int instanceID, int id ); + public abstract ComputerProxy createProxy(); + // ITerminalTile @Override diff --git a/src/main/java/dan200/computercraft/shared/peripheral/common/DefaultPeripheralProvider.java b/src/main/java/dan200/computercraft/shared/peripheral/common/DefaultPeripheralProvider.java index d69a5ca55..e69c3c0f3 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/DefaultPeripheralProvider.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/DefaultPeripheralProvider.java @@ -45,12 +45,12 @@ public class DefaultPeripheralProvider implements IPeripheralProvider { if( !((TileTurtle)tile).hasMoved() ) { - return new ComputerPeripheral( "turtle", computerTile.createServerComputer() ); + return new ComputerPeripheral( "turtle", computerTile.createProxy() ); } } else { - return new ComputerPeripheral( "computer", computerTile.createServerComputer() ); + return new ComputerPeripheral( "computer", computerTile.createProxy() ); } } } diff --git a/src/main/java/dan200/computercraft/shared/pocket/items/ItemPocketComputer.java b/src/main/java/dan200/computercraft/shared/pocket/items/ItemPocketComputer.java index c998e1dae..2089baf74 100644 --- a/src/main/java/dan200/computercraft/shared/pocket/items/ItemPocketComputer.java +++ b/src/main/java/dan200/computercraft/shared/pocket/items/ItemPocketComputer.java @@ -402,10 +402,10 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia, I @Override public IMount createDataMount( @Nonnull ItemStack stack, @Nonnull World world ) { - ServerComputer computer = createServerComputer( world, null, null, stack ); - if( computer != null ) + int id = getComputerID( stack ); + if( id >= 0 ) { - return computer.getRootMount(); + return ComputerCraft.createSaveDirMount( world, "computer/" + id, ComputerCraft.computerSpaceLimit ); } return null; } diff --git a/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java b/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java index 0742ce890..34d5d2e4c 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java +++ b/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java @@ -10,6 +10,7 @@ import dan200.computercraft.ComputerCraft; import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.api.turtle.ITurtleUpgrade; import dan200.computercraft.api.turtle.TurtleSide; +import dan200.computercraft.shared.computer.blocks.ComputerProxy; import dan200.computercraft.shared.computer.blocks.TileComputerBase; import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.IComputer; @@ -43,7 +44,6 @@ import net.minecraftforge.items.wrapper.InvWrapper; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.List; import static net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY; @@ -114,6 +114,12 @@ public class TileTurtle extends TileComputerBase return createComputer( instanceID, id, ComputerCraft.terminalWidth_turtle, ComputerCraft.terminalHeight_turtle ); } + @Override + public ComputerProxy createProxy() + { + return m_brain.getProxy(); + } + @Override public void destroy() { diff --git a/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java b/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java index 887feab0e..9ab2e0b31 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java +++ b/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java @@ -12,6 +12,8 @@ import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.turtle.*; +import dan200.computercraft.shared.computer.blocks.ComputerProxy; +import dan200.computercraft.shared.computer.blocks.TileComputerBase; import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.IComputer; import dan200.computercraft.shared.computer.core.ServerComputer; @@ -104,6 +106,7 @@ public class TurtleBrain implements ITurtleAccess private static final int ANIM_DURATION = 8; private TileTurtle m_owner; + private ComputerProxy m_proxy; private LinkedList m_commandQueue; private int m_commandsIssued; @@ -169,6 +172,21 @@ public class TurtleBrain implements ITurtleAccess return m_owner; } + public ComputerProxy getProxy() + { + if(m_proxy == null) { + m_proxy = new ComputerProxy() + { + @Override + protected TileComputerBase getTile() + { + return m_owner; + } + }; + } + return m_proxy; + } + public ComputerFamily getFamily() { return m_owner.getFamily();