Skip to content

Commit

Permalink
Switch to static methods again for compression service
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Aug 26, 2023
1 parent d22546c commit dd5d3df
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 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 @@ -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<Object2IntMap<AEItemKey>> compressionChains = new ObjectLinkedOpenHashSet<>();

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

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 @@ -55,7 +51,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 @@ -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<CraftingRecipe> candidates, RegistryAccess access) {
private static boolean isReversibleRecipe(
CraftingRecipe recipe, List<CraftingRecipe> candidates, RegistryAccess access) {
var compressible = false;
var decompressible = false;

Expand All @@ -129,7 +126,7 @@ private boolean isReversibleRecipe(CraftingRecipe recipe, List<CraftingRecipe> c
return false;
}

private Object2IntMap<AEItemKey> generateChain(
private static Object2IntMap<AEItemKey> generateChain(
Item baseVariant,
List<CraftingRecipe> compressed,
List<CraftingRecipe> decompressed,
Expand Down Expand Up @@ -171,7 +168,7 @@ private Object2IntMap<AEItemKey> generateChain(
return fullChain;
}

private Pair<Item, Integer> getNextVariant(Item item, List<CraftingRecipe> recipes, RegistryAccess access) {
private static Pair<Item, Integer> getNextVariant(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 @@ -56,7 +56,7 @@ public void onServerStartTick() {
}

private Optional<Object2IntMap<AEItemKey>> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}

0 comments on commit dd5d3df

Please sign in to comment.