1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-07 08:52:59 +00:00

Introduce IWorkMonitor into the public API

This effectively acts as a public interface to canExecuteExternal() and
consumeTime(). It's hopefully sufficiently general that we can mess
around with the backend as much as we like in the future.

One thing to note here is that this is based on a polling API, as it's
largely intended for people running work every tick. It would be
possible to adapt this with callbacks for when work is available,
etc..., but that was not needed immediately.

This also removes IComputerOwned, as Plethora no longer needs it.
This commit is contained in:
SquidDev
2019-03-20 21:26:56 +00:00
parent 853e2622a1
commit 5d05205d69
10 changed files with 138 additions and 104 deletions

View File

@@ -16,8 +16,7 @@ import dan200.computercraft.api.network.wired.IWiredNode;
import dan200.computercraft.api.network.wired.IWiredSender;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.computer.Computer;
import dan200.computercraft.core.computer.IComputerOwned;
import dan200.computercraft.api.peripheral.IWorkMonitor;
import dan200.computercraft.shared.peripheral.modem.ModemPeripheral;
import dan200.computercraft.shared.peripheral.modem.ModemState;
import net.minecraft.world.World;
@@ -267,7 +266,7 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW
return wrappers == null ? null : wrappers.get( remoteName );
}
private static class RemotePeripheralWrapper implements IComputerAccess, IComputerOwned
private static class RemotePeripheralWrapper implements IComputerAccess
{
private final WiredModemElement m_element;
private final IPeripheral m_peripheral;
@@ -376,6 +375,13 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW
m_computer.queueEvent( event, arguments );
}
@Nullable
@Override
public IWorkMonitor getMainThreadMonitor()
{
return m_computer.getMainThreadMonitor();
}
@Nonnull
@Override
public String getAttachmentName()
@@ -402,12 +408,5 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW
return m_element.getRemotePeripherals().get( name );
}
}
@Nullable
@Override
public Computer getComputer()
{
return m_computer instanceof IComputerOwned ? ((IComputerOwned) m_computer).getComputer() : null;
}
}
}

View File

@@ -40,6 +40,7 @@ import net.minecraftforge.items.IItemHandlerModifiable;
import javax.annotation.Nonnull;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class TurtleBrain implements ITurtleAccess
{
@@ -960,7 +961,7 @@ public class TurtleBrain implements ITurtleAccess
// If we've got a computer, ensure that we're allowed to perform work.
ServerComputer computer = m_owner.getServerComputer();
if( computer != null && !computer.getComputer().canExecuteMainThread() ) return;
if( computer != null && !computer.getComputer().getMainThreadMonitor().canWork() ) return;
// Pull a new command
TurtleCommandQueueEntry nextCommand = m_commandQueue.poll();
@@ -973,7 +974,7 @@ public class TurtleBrain implements ITurtleAccess
// Dispatch the callback
if( computer == null ) return;
computer.getComputer().afterExecuteMainThread( end - start );
computer.getComputer().getMainThreadMonitor().trackWork( end - start, TimeUnit.NANOSECONDS );
int callbackID = nextCommand.callbackID;
if( callbackID < 0 ) return;