mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-26 19:37:39 +00:00 
			
		
		
		
	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.
This commit is contained in:
		| @@ -19,8 +19,8 @@ import dan200.computercraft.api.ComputerCraftAPI; | |||||||
|  */ |  */ | ||||||
| public interface WiredElement extends WiredSender { | public interface WiredElement extends WiredSender { | ||||||
|     /** |     /** | ||||||
|      * Called when objects on the network change. This may occur when network nodes are added or removed, or when |      * Called when peripherals on the network change. This may occur when network nodes are added or removed, or when | ||||||
|      * peripherals change. |      * peripherals are attached or detached from a modem. | ||||||
|      * |      * | ||||||
|      * @param change The change which occurred. |      * @param change The change which occurred. | ||||||
|      * @see WiredNetworkChange |      * @see WiredNetworkChange | ||||||
|   | |||||||
| @@ -38,8 +38,8 @@ public final class PocketItemRenderer extends ItemMapLikeRenderer { | |||||||
| 
 | 
 | ||||||
|         int termWidth, termHeight; |         int termWidth, termHeight; | ||||||
|         if (terminal == null) { |         if (terminal == null) { | ||||||
|             termWidth = Config.pocketTermWidth; |             termWidth = Config.DEFAULT_POCKET_TERM_WIDTH; | ||||||
|             termHeight = Config.pocketTermHeight; |             termHeight = Config.DEFAULT_POCKET_TERM_HEIGHT; | ||||||
|         } else { |         } else { | ||||||
|             termWidth = terminal.getWidth(); |             termWidth = terminal.getWidth(); | ||||||
|             termHeight = terminal.getHeight(); |             termHeight = terminal.getHeight(); | ||||||
|   | |||||||
| @@ -11,8 +11,7 @@ import dan200.computercraft.shared.computer.core.ComputerFamily; | |||||||
| import dan200.computercraft.shared.computer.core.ComputerState; | import dan200.computercraft.shared.computer.core.ComputerState; | ||||||
| import dan200.computercraft.shared.computer.core.ServerComputer; | import dan200.computercraft.shared.computer.core.ServerComputer; | ||||||
| import dan200.computercraft.shared.computer.inventory.ComputerMenuWithoutInventory; | import dan200.computercraft.shared.computer.inventory.ComputerMenuWithoutInventory; | ||||||
| import dan200.computercraft.shared.config.Config; | import dan200.computercraft.shared.config.ConfigSpec; | ||||||
| import dan200.computercraft.shared.util.ComponentMap; |  | ||||||
| import net.minecraft.core.BlockPos; | import net.minecraft.core.BlockPos; | ||||||
| import net.minecraft.core.Direction; | import net.minecraft.core.Direction; | ||||||
| import net.minecraft.server.level.ServerLevel; | import net.minecraft.server.level.ServerLevel; | ||||||
| @@ -33,10 +32,9 @@ public class ComputerBlockEntity extends AbstractComputerBlockEntity { | |||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     protected ServerComputer createComputer(int id) { |     protected ServerComputer createComputer(int id) { | ||||||
|         return new ServerComputer( |         return new ServerComputer((ServerLevel) getLevel(), getBlockPos(), ServerComputer.properties(id, getFamily()) | ||||||
|             (ServerLevel) getLevel(), getBlockPos(), id, label, |             .label(getLabel()) | ||||||
|             getFamily(), Config.computerTermWidth, Config.computerTermHeight, |             .terminalSize(ConfigSpec.computerTermWidth.get(), ConfigSpec.computerTermHeight.get()) | ||||||
|             ComponentMap.empty() |  | ||||||
|         ); |         ); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|   | |||||||
| @@ -6,6 +6,7 @@ package dan200.computercraft.shared.computer.core; | |||||||
| 
 | 
 | ||||||
| import dan200.computercraft.api.ComputerCraftAPI; | import dan200.computercraft.api.ComputerCraftAPI; | ||||||
| import dan200.computercraft.api.component.AdminComputer; | import dan200.computercraft.api.component.AdminComputer; | ||||||
|  | import dan200.computercraft.api.component.ComputerComponent; | ||||||
| import dan200.computercraft.api.component.ComputerComponents; | import dan200.computercraft.api.component.ComputerComponents; | ||||||
| import dan200.computercraft.api.filesystem.WritableMount; | import dan200.computercraft.api.filesystem.WritableMount; | ||||||
| import dan200.computercraft.api.peripheral.IPeripheral; | import dan200.computercraft.api.peripheral.IPeripheral; | ||||||
| @@ -51,17 +52,22 @@ public class ServerComputer implements ComputerEnvironment, ComputerEvents.Recei | |||||||
| 
 | 
 | ||||||
|     private int ticksSincePing; |     private int ticksSincePing; | ||||||
| 
 | 
 | ||||||
|  |     @Deprecated | ||||||
|     public ServerComputer( |     public ServerComputer( | ||||||
|         ServerLevel level, BlockPos position, int computerID, @Nullable String label, ComputerFamily family, int terminalWidth, int terminalHeight, |         ServerLevel level, BlockPos position, int computerID, @Nullable String label, ComputerFamily family, int terminalWidth, int terminalHeight, | ||||||
|         ComponentMap baseComponents |         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.level = level; | ||||||
|         this.position = position; |         this.position = position; | ||||||
|         this.family = family; |         this.family = properties.family; | ||||||
| 
 | 
 | ||||||
|         var context = ServerContext.get(level.getServer()); |         var context = ServerContext.get(level.getServer()); | ||||||
|         instanceID = context.registry().getUnusedInstanceID(); |         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); |         metrics = context.metrics().createMetricObserver(this); | ||||||
| 
 | 
 | ||||||
|         var componentBuilder = ComponentMap.builder(); |         var componentBuilder = ComponentMap.builder(); | ||||||
| @@ -70,11 +76,11 @@ public class ServerComputer implements ComputerEnvironment, ComputerEvents.Recei | |||||||
|             componentBuilder.add(ComputerComponents.ADMIN_COMPUTER, new AdminComputer() { |             componentBuilder.add(ComputerComponents.ADMIN_COMPUTER, new AdminComputer() { | ||||||
|             }); |             }); | ||||||
|         } |         } | ||||||
|         componentBuilder.add(baseComponents); |         componentBuilder.add(properties.components.build()); | ||||||
|         var components = componentBuilder.build(); |         var components = componentBuilder.build(); | ||||||
| 
 | 
 | ||||||
|         computer = new Computer(context.computerContext(), this, terminal, computerID); |         computer = new Computer(context.computerContext(), this, terminal, properties.computerID); | ||||||
|         computer.setLabel(label); |         computer.setLabel(properties.label); | ||||||
| 
 | 
 | ||||||
|         // Load in the externally registered APIs. |         // Load in the externally registered APIs. | ||||||
|         for (var factory : ApiFactories.getAll()) { |         for (var factory : ApiFactories.getAll()) { | ||||||
| @@ -271,4 +277,46 @@ public class ServerComputer implements ComputerEnvironment, ComputerEvents.Recei | |||||||
|     public final WritableMount createRootMount() { |     public final WritableMount createRootMount() { | ||||||
|         return ComputerCraftAPI.createSaveDirMount(level.getServer(), "computer/" + computer.getID(), Config.computerSpaceLimit); |         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 <T> Properties addComponent(ComputerComponent<T> component, T value) { | ||||||
|  |             components.add(component, value); | ||||||
|  |             return this; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         private Properties addComponents(ComponentMap components) { | ||||||
|  |             this.components.add(components); | ||||||
|  |             return this; | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -32,14 +32,14 @@ public final class Config { | |||||||
|     public static int advancedTurtleFuelLimit = 100000; |     public static int advancedTurtleFuelLimit = 100000; | ||||||
|     public static boolean turtlesCanPush = true; |     public static boolean turtlesCanPush = true; | ||||||
| 
 | 
 | ||||||
|     public static int computerTermWidth = 51; |     public static final int DEFAULT_COMPUTER_TERM_WIDTH = 51; | ||||||
|     public static int computerTermHeight = 19; |     public static final int DEFAULT_COMPUTER_TERM_HEIGHT = 19; | ||||||
| 
 | 
 | ||||||
|     public static final int turtleTermWidth = 39; |     public static final int TURTLE_TERM_WIDTH = 39; | ||||||
|     public static final int turtleTermHeight = 13; |     public static final int TURTLE_TERM_HEIGHT = 13; | ||||||
| 
 | 
 | ||||||
|     public static int pocketTermWidth = 26; |     public static final int DEFAULT_POCKET_TERM_WIDTH = 26; | ||||||
|     public static int pocketTermHeight = 20; |     public static final int DEFAULT_POCKET_TERM_HEIGHT = 20; | ||||||
| 
 | 
 | ||||||
|     public static int monitorWidth = 8; |     public static int monitorWidth = 8; | ||||||
|     public static int monitorHeight = 6; |     public static int monitorHeight = 6; | ||||||
|   | |||||||
| @@ -344,13 +344,13 @@ public final class ConfigSpec { | |||||||
|                 .push("term_sizes"); |                 .push("term_sizes"); | ||||||
| 
 | 
 | ||||||
|             builder.comment("Terminal size of computers.").push("computer"); |             builder.comment("Terminal size of computers.").push("computer"); | ||||||
|             computerTermWidth = builder.comment("Width of computer terminal").defineInRange("width", Config.computerTermWidth, 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.computerTermHeight, 1, 255); |             computerTermHeight = builder.comment("Height of computer terminal").defineInRange("height", Config.DEFAULT_COMPUTER_TERM_HEIGHT, 1, 255); | ||||||
|             builder.pop(); |             builder.pop(); | ||||||
| 
 | 
 | ||||||
|             builder.comment("Terminal size of pocket computers.").push("pocket_computer"); |             builder.comment("Terminal size of pocket computers.").push("pocket_computer"); | ||||||
|             pocketTermWidth = builder.comment("Width of pocket computer terminal").defineInRange("width", Config.pocketTermWidth, 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.pocketTermHeight, 1, 255); |             pocketTermHeight = builder.comment("Height of pocket computer terminal").defineInRange("height", Config.DEFAULT_POCKET_TERM_HEIGHT, 1, 255); | ||||||
|             builder.pop(); |             builder.pop(); | ||||||
| 
 | 
 | ||||||
|             builder.comment("Maximum size of monitors (in blocks).").push("monitor"); |             builder.comment("Maximum size of monitors (in blocks).").push("monitor"); | ||||||
| @@ -437,10 +437,6 @@ public final class ConfigSpec { | |||||||
|         Config.turtlesCanPush = turtlesCanPush.get(); |         Config.turtlesCanPush = turtlesCanPush.get(); | ||||||
| 
 | 
 | ||||||
|         // Terminal size |         // Terminal size | ||||||
|         Config.computerTermWidth = computerTermWidth.get(); |  | ||||||
|         Config.computerTermHeight = computerTermHeight.get(); |  | ||||||
|         Config.pocketTermWidth = pocketTermWidth.get(); |  | ||||||
|         Config.pocketTermHeight = pocketTermHeight.get(); |  | ||||||
|         Config.monitorWidth = monitorWidth.get(); |         Config.monitorWidth = monitorWidth.get(); | ||||||
|         Config.monitorHeight = monitorHeight.get(); |         Config.monitorHeight = monitorHeight.get(); | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -10,7 +10,7 @@ import dan200.computercraft.api.pocket.IPocketUpgrade; | |||||||
| import dan200.computercraft.api.upgrades.UpgradeData; | import dan200.computercraft.api.upgrades.UpgradeData; | ||||||
| import dan200.computercraft.core.computer.ComputerSide; | import dan200.computercraft.core.computer.ComputerSide; | ||||||
| import dan200.computercraft.shared.common.IColouredItem; | 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.client.PocketComputerDataMessage; | ||||||
| import dan200.computercraft.shared.network.server.ServerNetworking; | import dan200.computercraft.shared.network.server.ServerNetworking; | ||||||
| import dan200.computercraft.shared.pocket.items.PocketComputerItem; | import dan200.computercraft.shared.pocket.items.PocketComputerItem; | ||||||
| @@ -44,8 +44,8 @@ public final class PocketBrain implements IPocketAccess { | |||||||
|     private int colour = -1; |     private int colour = -1; | ||||||
|     private int lightColour = -1; |     private int lightColour = -1; | ||||||
| 
 | 
 | ||||||
|     public PocketBrain(PocketHolder holder, int computerID, @Nullable String label, ComputerFamily family, @Nullable UpgradeData<IPocketUpgrade> upgrade) { |     public PocketBrain(PocketHolder holder, @Nullable UpgradeData<IPocketUpgrade> upgrade, ServerComputer.Properties properties) { | ||||||
|         this.computer = new PocketServerComputer(this, holder, computerID, label, family); |         this.computer = new PocketServerComputer(this, holder, properties); | ||||||
|         this.holder = holder; |         this.holder = holder; | ||||||
|         this.position = holder.pos(); |         this.position = holder.pos(); | ||||||
|         this.upgrade = UpgradeData.copyOf(upgrade); |         this.upgrade = UpgradeData.copyOf(upgrade); | ||||||
|   | |||||||
| @@ -5,15 +5,13 @@ | |||||||
| package dan200.computercraft.shared.pocket.core; | package dan200.computercraft.shared.pocket.core; | ||||||
| 
 | 
 | ||||||
| import dan200.computercraft.api.component.ComputerComponents; | 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.ComputerState; | ||||||
| import dan200.computercraft.shared.computer.core.ServerComputer; | 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.PocketComputerDataMessage; | ||||||
| import dan200.computercraft.shared.network.client.PocketComputerDeletedClientMessage; | import dan200.computercraft.shared.network.client.PocketComputerDeletedClientMessage; | ||||||
| import dan200.computercraft.shared.network.server.ServerNetworking; | import dan200.computercraft.shared.network.server.ServerNetworking; | ||||||
| import dan200.computercraft.shared.pocket.items.PocketComputerItem; | import dan200.computercraft.shared.pocket.items.PocketComputerItem; | ||||||
| import dan200.computercraft.shared.util.ComponentMap; |  | ||||||
| import net.minecraft.server.level.ServerPlayer; | import net.minecraft.server.level.ServerPlayer; | ||||||
| import net.minecraft.world.level.ChunkPos; | import net.minecraft.world.level.ChunkPos; | ||||||
| 
 | 
 | ||||||
| @@ -41,10 +39,10 @@ public final class PocketServerComputer extends ServerComputer { | |||||||
| 
 | 
 | ||||||
|     private Set<ServerPlayer> tracking = Set.of(); |     private Set<ServerPlayer> tracking = Set.of(); | ||||||
| 
 | 
 | ||||||
|     PocketServerComputer(PocketBrain brain, PocketHolder holder, int computerID, @Nullable String label, ComputerFamily family) { |     PocketServerComputer(PocketBrain brain, PocketHolder holder, ServerComputer.Properties properties) { | ||||||
|         super( |         super(holder.level(), holder.blockPos(), properties | ||||||
|             holder.level(), holder.blockPos(), computerID, label, family, Config.pocketTermWidth, Config.pocketTermHeight, |             .terminalSize(ConfigSpec.pocketTermWidth.get(), ConfigSpec.pocketTermHeight.get()) | ||||||
|             ComponentMap.builder().add(ComputerComponents.POCKET, brain).build() |             .addComponent(ComputerComponents.POCKET, brain) | ||||||
|         ); |         ); | ||||||
|         this.brain = brain; |         this.brain = brain; | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -235,7 +235,10 @@ public class PocketComputerItem extends Item implements IComputerItem, IMedia, I | |||||||
|             setComputerID(stack, computerID); |             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 computer = brain.computer(); | ||||||
| 
 | 
 | ||||||
|         var tag = stack.getOrCreateTag(); |         var tag = stack.getOrCreateTag(); | ||||||
|   | |||||||
| @@ -20,7 +20,6 @@ import dan200.computercraft.shared.config.Config; | |||||||
| import dan200.computercraft.shared.container.BasicContainer; | import dan200.computercraft.shared.container.BasicContainer; | ||||||
| import dan200.computercraft.shared.turtle.core.TurtleBrain; | import dan200.computercraft.shared.turtle.core.TurtleBrain; | ||||||
| import dan200.computercraft.shared.turtle.inventory.TurtleMenu; | import dan200.computercraft.shared.turtle.inventory.TurtleMenu; | ||||||
| import dan200.computercraft.shared.util.ComponentMap; |  | ||||||
| import net.minecraft.core.BlockPos; | import net.minecraft.core.BlockPos; | ||||||
| import net.minecraft.core.Direction; | import net.minecraft.core.Direction; | ||||||
| import net.minecraft.core.NonNullList; | import net.minecraft.core.NonNullList; | ||||||
| @@ -74,10 +73,10 @@ public class TurtleBlockEntity extends AbstractComputerBlockEntity implements Ba | |||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     protected ServerComputer createComputer(int id) { |     protected ServerComputer createComputer(int id) { | ||||||
|         var computer = new ServerComputer( |         var computer = new ServerComputer((ServerLevel) getLevel(), getBlockPos(), ServerComputer.properties(id, getFamily()) | ||||||
|             (ServerLevel) getLevel(), getBlockPos(), id, label, |             .label(getLabel()) | ||||||
|             getFamily(), Config.turtleTermWidth, Config.turtleTermHeight, |             .terminalSize(Config.TURTLE_TERM_WIDTH, Config.TURTLE_TERM_HEIGHT) | ||||||
|             ComponentMap.builder().add(ComputerComponents.TURTLE, brain).build() |             .addComponent(ComputerComponents.TURTLE, brain) | ||||||
|         ); |         ); | ||||||
|         brain.setupComputer(computer); |         brain.setupComputer(computer); | ||||||
|         return computer; |         return computer; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jonathan Coates
					Jonathan Coates