From dd5d3df2fb2a400890f6acbdaac9167e544696e6 Mon Sep 17 00:00:00 2001 From: 90 Date: Sat, 26 Aug 2023 21:10:15 +0100 Subject: [PATCH] Switch to static methods again for compression service --- .../item/cell/BulkCellInventory.java | 4 ++-- .../megacells/service/CompressionService.java | 23 ++++++++----------- .../service/DecompressionService.java | 2 +- .../_90/megacells/fabric/FabricPlatform.java | 4 ++-- .../_90/megacells/forge/ForgePlatform.java | 4 ++-- 5 files changed, 17 insertions(+), 20 deletions(-) diff --git a/common/src/main/java/gripe/_90/megacells/item/cell/BulkCellInventory.java b/common/src/main/java/gripe/_90/megacells/item/cell/BulkCellInventory.java index 7572f042..6648bb4c 100644 --- a/common/src/main/java/gripe/_90/megacells/item/cell/BulkCellInventory.java +++ b/common/src/main/java/gripe/_90/megacells/item/cell/BulkCellInventory.java @@ -60,8 +60,8 @@ public BulkCellInventory(MEGABulkCell cell, ItemStack o, ISaveProvider container builder.addAll(config.keySet()); this.partitionList = builder.build(); - this.compressed = CompressionService.INSTANCE.getVariants(filterItem, false); - this.decompressed = CompressionService.INSTANCE.getVariants(filterItem, true); + this.compressed = CompressionService.getVariants(filterItem, false); + this.decompressed = CompressionService.getVariants(filterItem, true); this.unitFactor = decompressed.values().intStream().asLongStream().reduce(1, Math::multiplyExact); this.storedItem = getTag().contains(KEY) ? AEItemKey.fromTag(getTag().getCompound(KEY)) : null; diff --git a/common/src/main/java/gripe/_90/megacells/service/CompressionService.java b/common/src/main/java/gripe/_90/megacells/service/CompressionService.java index 01cf25f9..8ee04212 100644 --- a/common/src/main/java/gripe/_90/megacells/service/CompressionService.java +++ b/common/src/main/java/gripe/_90/megacells/service/CompressionService.java @@ -20,23 +20,19 @@ import appeng.api.stacks.AEItemKey; public class CompressionService { - public static final CompressionService INSTANCE = new CompressionService(); - // Each chain is an ordered map with the items themselves as the keys and the values being the amount of either: // - the item itself, needed to compress to its next variant // - the next variant, when decompressing the item // This value is typically either 4 or 9 for any given item. - private final Set> compressionChains = new ObjectLinkedOpenHashSet<>(); - - private CompressionService() {} + private static final Set> compressionChains = new ObjectLinkedOpenHashSet<>(); - public Optional> getChain(AEItemKey key) { + public static Optional> getChain(AEItemKey key) { return compressionChains.stream() .filter(chain -> chain.containsKey(key)) .findFirst(); } - public Object2IntMap getVariants(AEItemKey key, boolean decompress) { + public static Object2IntMap getVariants(AEItemKey key, boolean decompress) { return getChain(key) .map(chain -> { var keys = new ObjectArrayList<>(chain.keySet()); @@ -55,7 +51,7 @@ public Object2IntMap getVariants(AEItemKey key, boolean decompress) { .orElseGet(Object2IntLinkedOpenHashMap::new); } - public void loadRecipes(RecipeManager recipeManager, RegistryAccess access) { + public static void loadRecipes(RecipeManager recipeManager, RegistryAccess access) { // Clear old variant cache in case of the server restarting or recipes being reloaded compressionChains.clear(); @@ -91,19 +87,20 @@ public void loadRecipes(RecipeManager recipeManager, RegistryAccess access) { } } - private boolean isCompressionRecipe(CraftingRecipe recipe, RegistryAccess access) { + private static boolean isCompressionRecipe(CraftingRecipe recipe, RegistryAccess access) { return (recipe.getIngredients().size() == 4 || recipe.getIngredients().size() == 9) && recipe.getIngredients().stream().distinct().count() <= 1 && recipe.getResultItem(access).getCount() == 1; } - private boolean isDecompressionRecipe(CraftingRecipe recipe, RegistryAccess access) { + private static boolean isDecompressionRecipe(CraftingRecipe recipe, RegistryAccess access) { return (recipe.getResultItem(access).getCount() == 4 || recipe.getResultItem(access).getCount() == 9) && recipe.getIngredients().size() == 1; } - private boolean isReversibleRecipe(CraftingRecipe recipe, List candidates, RegistryAccess access) { + private static boolean isReversibleRecipe( + CraftingRecipe recipe, List candidates, RegistryAccess access) { var compressible = false; var decompressible = false; @@ -129,7 +126,7 @@ private boolean isReversibleRecipe(CraftingRecipe recipe, List c return false; } - private Object2IntMap generateChain( + private static Object2IntMap generateChain( Item baseVariant, List compressed, List decompressed, @@ -171,7 +168,7 @@ private Object2IntMap generateChain( return fullChain; } - private Pair getNextVariant(Item item, List recipes, RegistryAccess access) { + private static Pair getNextVariant(Item item, List recipes, RegistryAccess access) { for (var recipe : recipes) { for (var input : recipe.getIngredients().get(0).getItems()) { if (input.getItem().equals(item)) { diff --git a/common/src/main/java/gripe/_90/megacells/service/DecompressionService.java b/common/src/main/java/gripe/_90/megacells/service/DecompressionService.java index d355ce93..c85a630d 100644 --- a/common/src/main/java/gripe/_90/megacells/service/DecompressionService.java +++ b/common/src/main/java/gripe/_90/megacells/service/DecompressionService.java @@ -56,7 +56,7 @@ public void onServerStartTick() { } private Optional> getChain(BulkCellInventory cell) { - return CompressionService.INSTANCE.getChain(cell.getStoredItem()).map(c -> { + return CompressionService.getChain(cell.getStoredItem()).map(c -> { var keys = new ObjectArrayList<>(c.keySet()); Collections.reverse(keys); diff --git a/fabric/src/main/java/gripe/_90/megacells/fabric/FabricPlatform.java b/fabric/src/main/java/gripe/_90/megacells/fabric/FabricPlatform.java index fdfd1f86..4bbeeb3b 100644 --- a/fabric/src/main/java/gripe/_90/megacells/fabric/FabricPlatform.java +++ b/fabric/src/main/java/gripe/_90/megacells/fabric/FabricPlatform.java @@ -29,9 +29,9 @@ public boolean isAddonLoaded(Addons addon) { @Override public void initCompression() { ServerLifecycleEvents.SERVER_STARTED.register( - server -> CompressionService.INSTANCE.loadRecipes(server.getRecipeManager(), server.registryAccess())); + server -> CompressionService.loadRecipes(server.getRecipeManager(), server.registryAccess())); ServerLifecycleEvents.END_DATA_PACK_RELOAD.register((server, resourceManager, success) -> { - if (success) CompressionService.INSTANCE.loadRecipes(server.getRecipeManager(), server.registryAccess()); + if (success) CompressionService.loadRecipes(server.getRecipeManager(), server.registryAccess()); }); } } diff --git a/forge/src/main/java/gripe/_90/megacells/forge/ForgePlatform.java b/forge/src/main/java/gripe/_90/megacells/forge/ForgePlatform.java index f251bcde..11aed8fc 100644 --- a/forge/src/main/java/gripe/_90/megacells/forge/ForgePlatform.java +++ b/forge/src/main/java/gripe/_90/megacells/forge/ForgePlatform.java @@ -38,9 +38,9 @@ public boolean isAddonLoaded(Addons addon) { @Override public void initCompression() { - MinecraftForge.EVENT_BUS.addListener((ServerStartedEvent event) -> CompressionService.INSTANCE.loadRecipes( + MinecraftForge.EVENT_BUS.addListener((ServerStartedEvent event) -> CompressionService.loadRecipes( event.getServer().getRecipeManager(), event.getServer().registryAccess())); - MinecraftForge.EVENT_BUS.addListener((AddReloadListenerEvent event) -> CompressionService.INSTANCE.loadRecipes( + MinecraftForge.EVENT_BUS.addListener((AddReloadListenerEvent event) -> CompressionService.loadRecipes( event.getServerResources().getRecipeManager(), event.getRegistryAccess())); } }