From cee60cdb5b11c6ba583a1a8206ee77cf6b70f139 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Fri, 21 Oct 2022 19:51:23 +0100 Subject: [PATCH] Require computers to have a fixed ID Moves ID assigning out of the Computer class and into wherever we construct the ServerComputer (so in computer blocks and pocket computer items). This is definitely not perfect - it'd be nice to make ServerComputers more responsible for managing the lifecycle of computers (so assigning ids, handling auto-starting, etc...), but I've not found a good way to handle this yet! --- .../computercraft/core/computer/Computer.java | 17 ++------------- .../core/computer/ComputerExecutor.java | 8 +------ .../core/computer/Environment.java | 2 +- .../core/computer/IComputerEnvironment.java | 6 +----- .../computer/blocks/ComputerPeripheral.java | 2 +- .../shared/computer/blocks/ComputerProxy.java | 2 +- .../computer/blocks/TileComputerBase.java | 13 +++++++++--- .../shared/computer/core/ServerComputer.java | 21 ++----------------- .../pocket/items/ItemPocketComputer.java | 3 ++- .../computercraft/shared/util/IDAssigner.java | 2 ++ .../core/computer/BasicEnvironment.java | 14 +------------ 11 files changed, 24 insertions(+), 66 deletions(-) diff --git a/src/main/java/dan200/computercraft/core/computer/Computer.java b/src/main/java/dan200/computercraft/core/computer/Computer.java index 9fc2f3dac..57ac8268e 100644 --- a/src/main/java/dan200/computercraft/core/computer/Computer.java +++ b/src/main/java/dan200/computercraft/core/computer/Computer.java @@ -32,7 +32,7 @@ public class Computer private static final int START_DELAY = 50; // Various properties of the computer - private int id; + private final int id; private String label = null; // Read-only fields about the computer @@ -51,6 +51,7 @@ public class Computer public Computer( IComputerEnvironment environment, Terminal terminal, int id ) { + if( id < 0 ) throw new IllegalStateException( "Id has not been assigned" ); this.id = id; this.environment = environment; this.terminal = terminal; @@ -135,20 +136,6 @@ public class Computer return id; } - public int assignID() - { - if( id < 0 ) - { - id = environment.assignNewID(); - } - return id; - } - - public void setID( int id ) - { - this.id = id; - } - public String getLabel() { return label; diff --git a/src/main/java/dan200/computercraft/core/computer/ComputerExecutor.java b/src/main/java/dan200/computercraft/core/computer/ComputerExecutor.java index bd7e5a81c..25c90703e 100644 --- a/src/main/java/dan200/computercraft/core/computer/ComputerExecutor.java +++ b/src/main/java/dan200/computercraft/core/computer/ComputerExecutor.java @@ -345,13 +345,7 @@ final class ComputerExecutor private IWritableMount getRootMount() { - if( rootMount == null ) - { - rootMount = computer.getComputerEnvironment().createSaveDirMount( - "computer/" + computer.assignID(), - computer.getComputerEnvironment().getComputerSpaceLimit() - ); - } + if( rootMount == null ) rootMount = computer.getComputerEnvironment().createRootMount(); return rootMount; } diff --git a/src/main/java/dan200/computercraft/core/computer/Environment.java b/src/main/java/dan200/computercraft/core/computer/Environment.java index 354f5233d..b96ebf8ae 100644 --- a/src/main/java/dan200/computercraft/core/computer/Environment.java +++ b/src/main/java/dan200/computercraft/core/computer/Environment.java @@ -68,7 +68,7 @@ public final class Environment implements IAPIEnvironment @Override public int getComputerID() { - return computer.assignID(); + return computer.getID(); } @Nonnull diff --git a/src/main/java/dan200/computercraft/core/computer/IComputerEnvironment.java b/src/main/java/dan200/computercraft/core/computer/IComputerEnvironment.java index 9ca82eb90..6e39eb557 100644 --- a/src/main/java/dan200/computercraft/core/computer/IComputerEnvironment.java +++ b/src/main/java/dan200/computercraft/core/computer/IComputerEnvironment.java @@ -18,18 +18,14 @@ public interface IComputerEnvironment double getTimeOfDay(); - long getComputerSpaceLimit(); - @Nonnull String getHostString(); @Nonnull String getUserAgent(); - int assignNewID(); - @Nullable - IWritableMount createSaveDirMount( String subPath, long capacity ); + IWritableMount createRootMount(); @Nullable IMount createResourceMount( String domain, String subPath ); 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 c905b5396..5ea9df771 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerPeripheral.java @@ -74,7 +74,7 @@ public class ComputerPeripheral implements IPeripheral @LuaFunction public final int getID() { - return computer.assignID(); + return computer.getID(); } /** diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerProxy.java b/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerProxy.java index a486b4288..af5dddde8 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerProxy.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerProxy.java @@ -68,7 +68,7 @@ public final class ComputerProxy } } - public int assignID() + public int getID() { TileComputerBase tile = getTile(); ServerComputer computer = tile.getServerComputer(); 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 b1aac103a..2cd090508 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java @@ -5,6 +5,7 @@ */ package dan200.computercraft.shared.computer.blocks; +import dan200.computercraft.api.ComputerCraftAPI; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.shared.BundledRedstone; @@ -16,6 +17,7 @@ import dan200.computercraft.shared.computer.core.ServerComputer; import dan200.computercraft.shared.computer.core.ServerComputerRegistry; import dan200.computercraft.shared.network.container.ComputerContainerData; import dan200.computercraft.shared.util.DirectionUtil; +import dan200.computercraft.shared.util.IDAssigner; import dan200.computercraft.shared.util.RedstoneUtil; import joptsimple.internal.Strings; import net.minecraft.block.BlockState; @@ -167,6 +169,7 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT public void tick() { if( getLevel().isClientSide ) return; + if( computerID < 0 && !startOn ) return; // Don't tick if we don't need a computer! ServerComputer computer = createServerComputer(); @@ -348,8 +351,6 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT if( getLevel().isClientSide || computerID == id ) return; computerID = id; - ServerComputer computer = getServerComputer(); - if( computer != null ) computer.setID( computerID ); setChanged(); } @@ -371,7 +372,7 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT } @Nonnull - public ServerComputer createServerComputer() + public final ServerComputer createServerComputer() { if( getLevel().isClientSide ) throw new IllegalStateException( "Cannot access server computer on the client." ); @@ -380,6 +381,12 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT ServerComputer computer = ServerComputerRegistry.INSTANCE.get( instanceID ); if( computer == null ) { + if( computerID < 0 ) + { + computerID = ComputerCraftAPI.createUniqueNumberedSaveDir( level, IDAssigner.COMPUTER ); + updateBlock(); + } + computer = createComputer( computerID ); instanceID = computer.register(); fresh = true; diff --git a/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java b/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java index 0aef4d356..aa866e100 100644 --- a/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java +++ b/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java @@ -172,11 +172,6 @@ public class ServerComputer implements InputHandler, IComputerEnvironment { } - public void setID( int id ) - { - computer.setID( id ); - } - public int getInstanceID() { return instanceID; @@ -286,9 +281,9 @@ public class ServerComputer implements InputHandler, IComputerEnvironment } @Override - public IWritableMount createSaveDirMount( String subPath, long capacity ) + public IWritableMount createRootMount() { - return ComputerCraftAPI.createSaveDirMount( world, subPath, capacity ); + return ComputerCraftAPI.createSaveDirMount( world, "computer/" + computer.getID(), ComputerCraft.computerSpaceLimit ); } @Override @@ -303,12 +298,6 @@ public class ServerComputer implements InputHandler, IComputerEnvironment return ComputerCraftAPIImpl.getResourceFile( domain, subPath ); } - @Override - public long getComputerSpaceLimit() - { - return ComputerCraft.computerSpaceLimit; - } - @Nonnull @Override public String getHostString() @@ -322,10 +311,4 @@ public class ServerComputer implements InputHandler, IComputerEnvironment { return ComputerCraft.MOD_ID + "/" + ComputerCraftAPI.getInstalledVersion(); } - - @Override - public int assignNewID() - { - return ComputerCraftAPI.createUniqueNumberedSaveDir( world, "computer" ); - } } 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 501f0bc52..2095f311b 100644 --- a/src/main/java/dan200/computercraft/shared/pocket/items/ItemPocketComputer.java +++ b/src/main/java/dan200/computercraft/shared/pocket/items/ItemPocketComputer.java @@ -21,6 +21,7 @@ import dan200.computercraft.shared.network.container.ComputerContainerData; import dan200.computercraft.shared.pocket.apis.PocketAPI; import dan200.computercraft.shared.pocket.core.PocketServerComputer; import dan200.computercraft.shared.pocket.inventory.PocketComputerMenuProvider; +import dan200.computercraft.shared.util.IDAssigner; import net.minecraft.client.util.ITooltipFlag; import net.minecraft.entity.Entity; import net.minecraft.entity.item.ItemEntity; @@ -233,7 +234,7 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia, I int computerID = getComputerID( stack ); if( computerID < 0 ) { - computerID = ComputerCraftAPI.createUniqueNumberedSaveDir( world, "computer" ); + computerID = ComputerCraftAPI.createUniqueNumberedSaveDir( world, IDAssigner.COMPUTER ); setComputerID( stack, computerID ); } diff --git a/src/main/java/dan200/computercraft/shared/util/IDAssigner.java b/src/main/java/dan200/computercraft/shared/util/IDAssigner.java index 82faa5fb7..dc153ab44 100644 --- a/src/main/java/dan200/computercraft/shared/util/IDAssigner.java +++ b/src/main/java/dan200/computercraft/shared/util/IDAssigner.java @@ -26,6 +26,8 @@ import java.util.Map; public final class IDAssigner { + public static final String COMPUTER = "computer"; + private static final FolderName FOLDER = new FolderName( ComputerCraft.MOD_ID ); private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); private static final Type ID_TOKEN = new TypeToken>() diff --git a/src/test/java/dan200/computercraft/core/computer/BasicEnvironment.java b/src/test/java/dan200/computercraft/core/computer/BasicEnvironment.java index 073000aa7..7393e07ba 100644 --- a/src/test/java/dan200/computercraft/core/computer/BasicEnvironment.java +++ b/src/test/java/dan200/computercraft/core/computer/BasicEnvironment.java @@ -39,13 +39,7 @@ public class BasicEnvironment implements IComputerEnvironment } @Override - public int assignNewID() - { - return 0; - } - - @Override - public IWritableMount createSaveDirMount( String path, long space ) + public IWritableMount createRootMount() { return mount; } @@ -62,12 +56,6 @@ public class BasicEnvironment implements IComputerEnvironment return 0; } - @Override - public long getComputerSpaceLimit() - { - return ComputerCraft.computerSpaceLimit; - } - @Nonnull @Override public String getHostString()