Permalink
Browse files

Do not instantiate ServerComputer instances in the peripheral provider

Instead we create a ComputerProxy, which delegates methods to the
ServerComputer or TileComputerBase, depending on which one exists.
  • Loading branch information...
SquidDev committed Sep 15, 2017
1 parent 7a916ed commit 245085f78772dc89161b807ab03972af5fac1005
@@ -10,17 +10,16 @@
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.shared.computer.core.ServerComputer;
import javax.annotation.Nonnull;
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;
@@ -0,0 +1,87 @@
package dan200.computercraft.shared.computer.blocks;
import dan200.computercraft.shared.computer.core.IComputer;
/**
* A proxy object for computer objects, delegating to {@link IComputer} or {@link TileComputer} where appropriate.
*/
public abstract class ComputerProxy
{
protected abstract TileComputerBase getTile();
public void turnOn()
{
TileComputerBase tile = getTile();
IComputer computer = tile.getComputer();
if( computer == null )
{
tile.m_startOn = true;
}
else
{
computer.turnOn();
}
}
public void shutdown()
{
TileComputerBase tile = getTile();
IComputer computer = tile.getComputer();
if( computer == null )
{
tile.m_startOn = false;
}
else
{
computer.shutdown();
}
}
public void reboot()
{
TileComputerBase tile = getTile();
IComputer computer = tile.getComputer();
if( computer == null )
{
tile.m_startOn = true;
}
else
{
computer.reboot();
}
}
public int assignID()
{
TileComputerBase tile = getTile();
IComputer computer = tile.getComputer();
if( computer == null )
{
return tile.m_computerID;
}
else
{
return computer.getID();
}
}
public boolean isOn()
{
IComputer computer = getTile().getComputer();
return computer != null && computer.isOn();
}
public String getLabel()
{
TileComputerBase tile = getTile();
IComputer computer = tile.getComputer();
if( computer == null )
{
return tile.m_label;
}
else
{
return computer.getLabel();
}
}
}
@@ -19,14 +19,14 @@
import net.minecraft.util.NonNullList;
import javax.annotation.Nonnull;
import java.util.List;
public class TileComputer extends TileComputerBase
{
// Statics
// Members
private ComputerProxy m_proxy;
public TileComputer()
{
}
@@ -48,6 +48,23 @@ protected ServerComputer createComputer( int instanceID, int id )
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<ItemStack> drops, boolean creative )
{
@@ -386,6 +386,8 @@ public void updateOutput()
protected abstract ServerComputer createComputer( int instanceID, int id );
public abstract ComputerProxy createProxy();
// ITerminalTile
@Override
@@ -45,12 +45,12 @@ public IPeripheral getPeripheral( @Nonnull World world, @Nonnull BlockPos pos, @
{
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() );
}
}
}
@@ -10,6 +10,7 @@
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 javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import static net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY;
@@ -114,6 +114,12 @@ protected ServerComputer createComputer( int instanceID, int id )
return createComputer( instanceID, id, ComputerCraft.terminalWidth_turtle, ComputerCraft.terminalHeight_turtle );
}
@Override
public ComputerProxy createProxy()
{
return m_brain.getProxy();
}
@Override
public void destroy()
{
@@ -12,6 +12,8 @@
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 static void cleanupBrains()
private static final int ANIM_DURATION = 8;
private TileTurtle m_owner;
private ComputerProxy m_proxy;
private LinkedList<TurtleCommandQueueEntry> m_commandQueue;
private int m_commandsIssued;
@@ -169,6 +172,21 @@ public TileTurtle getOwner()
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();

0 comments on commit 245085f

Please sign in to comment.