1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-10 01:10:30 +00:00

Hide a few ServerComputer internals

This commit is contained in:
Jonathan Coates 2024-06-22 18:48:31 +01:00
parent 898cb2a95d
commit 819a4f7231
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
8 changed files with 36 additions and 46 deletions

View File

@ -8,7 +8,7 @@ import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.api.filesystem.WritableMount; import dan200.computercraft.api.filesystem.WritableMount;
import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.lua.ILuaAPI;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.apis.IAPIEnvironment; import dan200.computercraft.api.peripheral.WorkMonitor;
import dan200.computercraft.core.computer.Computer; import dan200.computercraft.core.computer.Computer;
import dan200.computercraft.core.computer.ComputerEnvironment; import dan200.computercraft.core.computer.ComputerEnvironment;
import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.core.computer.ComputerSide;
@ -74,31 +74,20 @@ public class ServerComputer implements InputHandler, ComputerEnvironment {
return level; return level;
} }
public void setLevel(ServerLevel level) {
this.level = level;
}
public BlockPos getPosition() { public BlockPos getPosition() {
return position; return position;
} }
public void setPosition(BlockPos pos) { public void setPosition(ServerLevel level, BlockPos pos) {
position = new BlockPos(pos); this.level = level;
} position = pos.immutable();
public IAPIEnvironment getAPIEnvironment() {
return computer.getAPIEnvironment();
}
public Computer getComputer() {
return computer;
} }
protected void markTerminalChanged() { protected void markTerminalChanged() {
terminalChanged.set(true); terminalChanged.set(true);
} }
public void tickServer() { protected void tickServer() {
ticksSincePing++; ticksSincePing++;
computer.tick(); computer.tick();
if (terminalChanged.getAndSet(false)) onTerminalChanged(); if (terminalChanged.getAndSet(false)) onTerminalChanged();
@ -116,10 +105,15 @@ public class ServerComputer implements InputHandler, ComputerEnvironment {
ticksSincePing = 0; ticksSincePing = 0;
} }
public boolean hasTimedOut() { boolean hasTimedOut() {
return ticksSincePing > 100; return ticksSincePing > 100;
} }
/**
* Get a bitmask returning which sides on the computer have changed, resetting the internal state.
*
* @return What sides on the computer have changed.
*/
public int pollAndResetChanges() { public int pollAndResetChanges() {
return computer.pollAndResetChanges(); return computer.pollAndResetChanges();
} }
@ -189,25 +183,21 @@ public class ServerComputer implements InputHandler, ComputerEnvironment {
@Override @Override
public void turnOn() { public void turnOn() {
// Turn on
computer.turnOn(); computer.turnOn();
} }
@Override @Override
public void shutdown() { public void shutdown() {
// Shutdown
computer.shutdown(); computer.shutdown();
} }
@Override @Override
public void reboot() { public void reboot() {
// Reboot
computer.reboot(); computer.reboot();
} }
@Override @Override
public void queueEvent(String event, @Nullable Object[] arguments) { public void queueEvent(String event, @Nullable Object[] arguments) {
// Queue event
computer.queueEvent(event, arguments); computer.queueEvent(event, arguments);
} }
@ -259,6 +249,10 @@ public class ServerComputer implements InputHandler, ComputerEnvironment {
return metrics; return metrics;
} }
public WorkMonitor getMainThreadMonitor() {
return computer.getMainThreadMonitor();
}
@Override @Override
public @Nullable WritableMount createRootMount() { public @Nullable WritableMount createRootMount() {
return ComputerCraftAPI.createSaveDirMount(level.getServer(), "computer/" + computer.getID(), Config.computerSpaceLimit); return ComputerCraftAPI.createSaveDirMount(level.getServer(), "computer/" + computer.getID(), Config.computerSpaceLimit);

View File

@ -132,10 +132,7 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces
} }
public synchronized void updateValues(@Nullable Entity entity, ItemStack stack, @Nullable IPocketUpgrade upgrade) { public synchronized void updateValues(@Nullable Entity entity, ItemStack stack, @Nullable IPocketUpgrade upgrade) {
if (entity != null) { if (entity != null) setPosition((ServerLevel) entity.level(), entity.blockPosition());
setLevel((ServerLevel) entity.getCommandSenderWorld());
setPosition(entity.blockPosition());
}
// If a new entity has picked it up then rebroadcast the terminal to them // If a new entity has picked it up then rebroadcast the terminal to them
if (entity != this.entity && entity instanceof ServerPlayer) markTerminalChanged(); if (entity != this.entity && entity instanceof ServerPlayer) markTerminalChanged();
@ -150,7 +147,7 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces
} }
@Override @Override
public void tickServer() { protected void tickServer() {
super.tickServer(); super.tickServer();
// Find any players which have gone missing and remove them from the tracking list. // Find any players which have gone missing and remove them from the tracking list.

View File

@ -72,10 +72,9 @@ public class PocketComputerItem extends Item implements IComputerItem, IMedia, I
return result; return result;
} }
private boolean tick(ItemStack stack, Level world, Entity entity, PocketServerComputer computer) { private boolean tick(ItemStack stack, Entity entity, PocketServerComputer computer) {
var upgrade = getUpgrade(stack); var upgrade = getUpgrade(stack);
computer.setLevel((ServerLevel) world);
computer.updateValues(entity, stack, upgrade); computer.updateValues(entity, stack, upgrade);
var changed = false; var changed = false;
@ -113,7 +112,7 @@ public class PocketComputerItem extends Item implements IComputerItem, IMedia, I
var computer = createServerComputer((ServerLevel) world, entity, inventory, stack); var computer = createServerComputer((ServerLevel) world, entity, inventory, stack);
computer.keepAlive(); computer.keepAlive();
var changed = tick(stack, world, entity, computer); var changed = tick(stack, entity, computer);
if (changed && inventory != null) inventory.setChanged(); if (changed && inventory != null) inventory.setChanged();
} }
@ -123,7 +122,7 @@ public class PocketComputerItem extends Item implements IComputerItem, IMedia, I
if (level.isClientSide || level.getServer() == null) return false; if (level.isClientSide || level.getServer() == null) return false;
var computer = getServerComputer(level.getServer(), stack); var computer = getServerComputer(level.getServer(), stack);
if (computer != null && tick(stack, entity.level(), entity, computer)) entity.setItem(stack.copy()); if (computer != null && tick(stack, entity, computer)) entity.setItem(stack.copy());
return false; return false;
} }
@ -215,7 +214,7 @@ public class PocketComputerItem extends Item implements IComputerItem, IMedia, I
if (inventory != null) inventory.setChanged(); if (inventory != null) inventory.setChanged();
} }
computer.setLevel(level);
return computer; return computer;
} }

View File

@ -25,7 +25,7 @@ public class PocketModemPeripheral extends WirelessModemPeripheral {
void setLocation(IPocketAccess access) { void setLocation(IPocketAccess access) {
var entity = access.getEntity(); var entity = access.getEntity();
if (entity != null) { if (entity != null) {
level = entity.getCommandSenderWorld(); level = entity.level();
position = entity.getEyePosition(1); position = entity.getEyePosition(1);
} }
} }

View File

@ -9,8 +9,9 @@ import dan200.computercraft.api.lua.*;
import dan200.computercraft.api.turtle.TurtleCommand; import dan200.computercraft.api.turtle.TurtleCommand;
import dan200.computercraft.api.turtle.TurtleCommandResult; import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.api.turtle.TurtleSide; import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.core.apis.IAPIEnvironment;
import dan200.computercraft.core.metrics.Metrics; import dan200.computercraft.core.metrics.Metrics;
import dan200.computercraft.core.metrics.MetricsObserver;
import dan200.computercraft.shared.computer.core.ServerComputer;
import dan200.computercraft.shared.peripheral.generic.methods.AbstractInventoryMethods; import dan200.computercraft.shared.peripheral.generic.methods.AbstractInventoryMethods;
import dan200.computercraft.shared.turtle.core.*; import dan200.computercraft.shared.turtle.core.*;
@ -64,11 +65,11 @@ import java.util.Optional;
* @cc.since 1.3 * @cc.since 1.3
*/ */
public class TurtleAPI implements ILuaAPI { public class TurtleAPI implements ILuaAPI {
private final IAPIEnvironment environment; private final MetricsObserver metrics;
private final TurtleAccessInternal turtle; private final TurtleAccessInternal turtle;
public TurtleAPI(IAPIEnvironment environment, TurtleAccessInternal turtle) { public TurtleAPI(ServerComputer computer, TurtleAccessInternal turtle) {
this.environment = environment; this.metrics = computer.getMetrics();
this.turtle = turtle; this.turtle = turtle;
} }
@ -78,7 +79,7 @@ public class TurtleAPI implements ILuaAPI {
} }
private MethodResult trackCommand(TurtleCommand command) { private MethodResult trackCommand(TurtleCommand command) {
environment.observe(Metrics.TURTLE_OPS); metrics.observe(Metrics.TURTLE_OPS);
return turtle.executeCommand(command); return turtle.executeCommand(command);
} }
@ -169,7 +170,7 @@ public class TurtleAPI implements ILuaAPI {
*/ */
@LuaFunction @LuaFunction
public final MethodResult dig(Optional<TurtleSide> side) { public final MethodResult dig(Optional<TurtleSide> side) {
environment.observe(Metrics.TURTLE_OPS); metrics.observe(Metrics.TURTLE_OPS);
return trackCommand(TurtleToolCommand.dig(InteractDirection.FORWARD, side.orElse(null))); return trackCommand(TurtleToolCommand.dig(InteractDirection.FORWARD, side.orElse(null)));
} }
@ -184,7 +185,7 @@ public class TurtleAPI implements ILuaAPI {
*/ */
@LuaFunction @LuaFunction
public final MethodResult digUp(Optional<TurtleSide> side) { public final MethodResult digUp(Optional<TurtleSide> side) {
environment.observe(Metrics.TURTLE_OPS); metrics.observe(Metrics.TURTLE_OPS);
return trackCommand(TurtleToolCommand.dig(InteractDirection.UP, side.orElse(null))); return trackCommand(TurtleToolCommand.dig(InteractDirection.UP, side.orElse(null)));
} }
@ -199,7 +200,7 @@ public class TurtleAPI implements ILuaAPI {
*/ */
@LuaFunction @LuaFunction
public final MethodResult digDown(Optional<TurtleSide> side) { public final MethodResult digDown(Optional<TurtleSide> side) {
environment.observe(Metrics.TURTLE_OPS); metrics.observe(Metrics.TURTLE_OPS);
return trackCommand(TurtleToolCommand.dig(InteractDirection.DOWN, side.orElse(null))); return trackCommand(TurtleToolCommand.dig(InteractDirection.DOWN, side.orElse(null)));
} }

View File

@ -78,7 +78,7 @@ public class TurtleBlockEntity extends AbstractComputerBlockEntity implements Ba
getFamily(), Config.turtleTermWidth, getFamily(), Config.turtleTermWidth,
Config.turtleTermHeight Config.turtleTermHeight
); );
computer.addAPI(new TurtleAPI(computer.getAPIEnvironment(), brain)); computer.addAPI(new TurtleAPI(computer, brain));
brain.setupComputer(computer); brain.setupComputer(computer);
return computer; return computer;
} }

View File

@ -290,8 +290,7 @@ public class TurtleBrain implements TurtleAccessInternal {
newTurtle.transferStateFrom(oldOwner); newTurtle.transferStateFrom(oldOwner);
var computer = newTurtle.createServerComputer(); var computer = newTurtle.createServerComputer();
computer.setLevel((ServerLevel) world); computer.setPosition((ServerLevel) world, pos);
computer.setPosition(pos);
// Remove the old turtle // Remove the old turtle
oldWorld.removeBlock(oldPos, false); oldWorld.removeBlock(oldPos, false);
@ -608,7 +607,7 @@ public class TurtleBrain implements TurtleAccessInternal {
// If we've got a computer, ensure that we're allowed to perform work. // If we've got a computer, ensure that we're allowed to perform work.
var computer = owner.getServerComputer(); var computer = owner.getServerComputer();
if (computer != null && !computer.getComputer().getMainThreadMonitor().canWork()) return; if (computer != null && !computer.getMainThreadMonitor().canWork()) return;
// Pull a new command // Pull a new command
var nextCommand = commandQueue.poll(); var nextCommand = commandQueue.poll();
@ -621,7 +620,7 @@ public class TurtleBrain implements TurtleAccessInternal {
// Dispatch the callback // Dispatch the callback
if (computer == null) return; if (computer == null) return;
computer.getComputer().getMainThreadMonitor().trackWork(end - start, TimeUnit.NANOSECONDS); computer.getMainThreadMonitor().trackWork(end - start, TimeUnit.NANOSECONDS);
var callbackID = nextCommand.callbackID(); var callbackID = nextCommand.callbackID();
if (callbackID < 0) return; if (callbackID < 0) return;

View File

@ -52,7 +52,7 @@ public final class TurtlePlayer {
var player = brain.cachedPlayer; var player = brain.cachedPlayer;
if (player == null || player.player.getGameProfile() != getProfile(access.getOwningPlayer()) if (player == null || player.player.getGameProfile() != getProfile(access.getOwningPlayer())
|| player.player.getCommandSenderWorld() != access.getLevel()) { || player.player.level() != access.getLevel()) {
player = brain.cachedPlayer = create(brain); player = brain.cachedPlayer = create(brain);
} else { } else {
player.setState(access); player.setState(access);