1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-11-14 05:44:52 +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.lua.ILuaAPI;
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.ComputerEnvironment;
import dan200.computercraft.core.computer.ComputerSide;
@ -74,31 +74,20 @@ public class ServerComputer implements InputHandler, ComputerEnvironment {
return level;
}
public void setLevel(ServerLevel level) {
this.level = level;
}
public BlockPos getPosition() {
return position;
}
public void setPosition(BlockPos pos) {
position = new BlockPos(pos);
}
public IAPIEnvironment getAPIEnvironment() {
return computer.getAPIEnvironment();
}
public Computer getComputer() {
return computer;
public void setPosition(ServerLevel level, BlockPos pos) {
this.level = level;
position = pos.immutable();
}
protected void markTerminalChanged() {
terminalChanged.set(true);
}
public void tickServer() {
protected void tickServer() {
ticksSincePing++;
computer.tick();
if (terminalChanged.getAndSet(false)) onTerminalChanged();
@ -116,10 +105,15 @@ public class ServerComputer implements InputHandler, ComputerEnvironment {
ticksSincePing = 0;
}
public boolean hasTimedOut() {
boolean hasTimedOut() {
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() {
return computer.pollAndResetChanges();
}
@ -189,25 +183,21 @@ public class ServerComputer implements InputHandler, ComputerEnvironment {
@Override
public void turnOn() {
// Turn on
computer.turnOn();
}
@Override
public void shutdown() {
// Shutdown
computer.shutdown();
}
@Override
public void reboot() {
// Reboot
computer.reboot();
}
@Override
public void queueEvent(String event, @Nullable Object[] arguments) {
// Queue event
computer.queueEvent(event, arguments);
}
@ -259,6 +249,10 @@ public class ServerComputer implements InputHandler, ComputerEnvironment {
return metrics;
}
public WorkMonitor getMainThreadMonitor() {
return computer.getMainThreadMonitor();
}
@Override
public @Nullable WritableMount createRootMount() {
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) {
if (entity != null) {
setLevel((ServerLevel) entity.getCommandSenderWorld());
setPosition(entity.blockPosition());
}
if (entity != null) setPosition((ServerLevel) entity.level(), entity.blockPosition());
// If a new entity has picked it up then rebroadcast the terminal to them
if (entity != this.entity && entity instanceof ServerPlayer) markTerminalChanged();
@ -150,7 +147,7 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces
}
@Override
public void tickServer() {
protected void tickServer() {
super.tickServer();
// 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;
}
private boolean tick(ItemStack stack, Level world, Entity entity, PocketServerComputer computer) {
private boolean tick(ItemStack stack, Entity entity, PocketServerComputer computer) {
var upgrade = getUpgrade(stack);
computer.setLevel((ServerLevel) world);
computer.updateValues(entity, stack, upgrade);
var changed = false;
@ -113,7 +112,7 @@ public class PocketComputerItem extends Item implements IComputerItem, IMedia, I
var computer = createServerComputer((ServerLevel) world, entity, inventory, stack);
computer.keepAlive();
var changed = tick(stack, world, entity, computer);
var changed = tick(stack, entity, computer);
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;
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;
}
@ -215,7 +214,7 @@ public class PocketComputerItem extends Item implements IComputerItem, IMedia, I
if (inventory != null) inventory.setChanged();
}
computer.setLevel(level);
return computer;
}

View File

@ -25,7 +25,7 @@ public class PocketModemPeripheral extends WirelessModemPeripheral {
void setLocation(IPocketAccess access) {
var entity = access.getEntity();
if (entity != null) {
level = entity.getCommandSenderWorld();
level = entity.level();
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.TurtleCommandResult;
import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.core.apis.IAPIEnvironment;
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.turtle.core.*;
@ -64,11 +65,11 @@ import java.util.Optional;
* @cc.since 1.3
*/
public class TurtleAPI implements ILuaAPI {
private final IAPIEnvironment environment;
private final MetricsObserver metrics;
private final TurtleAccessInternal turtle;
public TurtleAPI(IAPIEnvironment environment, TurtleAccessInternal turtle) {
this.environment = environment;
public TurtleAPI(ServerComputer computer, TurtleAccessInternal turtle) {
this.metrics = computer.getMetrics();
this.turtle = turtle;
}
@ -78,7 +79,7 @@ public class TurtleAPI implements ILuaAPI {
}
private MethodResult trackCommand(TurtleCommand command) {
environment.observe(Metrics.TURTLE_OPS);
metrics.observe(Metrics.TURTLE_OPS);
return turtle.executeCommand(command);
}
@ -169,7 +170,7 @@ public class TurtleAPI implements ILuaAPI {
*/
@LuaFunction
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)));
}
@ -184,7 +185,7 @@ public class TurtleAPI implements ILuaAPI {
*/
@LuaFunction
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)));
}
@ -199,7 +200,7 @@ public class TurtleAPI implements ILuaAPI {
*/
@LuaFunction
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)));
}

View File

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

View File

@ -290,8 +290,7 @@ public class TurtleBrain implements TurtleAccessInternal {
newTurtle.transferStateFrom(oldOwner);
var computer = newTurtle.createServerComputer();
computer.setLevel((ServerLevel) world);
computer.setPosition(pos);
computer.setPosition((ServerLevel) world, pos);
// Remove the old turtle
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.
var computer = owner.getServerComputer();
if (computer != null && !computer.getComputer().getMainThreadMonitor().canWork()) return;
if (computer != null && !computer.getMainThreadMonitor().canWork()) return;
// Pull a new command
var nextCommand = commandQueue.poll();
@ -621,7 +620,7 @@ public class TurtleBrain implements TurtleAccessInternal {
// Dispatch the callback
if (computer == null) return;
computer.getComputer().getMainThreadMonitor().trackWork(end - start, TimeUnit.NANOSECONDS);
computer.getMainThreadMonitor().trackWork(end - start, TimeUnit.NANOSECONDS);
var callbackID = nextCommand.callbackID();
if (callbackID < 0) return;

View File

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