Resolve a few TODOs

- Update ForgeConfigAPI to the latest version, to fix the race
   condition.
 - Move WirelessNetwork lifecycle management to ServerContext.
 - Some doc fixes.
This commit is contained in:
Jonathan Coates 2022-12-03 15:55:48 +00:00
parent 87c6d3aef6
commit fa122a56cf
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
10 changed files with 26 additions and 41 deletions

View File

@ -27,7 +27,7 @@ nightConfig = "3.6.5"
slf4j = "1.7.36"
# Minecraft mods
forgeConfig = "4.2.6"
forgeConfig = "4.2.7"
iris = "1.19.x-v1.4.0"
jei = "11.3.0.262"
modmenu = "4.1.0"

View File

@ -24,7 +24,6 @@
import dan200.computercraft.shared.computer.core.ServerContext;
import dan200.computercraft.shared.details.BlockDetails;
import dan200.computercraft.shared.details.ItemDetails;
import dan200.computercraft.shared.peripheral.modem.wireless.WirelessNetwork;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
@ -89,7 +88,7 @@ public final void registerMediaProvider(MediaProvider provider) {
@Override
public final PacketNetwork getWirelessNetwork(MinecraftServer server) {
return WirelessNetwork.getUniversal();
return ServerContext.get(server).wirelessNetwork();
}
@Override

View File

@ -12,7 +12,6 @@
import dan200.computercraft.shared.computer.core.ResourceMount;
import dan200.computercraft.shared.computer.core.ServerContext;
import dan200.computercraft.shared.computer.metrics.ComputerMBean;
import dan200.computercraft.shared.peripheral.modem.wireless.WirelessNetwork;
import dan200.computercraft.shared.peripheral.monitor.MonitorWatcher;
import dan200.computercraft.shared.util.DropConsumer;
import dan200.computercraft.shared.util.TickScheduler;
@ -70,7 +69,6 @@ public static void onServerStopped() {
private static void resetState() {
ServerContext.close();
WirelessNetwork.resetNetworks();
NetworkUtils.reset();
}

View File

@ -8,6 +8,7 @@
import com.google.common.annotations.VisibleForTesting;
import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.api.filesystem.Mount;
import dan200.computercraft.api.network.PacketNetwork;
import dan200.computercraft.core.ComputerContext;
import dan200.computercraft.core.computer.ComputerThread;
import dan200.computercraft.core.computer.GlobalEnvironment;
@ -18,6 +19,7 @@
import dan200.computercraft.shared.CommonHooks;
import dan200.computercraft.shared.computer.metrics.GlobalMetrics;
import dan200.computercraft.shared.config.Config;
import dan200.computercraft.shared.peripheral.modem.wireless.WirelessNetwork;
import dan200.computercraft.shared.util.IDAssigner;
import net.minecraft.SharedConstants;
import net.minecraft.server.MinecraftServer;
@ -58,6 +60,7 @@ public final class ServerContext {
private final ComputerContext context;
private final MainThread mainThread;
private final IDAssigner idAssigner;
private final WirelessNetwork wirelessNetwork = new WirelessNetwork();
private final Path storageDir;
private ServerContext(MinecraftServer server) {
@ -180,6 +183,17 @@ public GlobalMetrics metrics() {
return metrics;
}
/**
* Get the global wireless network.
* <p>
* Use {@link ComputerCraftAPI#getWirelessNetwork(MinecraftServer)} instead of this method.
*
* @return The wireless network.
*/
public PacketNetwork wirelessNetwork() {
return wirelessNetwork;
}
private record Environment(MinecraftServer server) implements GlobalEnvironment {
@Override
public @Nullable Mount createResourceMount(String domain, String subPath) {

View File

@ -408,17 +408,8 @@ private static void syncClient() {
public static void sync(ModConfig config) {
if (!config.getModId().equals(ComputerCraftAPI.MOD_ID)) return;
try {
if (config.getType() == ModConfig.Type.SERVER) syncServer();
if (config.getType() == ModConfig.Type.CLIENT) syncClient();
} catch (IllegalStateException e) {
// TODO: Remove when https://github.com/Fuzss/forgeconfigapiport-fabric/issues/26 is fixed.
if (e.getMessage() != null && e.getMessage().startsWith("Cannot get config value before config is loaded.")) {
return;
}
throw e;
}
if (config.getType() == ModConfig.Type.SERVER) syncServer();
if (config.getType() == ModConfig.Type.CLIENT) syncClient();
}
/**

View File

@ -9,25 +9,12 @@
import dan200.computercraft.api.network.PacketNetwork;
import dan200.computercraft.api.network.PacketReceiver;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class WirelessNetwork implements PacketNetwork {
// TODO: Move this to ServerContext.
private static @Nullable WirelessNetwork universalNetwork = null;
public static WirelessNetwork getUniversal() {
if (universalNetwork == null) universalNetwork = new WirelessNetwork();
return universalNetwork;
}
public static void resetNetworks() {
universalNetwork = null;
}
private final Set<PacketReceiver> receivers = Collections.newSetFromMap(new ConcurrentHashMap<>());
@Override

View File

@ -33,11 +33,7 @@ public int read(ByteBuffer destination) throws ClosedChannelException {
var remaining = Math.min(backing.limit() - position, destination.remaining());
// TODO: Switch to Java 17 methods on 1.18.x
var slice = backing.slice();
slice.position(position);
slice.limit(position + remaining);
destination.put(slice);
destination.put(position, backing, position, remaining);
position += remaining;
return remaining;
}

View File

@ -391,7 +391,7 @@ private void afterWork(Worker runner, ComputerExecutor executor) {
*/
long scaledPeriod() {
// FIXME: We access this on other threads (in TimeoutState), so their reads won't be consistent. This isn't
// "criticial" behaviour, so not clear if it matters too much.
// "critical" behaviour, so not clear if it matters too much.
// +1 to include the current task
var count = 1 + computerQueue.size();

View File

@ -35,7 +35,9 @@ addRemappedConfiguration("testWithSodium")
addRemappedConfiguration("testWithIris")
dependencies {
modImplementation(libs.bundles.externalMods.fabric)
modImplementation(libs.bundles.externalMods.fabric) {
exclude("curse.maven", "configured-fabric-667378") // Added by Forge Config API
}
modCompileOnly(libs.bundles.externalMods.fabric.compile) {
exclude("net.fabricmc", "fabric-loader")
exclude("net.fabricmc.fabric-api")

View File

@ -5,26 +5,24 @@
*/
package dan200.computercraft.api.peripheral;
import dan200.computercraft.api.ForgeComputerCraftAPI;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.common.util.LazyOptional;
/**
* This interface is used to create peripheral implementations for blocks.
* <p>
* If you have a {@link BlockEntity} which acts as a peripheral, you may alternatively expose the {@link IPeripheral}
* capability.
* <p>
* {@code dan200.computercraft.api.ForgeComputerCraftAPI#registerPeripheralProvider(IPeripheralProvider)} should be used
* to register a peripheral provider.
* {@link ForgeComputerCraftAPI#registerPeripheralProvider(IPeripheralProvider)} should be used to register a peripheral
* provider.
*/
@FunctionalInterface
public interface IPeripheralProvider {
// TODO(1.19.3): Move to Forge and fix link above.
/**
* Produce an peripheral implementation from a block location.
*