1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-15 11:45:42 +00:00

Merge pull request #454 from SquidDev-CC/ComputerCraft/hotfix/lazy-computer-peripheral

[WIP] Only instantiate ServerComputer on tile ticks
This commit is contained in:
SquidDev 2017-11-15 11:42:54 +00:00
commit 0caa133089
8 changed files with 140 additions and 10 deletions

View File

@ -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;

View File

@ -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();
}
}
}

View File

@ -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<ItemStack> drops, boolean creative )
{

View File

@ -389,6 +389,8 @@ public abstract class TileComputerBase extends TileGeneric
protected abstract ServerComputer createComputer( int instanceID, int id );
public abstract ComputerProxy createProxy();
// ITerminalTile
@Override

View File

@ -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() );
}
}
}

View File

@ -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;
}

View File

@ -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()
{

View File

@ -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<TurtleCommandQueueEntry> 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();