1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-14 20:20:30 +00:00

Require computers to have a fixed ID

Moves ID assigning out of the Computer class and into wherever we
construct the ServerComputer (so in computer blocks and pocket computer
items).

This is definitely not perfect - it'd be nice to make ServerComputers
more responsible for managing the lifecycle of computers (so assigning
ids, handling auto-starting, etc...), but I've not found a good way to
handle this yet!
This commit is contained in:
Jonathan Coates 2022-10-21 19:51:23 +01:00
parent 695ef0542a
commit cee60cdb5b
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
11 changed files with 24 additions and 66 deletions

View File

@ -32,7 +32,7 @@ public class Computer
private static final int START_DELAY = 50;
// Various properties of the computer
private int id;
private final int id;
private String label = null;
// Read-only fields about the computer
@ -51,6 +51,7 @@ public class Computer
public Computer( IComputerEnvironment environment, Terminal terminal, int id )
{
if( id < 0 ) throw new IllegalStateException( "Id has not been assigned" );
this.id = id;
this.environment = environment;
this.terminal = terminal;
@ -135,20 +136,6 @@ public class Computer
return id;
}
public int assignID()
{
if( id < 0 )
{
id = environment.assignNewID();
}
return id;
}
public void setID( int id )
{
this.id = id;
}
public String getLabel()
{
return label;

View File

@ -345,13 +345,7 @@ final class ComputerExecutor
private IWritableMount getRootMount()
{
if( rootMount == null )
{
rootMount = computer.getComputerEnvironment().createSaveDirMount(
"computer/" + computer.assignID(),
computer.getComputerEnvironment().getComputerSpaceLimit()
);
}
if( rootMount == null ) rootMount = computer.getComputerEnvironment().createRootMount();
return rootMount;
}

View File

@ -68,7 +68,7 @@ public final class Environment implements IAPIEnvironment
@Override
public int getComputerID()
{
return computer.assignID();
return computer.getID();
}
@Nonnull

View File

@ -18,18 +18,14 @@ public interface IComputerEnvironment
double getTimeOfDay();
long getComputerSpaceLimit();
@Nonnull
String getHostString();
@Nonnull
String getUserAgent();
int assignNewID();
@Nullable
IWritableMount createSaveDirMount( String subPath, long capacity );
IWritableMount createRootMount();
@Nullable
IMount createResourceMount( String domain, String subPath );

View File

@ -74,7 +74,7 @@ public class ComputerPeripheral implements IPeripheral
@LuaFunction
public final int getID()
{
return computer.assignID();
return computer.getID();
}
/**

View File

@ -68,7 +68,7 @@ public final class ComputerProxy
}
}
public int assignID()
public int getID()
{
TileComputerBase tile = getTile();
ServerComputer computer = tile.getServerComputer();

View File

@ -5,6 +5,7 @@
*/
package dan200.computercraft.shared.computer.blocks;
import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.computer.ComputerSide;
import dan200.computercraft.shared.BundledRedstone;
@ -16,6 +17,7 @@ import dan200.computercraft.shared.computer.core.ServerComputer;
import dan200.computercraft.shared.computer.core.ServerComputerRegistry;
import dan200.computercraft.shared.network.container.ComputerContainerData;
import dan200.computercraft.shared.util.DirectionUtil;
import dan200.computercraft.shared.util.IDAssigner;
import dan200.computercraft.shared.util.RedstoneUtil;
import joptsimple.internal.Strings;
import net.minecraft.block.BlockState;
@ -167,6 +169,7 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT
public void tick()
{
if( getLevel().isClientSide ) return;
if( computerID < 0 && !startOn ) return; // Don't tick if we don't need a computer!
ServerComputer computer = createServerComputer();
@ -348,8 +351,6 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT
if( getLevel().isClientSide || computerID == id ) return;
computerID = id;
ServerComputer computer = getServerComputer();
if( computer != null ) computer.setID( computerID );
setChanged();
}
@ -371,7 +372,7 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT
}
@Nonnull
public ServerComputer createServerComputer()
public final ServerComputer createServerComputer()
{
if( getLevel().isClientSide ) throw new IllegalStateException( "Cannot access server computer on the client." );
@ -380,6 +381,12 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT
ServerComputer computer = ServerComputerRegistry.INSTANCE.get( instanceID );
if( computer == null )
{
if( computerID < 0 )
{
computerID = ComputerCraftAPI.createUniqueNumberedSaveDir( level, IDAssigner.COMPUTER );
updateBlock();
}
computer = createComputer( computerID );
instanceID = computer.register();
fresh = true;

View File

@ -172,11 +172,6 @@ public class ServerComputer implements InputHandler, IComputerEnvironment
{
}
public void setID( int id )
{
computer.setID( id );
}
public int getInstanceID()
{
return instanceID;
@ -286,9 +281,9 @@ public class ServerComputer implements InputHandler, IComputerEnvironment
}
@Override
public IWritableMount createSaveDirMount( String subPath, long capacity )
public IWritableMount createRootMount()
{
return ComputerCraftAPI.createSaveDirMount( world, subPath, capacity );
return ComputerCraftAPI.createSaveDirMount( world, "computer/" + computer.getID(), ComputerCraft.computerSpaceLimit );
}
@Override
@ -303,12 +298,6 @@ public class ServerComputer implements InputHandler, IComputerEnvironment
return ComputerCraftAPIImpl.getResourceFile( domain, subPath );
}
@Override
public long getComputerSpaceLimit()
{
return ComputerCraft.computerSpaceLimit;
}
@Nonnull
@Override
public String getHostString()
@ -322,10 +311,4 @@ public class ServerComputer implements InputHandler, IComputerEnvironment
{
return ComputerCraft.MOD_ID + "/" + ComputerCraftAPI.getInstalledVersion();
}
@Override
public int assignNewID()
{
return ComputerCraftAPI.createUniqueNumberedSaveDir( world, "computer" );
}
}

View File

@ -21,6 +21,7 @@ import dan200.computercraft.shared.network.container.ComputerContainerData;
import dan200.computercraft.shared.pocket.apis.PocketAPI;
import dan200.computercraft.shared.pocket.core.PocketServerComputer;
import dan200.computercraft.shared.pocket.inventory.PocketComputerMenuProvider;
import dan200.computercraft.shared.util.IDAssigner;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.ItemEntity;
@ -233,7 +234,7 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia, I
int computerID = getComputerID( stack );
if( computerID < 0 )
{
computerID = ComputerCraftAPI.createUniqueNumberedSaveDir( world, "computer" );
computerID = ComputerCraftAPI.createUniqueNumberedSaveDir( world, IDAssigner.COMPUTER );
setComputerID( stack, computerID );
}

View File

@ -26,6 +26,8 @@ import java.util.Map;
public final class IDAssigner
{
public static final String COMPUTER = "computer";
private static final FolderName FOLDER = new FolderName( ComputerCraft.MOD_ID );
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static final Type ID_TOKEN = new TypeToken<Map<String, Integer>>()

View File

@ -39,13 +39,7 @@ public class BasicEnvironment implements IComputerEnvironment
}
@Override
public int assignNewID()
{
return 0;
}
@Override
public IWritableMount createSaveDirMount( String path, long space )
public IWritableMount createRootMount()
{
return mount;
}
@ -62,12 +56,6 @@ public class BasicEnvironment implements IComputerEnvironment
return 0;
}
@Override
public long getComputerSpaceLimit()
{
return ComputerCraft.computerSpaceLimit;
}
@Nonnull
@Override
public String getHostString()