diff --git a/changelog/2.0.7+1.20.2.md b/changelog/2.0.7+1.20.2.md new file mode 100644 index 0000000..adb05d2 --- /dev/null +++ b/changelog/2.0.7+1.20.2.md @@ -0,0 +1,2 @@ +* Fix old storages not being removed from the cache +* Added `/drawercache` command for debugging the cache \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index e92d5f1..b9f17e0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ minecraft_version=1.20.2 yarn_mappings=1.20.2+build.2 loader_version=0.14.22 -mod_version=2.0.6 +mod_version=2.0.7 maven_group=io.github.mattidragon archives_base_name=ExtendedDrawers diff --git a/src/main/java/io/github/mattidragon/extendeddrawers/ExtendedDrawers.java b/src/main/java/io/github/mattidragon/extendeddrawers/ExtendedDrawers.java index ee559f8..a44f251 100644 --- a/src/main/java/io/github/mattidragon/extendeddrawers/ExtendedDrawers.java +++ b/src/main/java/io/github/mattidragon/extendeddrawers/ExtendedDrawers.java @@ -2,6 +2,7 @@ import io.github.mattidragon.configloader.api.ConfigManager; import io.github.mattidragon.extendeddrawers.config.ConfigData; +import io.github.mattidragon.extendeddrawers.misc.DrawerCacheCommand; import io.github.mattidragon.extendeddrawers.misc.DrawerContentsLootFunction; import io.github.mattidragon.extendeddrawers.misc.ShiftAccess; import io.github.mattidragon.extendeddrawers.network.NetworkRegistry; @@ -49,6 +50,7 @@ public void onInitialize() { NetworkRegistry.register(); CompressionOverrideSyncPacket.register(); SetLimiterLimitPacket.register(); + DrawerCacheCommand.register(); ResourceManagerHelper.registerBuiltinResourcePack(id("alt"), MOD_CONTAINER, Text.translatable("resourcepack.extended_drawers.alt"), ResourcePackActivationType.NORMAL); ResourceManagerHelper.registerBuiltinResourcePack(id("dev"), MOD_CONTAINER, Text.translatable("resourcepack.extended_drawers.programmer_art"), ResourcePackActivationType.NORMAL); } diff --git a/src/main/java/io/github/mattidragon/extendeddrawers/misc/DrawerCacheCommand.java b/src/main/java/io/github/mattidragon/extendeddrawers/misc/DrawerCacheCommand.java new file mode 100644 index 0000000..ff25b3b --- /dev/null +++ b/src/main/java/io/github/mattidragon/extendeddrawers/misc/DrawerCacheCommand.java @@ -0,0 +1,61 @@ +package io.github.mattidragon.extendeddrawers.misc; + +import io.github.mattidragon.extendeddrawers.network.NetworkRegistry; +import io.github.mattidragon.extendeddrawers.network.NetworkStorageCache; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; +import net.minecraft.command.argument.BlockPosArgumentType; +import net.minecraft.server.command.CommandManager; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; + +import java.util.List; + +public class DrawerCacheCommand { + public static void register() { + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { + dispatcher.register(CommandManager.literal("drawercache") + .requires(source -> source.hasPermissionLevel(2)) + .then(CommandManager.argument("pos", BlockPosArgumentType.blockPos()) + .then(CommandManager.literal("print") + .executes(context -> { + var source = context.getSource(); + var pos = BlockPosArgumentType.getBlockPos(context, "pos"); + NetworkRegistry.UNIVERSE.getServerGraphWorld(source.getWorld()) + .getAllGraphsAt(pos) + .map(graph -> graph.getGraphEntity(NetworkRegistry.STORAGE_CACHE_TYPE)) + .map(NetworkStorageCache::getDebugInfo) + .flatMap(List::stream) + .forEach(line -> source.sendFeedback(() -> line, false)); + return 1; + })) + .then(CommandManager.literal("update") + .executes(context -> { + var source = context.getSource(); + var pos = BlockPosArgumentType.getBlockPos(context, "pos"); + var caches = NetworkRegistry.UNIVERSE.getServerGraphWorld(source.getWorld()) + .getAllGraphsAt(pos) + .map(graph -> graph.getGraphEntity(NetworkRegistry.STORAGE_CACHE_TYPE)) + .toList(); + + caches.forEach(NetworkStorageCache::addMissingStorages); + + source.sendFeedback(() -> Text.literal("Updated cache").formatted(Formatting.GREEN), false); + return 1; + }) + .then(CommandManager.literal("force") + .executes(context -> { + var source = context.getSource(); + var pos = BlockPosArgumentType.getBlockPos(context, "pos"); + var caches = NetworkRegistry.UNIVERSE.getServerGraphWorld(source.getWorld()) + .getAllGraphsAt(pos) + .map(graph -> graph.getGraphEntity(NetworkRegistry.STORAGE_CACHE_TYPE)) + .toList(); + + caches.forEach(NetworkStorageCache::forceCacheUpdate); + + source.sendFeedback(() -> Text.literal("Force updated cache").formatted(Formatting.GREEN), false); + return 1; + }))))); + }); + } +} diff --git a/src/main/java/io/github/mattidragon/extendeddrawers/network/NetworkStorageCache.java b/src/main/java/io/github/mattidragon/extendeddrawers/network/NetworkStorageCache.java index 657a64b..c721aa3 100644 --- a/src/main/java/io/github/mattidragon/extendeddrawers/network/NetworkStorageCache.java +++ b/src/main/java/io/github/mattidragon/extendeddrawers/network/NetworkStorageCache.java @@ -8,11 +8,15 @@ import com.kneelawk.graphlib.api.graph.user.*; import com.kneelawk.graphlib.api.util.LinkPos; import io.github.mattidragon.extendeddrawers.block.entity.StorageDrawerBlockEntity; +import io.github.mattidragon.extendeddrawers.network.node.CompactingDrawerBlockNode; +import io.github.mattidragon.extendeddrawers.network.node.DrawerBlockNode; import io.github.mattidragon.extendeddrawers.storage.DrawerStorage; import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; import net.fabricmc.fabric.api.transfer.v1.storage.base.CombinedStorage; import net.minecraft.nbt.NbtElement; import net.minecraft.server.world.ServerWorld; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; import net.minecraft.util.math.BlockPos; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -46,11 +50,14 @@ public CombinedStorage get() { return cachedStorage; } - private void addMissingStorages() { + public void addMissingStorages() { if (!missingPositions.isEmpty()) { missingPositions.forEach(pos -> { if (context.getBlockWorld().getBlockEntity(pos) instanceof StorageDrawerBlockEntity drawer) { - drawer.streamStorages().forEach(cachedStorage.parts::add); + drawer.streamStorages().forEach(storage -> { + cachedStorage.parts.add(storage); + positions.put(pos, storage); + }); } }); missingPositions.clear(); @@ -97,11 +104,13 @@ public void onNodeDestroyed(@NotNull NodeHolder node, @Nullable NodeE // Remove storages from cache positions.get(pos).forEach(cachedStorage.parts::remove); + positions.removeAll(pos); missingPositions.remove(pos); } public void onNodeUnloaded(BlockPos pos) { positions.get(pos).forEach(cachedStorage.parts::remove); + positions.removeAll(pos); missingPositions.add(pos); } @@ -148,5 +157,48 @@ public void merge(@NotNull NetworkStorageCache other) { return newCache; } + + public List getDebugInfo() { + var list = new ArrayList(); + list.add(Text.literal("Storage Cache Debug Info").formatted(Formatting.BOLD, Formatting.YELLOW)); + list.add(Text.literal(" %s uncached positions".formatted(missingPositions.size()))); + list.add(Text.literal(" %s cached positions".formatted(positions.size()))); + list.add(Text.literal(" %s storages".formatted(cachedStorage.parts.size()))); + list.add(Text.empty()); + + context.getGraph() + .getNodes() + .filter(holder -> holder.getNode() instanceof DrawerBlockNode || holder.getNode() instanceof CompactingDrawerBlockNode) + .map(NodeHolder::getBlockPos) + .forEach(pos -> { + list.add(Text.literal("%s".formatted(pos.toShortString())).formatted(Formatting.YELLOW)); + var isValid = false; + if (missingPositions.contains(pos)) { + list.add(Text.literal(" Not cached").formatted(Formatting.RED)); + isValid = true; + } + if (positions.containsKey(pos)) { + list.add(Text.literal(" Cached: %s storage(s)".formatted(positions.get(pos).size())).formatted(Formatting.GREEN)); + isValid = true; + } + + if (!isValid) { + list.add(Text.literal(" Missing from cache").formatted(Formatting.DARK_RED)); + } + }); + + return list; + } + + public void forceCacheUpdate() { + cachedStorage.parts.clear(); + positions.clear(); + missingPositions.clear(); + context.getGraph() + .getNodes() + .map(NodeHolder::getBlockPos) + .forEach(missingPositions::add); + addMissingStorages(); + } }