Skip to content

Commit

Permalink
Switch to static utility class over singleton for compression
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Aug 3, 2023
1 parent 1a95fa9 commit 29a6ffe
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,15 @@
import appeng.api.stacks.AEItemKey;

public class CompressionService {
public static final CompressionService INSTANCE = new CompressionService();
private static final Set<Object2IntMap<AEItemKey>> compressionChains = new ObjectLinkedOpenHashSet<>();

private final Set<Object2IntMap<AEItemKey>> compressionChains = new ObjectLinkedOpenHashSet<>();

private CompressionService() {}

public Optional<Object2IntMap<AEItemKey>> getChain(AEItemKey key) {
public static Optional<Object2IntMap<AEItemKey>> getChain(AEItemKey key) {
return compressionChains.stream()
.filter(chain -> chain.containsKey(key))
.findFirst();
}

public Object2IntMap<AEItemKey> getVariants(AEItemKey key, boolean decompress) {
public static Object2IntMap<AEItemKey> getVariants(AEItemKey key, boolean decompress) {
return getChain(key)
.map(chain -> {
var keys = new ObjectArrayList<>(chain.keySet());
Expand All @@ -52,7 +48,7 @@ public Object2IntMap<AEItemKey> 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();

Expand Down Expand Up @@ -152,19 +148,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 Pair<Item, Integer> getSubsequentVariant(Item item, List<CraftingRecipe> recipes, RegistryAccess access) {
private static Pair<Item, Integer> getSubsequentVariant(
Item item, List<CraftingRecipe> recipes, RegistryAccess access) {
for (var recipe : recipes) {
for (var input : recipe.getIngredients().get(0).getItems()) {
if (input.getItem().equals(item)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ private StorageCell getCellByDriveSlot(DriveBlockEntity drive, int slot) throws

private Object2IntMap<AEItemKey> getChain(BulkCellInventory cell) {
if (cell.isCompressionEnabled()) {
return CompressionService.INSTANCE
.getChain(cell.getStoredItem())
return CompressionService.getChain(cell.getStoredItem())
.map(c -> {
var keys = new ObjectArrayList<>(c.keySet());
Collections.reverse(keys);
Expand Down
4 changes: 2 additions & 2 deletions fabric/src/main/java/gripe/_90/megacells/MEGACells.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ private void registerAll() {

private 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());
CompressionService.loadRecipes(server.getRecipeManager(), server.registryAccess());
}
});

Expand Down
4 changes: 2 additions & 2 deletions forge/src/main/java/gripe/_90/megacells/forge/MEGACells.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ private void initCells(FMLCommonSetupEvent event) {
}

private 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()));

GridServices.register(DecompressionService.class, DecompressionService.class);
Expand Down

0 comments on commit 29a6ffe

Please sign in to comment.