Skip to content

Commit

Permalink
better formatting, naming, commenting
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Jan 19, 2023
1 parent 955426f commit 3dca840
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public Object2IntMap<AEItemKey> getVariants(AEItemKey key, boolean decompress) {
Collections.reverse(keys);
}

var decompressed = new Object2IntLinkedOpenHashMap<AEItemKey>();
keys.subList(keys.indexOf(key) + 1, keys.size()).forEach(k -> decompressed.put(k, chain.getInt(k)));
return decompressed;
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 load() {
public void init() {
// Clear old variant cache in case of the server restarting or recipes being reloaded
compressionChains.clear();

Expand Down Expand Up @@ -90,29 +90,30 @@ public void load() {
return compressible && decompressible && constantAmount;
}).toList();

// Add final available variant chains to handler cache
// 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();

compressed.forEach(recipe -> {
var recipeOutput = recipe.getResultItem().getItem();
var baseVariant = recipe.getResultItem().getItem();

if (compressionChains.stream().noneMatch(chain -> chain.containsKey(AEItemKey.of(recipeOutput)))) {
if (compressionChains.stream().noneMatch(chain -> chain.containsKey(AEItemKey.of(baseVariant)))) {
var decompressionChain = new Object2IntLinkedOpenHashMap<AEItemKey>();

for (var lowerVariant = getSubsequentVariant(recipeOutput, decompressed); lowerVariant != null;) {
for (var lowerVariant = getSubsequentVariant(baseVariant, decompressed); lowerVariant != null;) {
decompressionChain.put(AEItemKey.of(lowerVariant.first()), (int) lowerVariant.second());
lowerVariant = getSubsequentVariant(lowerVariant.first(), decompressed);
}

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(recipeOutput), compressionChain.getInt(compressionChain.lastKey()));
compressionChain.put(AEItemKey.of(baseVariant), compressionChain.getInt(compressionChain.lastKey()));

for (var higherVariant = getSubsequentVariant(recipeOutput, compressed); higherVariant != null; ) {
for (var higherVariant = getSubsequentVariant(baseVariant, compressed); higherVariant != null;) {
compressionChain.put(AEItemKey.of(higherVariant.first()), (int) higherVariant.second());
higherVariant = getSubsequentVariant(higherVariant.first(), compressed);
}
Expand Down
6 changes: 3 additions & 3 deletions fabric/src/main/java/gripe/_90/megacells/MEGACellsFabric.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public void onAe2Initialized() {
InitStorageCells.init();
InitUpgrades.init();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ public MEGACellsForge() {
});
});

MinecraftForge.EVENT_BUS.addListener((ServerStartedEvent event) -> CompressionHandler.INSTANCE.load());
MinecraftForge.EVENT_BUS.addListener((AddReloadListenerEvent event) -> CompressionHandler.INSTANCE.load());
MinecraftForge.EVENT_BUS.addListener((RecipesUpdatedEvent event) -> CompressionHandler.INSTANCE.load());
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());

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

0 comments on commit 3dca840

Please sign in to comment.