From dc969c5a785514a167e6bd31717c1a6f5fe54d23 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Mon, 20 Jan 2025 19:47:18 +0000 Subject: [PATCH] Configure ServerComputer via a Properties builder We currently need to pass a whole bunch of arguments to a ServerComputer in order to construct it, and if we implement #1814, this will get a whole lot worse. Instead, we now pass most parameters (computer id, family, label, term size, components) via a separate Properties class, much like Minecraft does for blocks and items. I'm not wild about the design of the API here, but I think it's a step in the right direction. --- .../api/network/wired/WiredElement.java | 4 +- .../client/render/PocketItemRenderer.java | 4 +- .../computer/blocks/ComputerBlockEntity.java | 10 ++-- .../shared/computer/core/ServerComputer.java | 58 +++++++++++++++++-- .../computercraft/shared/config/Config.java | 12 ++-- .../shared/config/ConfigSpec.java | 12 ++-- .../shared/pocket/core/PocketBrain.java | 6 +- .../pocket/core/PocketServerComputer.java | 12 ++-- .../pocket/items/PocketComputerItem.java | 5 +- .../turtle/blocks/TurtleBlockEntity.java | 9 ++- 10 files changed, 87 insertions(+), 45 deletions(-) diff --git a/projects/common-api/src/main/java/dan200/computercraft/api/network/wired/WiredElement.java b/projects/common-api/src/main/java/dan200/computercraft/api/network/wired/WiredElement.java index 01d197ebd..947f43886 100644 --- a/projects/common-api/src/main/java/dan200/computercraft/api/network/wired/WiredElement.java +++ b/projects/common-api/src/main/java/dan200/computercraft/api/network/wired/WiredElement.java @@ -19,8 +19,8 @@ import dan200.computercraft.api.ComputerCraftAPI; */ public interface WiredElement extends WiredSender { /** - * Called when objects on the network change. This may occur when network nodes are added or removed, or when - * peripherals change. + * Called when peripherals on the network change. This may occur when network nodes are added or removed, or when + * peripherals are attached or detached from a modem. * * @param change The change which occurred. * @see WiredNetworkChange diff --git a/projects/common/src/client/java/dan200/computercraft/client/render/PocketItemRenderer.java b/projects/common/src/client/java/dan200/computercraft/client/render/PocketItemRenderer.java index 2f4337aee..bca19b391 100644 --- a/projects/common/src/client/java/dan200/computercraft/client/render/PocketItemRenderer.java +++ b/projects/common/src/client/java/dan200/computercraft/client/render/PocketItemRenderer.java @@ -38,8 +38,8 @@ public final class PocketItemRenderer extends ItemMapLikeRenderer { int termWidth, termHeight; if (terminal == null) { - termWidth = Config.pocketTermWidth; - termHeight = Config.pocketTermHeight; + termWidth = Config.DEFAULT_POCKET_TERM_WIDTH; + termHeight = Config.DEFAULT_POCKET_TERM_HEIGHT; } else { termWidth = terminal.getWidth(); termHeight = terminal.getHeight(); diff --git a/projects/common/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerBlockEntity.java b/projects/common/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerBlockEntity.java index 857b0a0e2..ab6f90e7b 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerBlockEntity.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerBlockEntity.java @@ -11,8 +11,7 @@ import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.ComputerState; import dan200.computercraft.shared.computer.core.ServerComputer; import dan200.computercraft.shared.computer.inventory.ComputerMenuWithoutInventory; -import dan200.computercraft.shared.config.Config; -import dan200.computercraft.shared.util.ComponentMap; +import dan200.computercraft.shared.config.ConfigSpec; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.server.level.ServerLevel; @@ -33,10 +32,9 @@ public class ComputerBlockEntity extends AbstractComputerBlockEntity { @Override protected ServerComputer createComputer(int id) { - return new ServerComputer( - (ServerLevel) getLevel(), getBlockPos(), id, label, - getFamily(), Config.computerTermWidth, Config.computerTermHeight, - ComponentMap.empty() + return new ServerComputer((ServerLevel) getLevel(), getBlockPos(), ServerComputer.properties(id, getFamily()) + .label(getLabel()) + .terminalSize(ConfigSpec.computerTermWidth.get(), ConfigSpec.computerTermHeight.get()) ); } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java b/projects/common/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java index 906d30bbe..ad32ee031 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java @@ -6,6 +6,7 @@ package dan200.computercraft.shared.computer.core; import dan200.computercraft.api.ComputerCraftAPI; import dan200.computercraft.api.component.AdminComputer; +import dan200.computercraft.api.component.ComputerComponent; import dan200.computercraft.api.component.ComputerComponents; import dan200.computercraft.api.filesystem.WritableMount; import dan200.computercraft.api.peripheral.IPeripheral; @@ -51,17 +52,22 @@ public class ServerComputer implements ComputerEnvironment, ComputerEvents.Recei private int ticksSincePing; + @Deprecated public ServerComputer( ServerLevel level, BlockPos position, int computerID, @Nullable String label, ComputerFamily family, int terminalWidth, int terminalHeight, ComponentMap baseComponents ) { + this(level, position, properties(computerID, family).label(label).terminalSize(terminalWidth, terminalHeight).addComponents(baseComponents)); + } + + public ServerComputer(ServerLevel level, BlockPos position, Properties properties) { this.level = level; this.position = position; - this.family = family; + this.family = properties.family; var context = ServerContext.get(level.getServer()); instanceID = context.registry().getUnusedInstanceID(); - terminal = new NetworkedTerminal(terminalWidth, terminalHeight, family != ComputerFamily.NORMAL, this::markTerminalChanged); + terminal = new NetworkedTerminal(properties.terminalWidth, properties.terminalHeight, family != ComputerFamily.NORMAL, this::markTerminalChanged); metrics = context.metrics().createMetricObserver(this); var componentBuilder = ComponentMap.builder(); @@ -70,11 +76,11 @@ public class ServerComputer implements ComputerEnvironment, ComputerEvents.Recei componentBuilder.add(ComputerComponents.ADMIN_COMPUTER, new AdminComputer() { }); } - componentBuilder.add(baseComponents); + componentBuilder.add(properties.components.build()); var components = componentBuilder.build(); - computer = new Computer(context.computerContext(), this, terminal, computerID); - computer.setLabel(label); + computer = new Computer(context.computerContext(), this, terminal, properties.computerID); + computer.setLabel(properties.label); // Load in the externally registered APIs. for (var factory : ApiFactories.getAll()) { @@ -271,4 +277,46 @@ public class ServerComputer implements ComputerEnvironment, ComputerEvents.Recei public final WritableMount createRootMount() { return ComputerCraftAPI.createSaveDirMount(level.getServer(), "computer/" + computer.getID(), Config.computerSpaceLimit); } + + public static Properties properties(int computerID, ComputerFamily family) { + return new Properties(computerID, family); + } + + public static final class Properties { + + private final int computerID; + private @Nullable String label; + private final ComputerFamily family; + + private int terminalWidth = Config.DEFAULT_COMPUTER_TERM_WIDTH; + private int terminalHeight = Config.DEFAULT_COMPUTER_TERM_HEIGHT; + private final ComponentMap.Builder components = ComponentMap.builder(); + + private Properties(int computerID, ComputerFamily family) { + this.computerID = computerID; + this.family = family; + } + + public Properties label(@Nullable String label) { + this.label = label; + return this; + } + + public Properties terminalSize(int width, int height) { + if (width <= 0 || height <= 0) throw new IllegalArgumentException("Terminal size must be positive"); + this.terminalWidth = width; + this.terminalHeight = height; + return this; + } + + public Properties addComponent(ComputerComponent component, T value) { + components.add(component, value); + return this; + } + + private Properties addComponents(ComponentMap components) { + this.components.add(components); + return this; + } + } } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/config/Config.java b/projects/common/src/main/java/dan200/computercraft/shared/config/Config.java index b18d7cafd..bb5b1180b 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/config/Config.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/config/Config.java @@ -32,14 +32,14 @@ public final class Config { public static int advancedTurtleFuelLimit = 100000; public static boolean turtlesCanPush = true; - public static int computerTermWidth = 51; - public static int computerTermHeight = 19; + public static final int DEFAULT_COMPUTER_TERM_WIDTH = 51; + public static final int DEFAULT_COMPUTER_TERM_HEIGHT = 19; - public static final int turtleTermWidth = 39; - public static final int turtleTermHeight = 13; + public static final int TURTLE_TERM_WIDTH = 39; + public static final int TURTLE_TERM_HEIGHT = 13; - public static int pocketTermWidth = 26; - public static int pocketTermHeight = 20; + public static final int DEFAULT_POCKET_TERM_WIDTH = 26; + public static final int DEFAULT_POCKET_TERM_HEIGHT = 20; public static int monitorWidth = 8; public static int monitorHeight = 6; diff --git a/projects/common/src/main/java/dan200/computercraft/shared/config/ConfigSpec.java b/projects/common/src/main/java/dan200/computercraft/shared/config/ConfigSpec.java index eaa17523a..201dcb635 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/config/ConfigSpec.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/config/ConfigSpec.java @@ -344,13 +344,13 @@ public final class ConfigSpec { .push("term_sizes"); builder.comment("Terminal size of computers.").push("computer"); - computerTermWidth = builder.comment("Width of computer terminal").defineInRange("width", Config.computerTermWidth, 1, 255); - computerTermHeight = builder.comment("Height of computer terminal").defineInRange("height", Config.computerTermHeight, 1, 255); + computerTermWidth = builder.comment("Width of computer terminal").defineInRange("width", Config.DEFAULT_COMPUTER_TERM_WIDTH, 1, 255); + computerTermHeight = builder.comment("Height of computer terminal").defineInRange("height", Config.DEFAULT_COMPUTER_TERM_HEIGHT, 1, 255); builder.pop(); builder.comment("Terminal size of pocket computers.").push("pocket_computer"); - pocketTermWidth = builder.comment("Width of pocket computer terminal").defineInRange("width", Config.pocketTermWidth, 1, 255); - pocketTermHeight = builder.comment("Height of pocket computer terminal").defineInRange("height", Config.pocketTermHeight, 1, 255); + pocketTermWidth = builder.comment("Width of pocket computer terminal").defineInRange("width", Config.DEFAULT_POCKET_TERM_WIDTH, 1, 255); + pocketTermHeight = builder.comment("Height of pocket computer terminal").defineInRange("height", Config.DEFAULT_POCKET_TERM_HEIGHT, 1, 255); builder.pop(); builder.comment("Maximum size of monitors (in blocks).").push("monitor"); @@ -437,10 +437,6 @@ public final class ConfigSpec { Config.turtlesCanPush = turtlesCanPush.get(); // Terminal size - Config.computerTermWidth = computerTermWidth.get(); - Config.computerTermHeight = computerTermHeight.get(); - Config.pocketTermWidth = pocketTermWidth.get(); - Config.pocketTermHeight = pocketTermHeight.get(); Config.monitorWidth = monitorWidth.get(); Config.monitorHeight = monitorHeight.get(); } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/pocket/core/PocketBrain.java b/projects/common/src/main/java/dan200/computercraft/shared/pocket/core/PocketBrain.java index 6d8286e5b..8e6e3c7b2 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/pocket/core/PocketBrain.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/pocket/core/PocketBrain.java @@ -10,7 +10,7 @@ import dan200.computercraft.api.pocket.IPocketUpgrade; import dan200.computercraft.api.upgrades.UpgradeData; import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.shared.common.IColouredItem; -import dan200.computercraft.shared.computer.core.ComputerFamily; +import dan200.computercraft.shared.computer.core.ServerComputer; import dan200.computercraft.shared.network.client.PocketComputerDataMessage; import dan200.computercraft.shared.network.server.ServerNetworking; import dan200.computercraft.shared.pocket.items.PocketComputerItem; @@ -44,8 +44,8 @@ public final class PocketBrain implements IPocketAccess { private int colour = -1; private int lightColour = -1; - public PocketBrain(PocketHolder holder, int computerID, @Nullable String label, ComputerFamily family, @Nullable UpgradeData upgrade) { - this.computer = new PocketServerComputer(this, holder, computerID, label, family); + public PocketBrain(PocketHolder holder, @Nullable UpgradeData upgrade, ServerComputer.Properties properties) { + this.computer = new PocketServerComputer(this, holder, properties); this.holder = holder; this.position = holder.pos(); this.upgrade = UpgradeData.copyOf(upgrade); diff --git a/projects/common/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java b/projects/common/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java index eabe10323..bad688559 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java @@ -5,15 +5,13 @@ package dan200.computercraft.shared.pocket.core; import dan200.computercraft.api.component.ComputerComponents; -import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.ComputerState; import dan200.computercraft.shared.computer.core.ServerComputer; -import dan200.computercraft.shared.config.Config; +import dan200.computercraft.shared.config.ConfigSpec; import dan200.computercraft.shared.network.client.PocketComputerDataMessage; import dan200.computercraft.shared.network.client.PocketComputerDeletedClientMessage; import dan200.computercraft.shared.network.server.ServerNetworking; import dan200.computercraft.shared.pocket.items.PocketComputerItem; -import dan200.computercraft.shared.util.ComponentMap; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.level.ChunkPos; @@ -41,10 +39,10 @@ public final class PocketServerComputer extends ServerComputer { private Set tracking = Set.of(); - PocketServerComputer(PocketBrain brain, PocketHolder holder, int computerID, @Nullable String label, ComputerFamily family) { - super( - holder.level(), holder.blockPos(), computerID, label, family, Config.pocketTermWidth, Config.pocketTermHeight, - ComponentMap.builder().add(ComputerComponents.POCKET, brain).build() + PocketServerComputer(PocketBrain brain, PocketHolder holder, ServerComputer.Properties properties) { + super(holder.level(), holder.blockPos(), properties + .terminalSize(ConfigSpec.pocketTermWidth.get(), ConfigSpec.pocketTermHeight.get()) + .addComponent(ComputerComponents.POCKET, brain) ); this.brain = brain; } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java b/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java index f08c871d1..9e54e2b3e 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java @@ -235,7 +235,10 @@ public class PocketComputerItem extends Item implements IComputerItem, IMedia, I setComputerID(stack, computerID); } - var brain = new PocketBrain(holder, getComputerID(stack), getLabel(stack), getFamily(), getUpgradeWithData(stack)); + var brain = new PocketBrain( + holder, getUpgradeWithData(stack), + ServerComputer.properties(getComputerID(stack), getFamily()).label(getLabel(stack)) + ); var computer = brain.computer(); var tag = stack.getOrCreateTag(); diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java index d166b0011..a1661cac9 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java @@ -20,7 +20,6 @@ import dan200.computercraft.shared.config.Config; import dan200.computercraft.shared.container.BasicContainer; import dan200.computercraft.shared.turtle.core.TurtleBrain; import dan200.computercraft.shared.turtle.inventory.TurtleMenu; -import dan200.computercraft.shared.util.ComponentMap; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.NonNullList; @@ -74,10 +73,10 @@ public class TurtleBlockEntity extends AbstractComputerBlockEntity implements Ba @Override protected ServerComputer createComputer(int id) { - var computer = new ServerComputer( - (ServerLevel) getLevel(), getBlockPos(), id, label, - getFamily(), Config.turtleTermWidth, Config.turtleTermHeight, - ComponentMap.builder().add(ComputerComponents.TURTLE, brain).build() + var computer = new ServerComputer((ServerLevel) getLevel(), getBlockPos(), ServerComputer.properties(id, getFamily()) + .label(getLabel()) + .terminalSize(Config.TURTLE_TERM_WIDTH, Config.TURTLE_TERM_HEIGHT) + .addComponent(ComputerComponents.TURTLE, brain) ); brain.setupComputer(computer); return computer;