Skip to content

Commit

Permalink
Final cleanup, optimisation and bullshit check removal
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Jan 19, 2023
1 parent 3dca840 commit 5aa58b3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public BulkCellInventory(MEGABulkCell cell, ItemStack o, ISaveProvider container
builder.addAll(config.keySet());
this.partitionList = builder.build();

this.compressed = CompressionHandler.INSTANCE.getVariants(filterItem, false);
this.decompressed = CompressionHandler.INSTANCE.getVariants(filterItem, true);
this.compressed = CompressionService.INSTANCE.getVariants(filterItem, false);
this.decompressed = CompressionService.INSTANCE.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 @@ -18,32 +18,34 @@
import appeng.api.stacks.AEItemKey;
import appeng.core.AppEng;

import gripe._90.megacells.util.Utils;

public class CompressionHandler {
public static final CompressionHandler INSTANCE = new CompressionHandler();
public class CompressionService {
public static final CompressionService INSTANCE = new CompressionService();

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

private CompressionHandler() {
private CompressionService() {
}

public Object2IntMap<AEItemKey> getVariants(AEItemKey key, boolean decompress) {
// Retrieve the (optional) variant chain containing the given item key
var variantChain = compressionChains.stream().filter(chain -> chain.containsKey(key)).findFirst();

return variantChain.map(chain -> {
var keys = new ObjectArrayList<>(chain.keySet());

// Reverse ordering when going from provided storage/filter variant to least-compressed "base unit"
if (decompress) {
Collections.reverse(keys);
}

// Split variant chain into separate compressed/decompressed chains, omitting the initial variant provided
var variants = new Object2IntLinkedOpenHashMap<AEItemKey>();
keys.subList(keys.indexOf(key) + 1, keys.size()).forEach(k -> variants.put(k, chain.getInt(k)));
return variants;
}).orElseGet(Object2IntLinkedOpenHashMap::new);
}

public void init() {
public void load() {
// Clear old variant cache in case of the server restarting or recipes being reloaded
compressionChains.clear();

Expand All @@ -61,39 +63,39 @@ public void init() {
var validRecipes = candidates.stream().filter(recipe -> {
var compressible = false;
var decompressible = false;
var constantAmount = false;

var input = recipe.getIngredients().get(0);
var output = recipe.getResultItem();

for (var candidate : candidates) {
var compressionMultiplier = 0;
var decompressionMultiplier = 0;
var checkAgainst = candidates.stream().filter(isCompressionRecipe(recipe)
? this::isDecompressionRecipe
: this::isCompressionRecipe).toList();

for (var candidate : checkAgainst) {
for (var item : candidate.getIngredients().get(0).getItems()) {
if (item.getItem().equals(output.getItem())) {
compressible = true;
compressionMultiplier = candidate.getIngredients().size();
}
}

for (var item : input.getItems()) {
if (item.getItem().equals(candidate.getResultItem().getItem())) {
decompressible = true;
decompressionMultiplier = candidate.getResultItem().getCount();
}
}

constantAmount = compressionMultiplier == decompressionMultiplier;
if (compressible && decompressible) {
break;
}
}

return compressible && decompressible && constantAmount;
return compressible && decompressible;
}).toList();

// Pull all available compression chains from the recipe shortlist and add these to the handler cache
var compressed = validRecipes.stream().filter(this::isCompressionRecipe).toList();
var decompressed = validRecipes.stream().filter(this::isDecompressionRecipe).toList();

// Pull all available compression chains from the recipe shortlist and add these to the handler cache
compressed.forEach(recipe -> {
var baseVariant = recipe.getResultItem().getItem();

Expand All @@ -106,23 +108,28 @@ public void init() {
}

var compressionChain = new Object2IntLinkedOpenHashMap<AEItemKey>();
var decompressionKeys = new ObjectArrayList<>(decompressionChain.keySet());

// Reverse current "decompression chain" and add base variant as the next compression step
Collections.reverse(decompressionKeys);
decompressionKeys.forEach(k -> compressionChain.put(k, decompressionChain.getInt(k)));
compressionChain.put(AEItemKey.of(baseVariant), compressionChain.getInt(compressionChain.lastKey()));

for (var higherVariant = getSubsequentVariant(baseVariant, compressed); higherVariant != null;) {
compressionChain.put(AEItemKey.of(higherVariant.first()), (int) higherVariant.second());
higherVariant = getSubsequentVariant(higherVariant.first(), compressed);
}

compressionChains.add(compressionChain);
// Collate decompression and compression chains together with base variant
var fullChain = new Object2IntLinkedOpenHashMap<AEItemKey>();

var decompressionKeys = new ObjectArrayList<>(decompressionChain.keySet());
Collections.reverse(decompressionKeys);
decompressionKeys.forEach(k -> fullChain.put(k, decompressionChain.getInt(k)));

// Retrieve appropriate multiplier for base variant for completion's sake
fullChain.put(AEItemKey.of(baseVariant), fullChain.isEmpty()
? compressionChain.getInt(compressionChain.firstKey())
: fullChain.getInt(fullChain.lastKey()));
fullChain.putAll(compressionChain);

compressionChains.add(fullChain);
}
});

Utils.LOGGER.info("Loaded bulk cell compression recipes.");
}

private boolean isCompressionRecipe(CraftingRecipe recipe) {
Expand All @@ -140,15 +147,9 @@ private Pair<Item, Integer> getSubsequentVariant(Item item, List<CraftingRecipe>
for (var recipe : recipes) {
for (var input : recipe.getIngredients().get(0).getItems()) {
if (input.getItem().equals(item)) {
var multiplier = 1;

if (isCompressionRecipe(recipe)) {
multiplier = recipe.getIngredients().size();
} else if (isDecompressionRecipe(recipe)) {
multiplier = recipe.getResultItem().getCount();
}

return Pair.of(recipe.getResultItem().getItem(), multiplier);
return Pair.of(recipe.getResultItem().getItem(), isCompressionRecipe(recipe)
? recipe.getIngredients().size()
: recipe.getResultItem().getCount());
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions fabric/src/main/java/gripe/_90/megacells/MEGACellsFabric.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import gripe._90.megacells.init.client.InitRenderTypes;
import gripe._90.megacells.init.client.InitScreens;
import gripe._90.megacells.integration.appbot.AppBotItems;
import gripe._90.megacells.item.cell.CompressionHandler;
import gripe._90.megacells.item.cell.CompressionService;
import gripe._90.megacells.menu.MEGAPatternProviderMenu;
import gripe._90.megacells.util.Utils;

Expand Down Expand Up @@ -52,14 +52,14 @@ public void onAe2Initialized() {
InitStorageCells.init();
InitUpgrades.init();

ServerLifecycleEvents.SERVER_STARTED.register(server -> CompressionHandler.INSTANCE.init());
ServerLifecycleEvents.SERVER_STARTED.register(server -> CompressionService.INSTANCE.load());
ServerLifecycleEvents.END_DATA_PACK_RELOAD.register((server, resourceManager, success) -> {
if (success)
CompressionHandler.INSTANCE.init();
CompressionService.INSTANCE.load();
});
CommonLifecycleEvents.TAGS_LOADED.register((registries, client) -> {
if (client)
CompressionHandler.INSTANCE.init();
CompressionService.INSTANCE.load();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import gripe._90.megacells.integration.appbot.AppBotItems;
import gripe._90.megacells.integration.appmek.AppMekIntegration;
import gripe._90.megacells.integration.appmek.AppMekItems;
import gripe._90.megacells.item.cell.CompressionHandler;
import gripe._90.megacells.item.cell.CompressionService;
import gripe._90.megacells.menu.MEGAPatternProviderMenu;
import gripe._90.megacells.util.Utils;

Expand Down Expand Up @@ -88,9 +88,9 @@ public MEGACellsForge() {
});
});

MinecraftForge.EVENT_BUS.addListener((ServerStartedEvent event) -> CompressionHandler.INSTANCE.init());
MinecraftForge.EVENT_BUS.addListener((AddReloadListenerEvent event) -> CompressionHandler.INSTANCE.init());
MinecraftForge.EVENT_BUS.addListener((RecipesUpdatedEvent event) -> CompressionHandler.INSTANCE.init());
MinecraftForge.EVENT_BUS.addListener((ServerStartedEvent event) -> CompressionService.INSTANCE.load());
MinecraftForge.EVENT_BUS.addListener((AddReloadListenerEvent event) -> CompressionService.INSTANCE.load());
MinecraftForge.EVENT_BUS.addListener((RecipesUpdatedEvent event) -> CompressionService.INSTANCE.load());

DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> InitAutoRotatingModel::init);
DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> InitBlockEntityRenderers::init);
Expand Down

0 comments on commit 5aa58b3

Please sign in to comment.