1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-08-29 08:42:17 +00:00

Fix: register missing data pack reload listener.

Fixes "ComputerCraft may be installed incorrectly" and "File not found"
errors that sometimes happened after switching single player worlds or
running /reload.
This commit is contained in:
Toad-Dev
2022-01-01 19:46:27 -08:00
parent 6982841f6c
commit 767a919ad5
2 changed files with 31 additions and 17 deletions

View File

@@ -12,12 +12,12 @@ import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.filesystem.IMount;
import dan200.computercraft.core.apis.handles.ArrayByteChannel;
import dan200.computercraft.shared.util.IoUtil;
import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
import net.fabricmc.fabric.api.resource.SimpleResourceReloadListener;
import net.minecraft.ResourceLocationException;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.PreparableReloadListener;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.packs.resources.SimplePreparableReloadListener;
import net.minecraft.util.profiling.ProfilerFiller;
import javax.annotation.Nonnull;
@@ -30,6 +30,8 @@ import java.nio.channels.ReadableByteChannel;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
public final class ResourceMount implements IMount
@@ -282,30 +284,37 @@ public final class ResourceMount implements IMount
}
/**
* A {@link PreparableReloadListener} which reloads any associated mounts and correctly updates the resource manager
* An {@link IdentifiableResourceReloadListener} which reloads any associated mounts and correctly updates the resource manager
* they point to.
*/
public static final SimplePreparableReloadListener<Void> RELOAD_LISTENER = new SimplePreparableReloadListener<>()
public static final IdentifiableResourceReloadListener RELOAD_LISTENER = new SimpleResourceReloadListener<Void>()
{
@Nonnull
@Override
protected Void prepare( @Nonnull ResourceManager manager, @Nonnull ProfilerFiller profiler )
public ResourceLocation getFabricId()
{
profiler.push( "Reloading ComputerCraft mounts" );
try
{
for( ResourceMount mount : MOUNT_CACHE.values() ) mount.load( manager );
}
finally
{
profiler.pop();
}
return null;
return new ResourceLocation( ComputerCraft.MOD_ID, "resource_mount_reload_listener" );
}
@Override
protected void apply( @Nonnull Void result, @Nonnull ResourceManager manager, @Nonnull ProfilerFiller profiler )
public CompletableFuture<Void> load( ResourceManager manager, ProfilerFiller profiler, Executor executor )
{
return CompletableFuture.runAsync( () -> {
profiler.push( "Reloading ComputerCraft mounts" );
try
{
for( ResourceMount mount : MOUNT_CACHE.values() ) mount.load( manager );
}
finally
{
profiler.pop();
}
}, executor );
}
@Override
public CompletableFuture<Void> apply( Void data, ResourceManager manager, ProfilerFiller profiler, Executor executor )
{
return CompletableFuture.runAsync( () -> {}, executor );
}
};
}

View File

@@ -11,6 +11,7 @@ import dan200.computercraft.api.media.IMedia;
import dan200.computercraft.api.peripheral.IPeripheralTile;
import dan200.computercraft.api.turtle.event.TurtleEvent;
import dan200.computercraft.core.computer.MainThread;
import dan200.computercraft.core.filesystem.ResourceMount;
import dan200.computercraft.core.tracking.Tracking;
import dan200.computercraft.shared.TurtlePermissions;
import dan200.computercraft.shared.command.CommandComputerCraft;
@@ -34,9 +35,11 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerBlockEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.packs.PackType;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.RecordItem;
import net.minecraft.world.level.block.entity.BlockEntity;
@@ -137,6 +140,8 @@ public final class ComputerCraftProxyCommon
TurtleEvent.EVENT_BUS.register( FurnaceRefuelHandler.INSTANCE );
TurtleEvent.EVENT_BUS.register( new TurtlePermissions() );
ResourceManagerHelper.get( PackType.SERVER_DATA ).registerReloadListener( ResourceMount.RELOAD_LISTENER );
}
public static void registerLoot()