Skip to content

Commit

Permalink
More refactoring and reorganisation
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Nov 1, 2023
1 parent a9bbb0b commit d1c0647
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 58 deletions.
26 changes: 12 additions & 14 deletions common/src/main/java/gripe/_90/megacells/MEGACells.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import appeng.core.localization.GuiText;
import appeng.hotkeys.HotkeyActions;

import gripe._90.megacells.compression.DecompressionService;
import gripe._90.megacells.core.Addons;
import gripe._90.megacells.core.Platform;
import gripe._90.megacells.definition.MEGABlockEntities;
Expand All @@ -30,6 +29,7 @@
import gripe._90.megacells.integration.appbot.AppBotIntegration;
import gripe._90.megacells.integration.appbot.AppBotItems;
import gripe._90.megacells.item.cell.BulkCellItem;
import gripe._90.megacells.misc.DecompressionService;

public final class MEGACells {
private MEGACells() {}
Expand All @@ -52,41 +52,39 @@ public static void initCommon() {
MEGABlocks.init();
MEGABlockEntities.init();

if (MEGACells.PLATFORM.isAddonLoaded(Addons.APPBOT)) {
if (PLATFORM.isAddonLoaded(Addons.APPBOT)) {
AppBotItems.init();
}

initStorageCells();

MEGACells.PLATFORM.initCompression();
PLATFORM.initCompression();
GridServices.register(DecompressionService.class, DecompressionService.class);

MEGACells.PLATFORM.addVillagerTrade(MEGAItems.SKY_STEEL_INGOT, 8, 3, 20);
MEGACells.PLATFORM.addVillagerTrade(MEGAItems.ACCUMULATION_PROCESSOR_PRESS, 40, 1, 50);
PLATFORM.addVillagerTrade(MEGAItems.SKY_STEEL_INGOT, 8, 3, 20);
PLATFORM.addVillagerTrade(MEGAItems.ACCUMULATION_PROCESSOR_PRESS, 40, 1, 50);
}

private static void initStorageCells() {
Stream.of(MEGAItems.getItemCells(), MEGAItems.getItemPortables())
.flatMap(Collection::stream)
.forEach(c -> StorageCellModels.registerModel(c, MEGACells.makeId("block/drive/cells/mega_item_cell")));
.forEach(c -> StorageCellModels.registerModel(c, makeId("block/drive/cells/mega_item_cell")));
Stream.of(MEGAItems.getFluidCells(), MEGAItems.getFluidPortables())
.flatMap(Collection::stream)
.forEach(
c -> StorageCellModels.registerModel(c, MEGACells.makeId("block/drive/cells/mega_fluid_cell")));
.forEach(c -> StorageCellModels.registerModel(c, makeId("block/drive/cells/mega_fluid_cell")));

StorageCells.addCellHandler(BulkCellItem.HANDLER);
StorageCellModels.registerModel(MEGAItems.BULK_ITEM_CELL, MEGACells.makeId("block/drive/cells/bulk_item_cell"));
StorageCellModels.registerModel(MEGAItems.BULK_ITEM_CELL, makeId("block/drive/cells/bulk_item_cell"));

MEGAItems.getItemPortables()
.forEach(cell -> HotkeyActions.registerPortableCell(cell, HotkeyAction.PORTABLE_ITEM_CELL));
MEGAItems.getFluidPortables()
.forEach(cell -> HotkeyActions.registerPortableCell(cell, HotkeyAction.PORTABLE_FLUID_CELL));

if (MEGACells.PLATFORM.isAddonLoaded(Addons.APPBOT)) {
if (PLATFORM.isAddonLoaded(Addons.APPBOT)) {
Stream.of(AppBotItems.getCells(), AppBotItems.getPortables())
.flatMap(Collection::stream)
.forEach(c ->
StorageCellModels.registerModel(c, MEGACells.makeId("block/drive/cells/mega_mana_cell")));
.forEach(c -> StorageCellModels.registerModel(c, makeId("block/drive/cells/mega_mana_cell")));
}
}

Expand Down Expand Up @@ -152,11 +150,11 @@ public static void initUpgrades() {
Upgrades.add(MEGAItems.GREATER_ENERGY_CARD, portableCell, 2, portableCellGroup);
}

if (MEGACells.PLATFORM.isAddonLoaded(Addons.AE2WTLIB)) {
if (PLATFORM.isAddonLoaded(Addons.AE2WTLIB)) {
AE2WTIntegration.initUpgrades();
}

if (MEGACells.PLATFORM.isAddonLoaded(Addons.APPBOT)) {
if (PLATFORM.isAddonLoaded(Addons.APPBOT)) {
AppBotIntegration.initUpgrades();
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import appeng.menu.me.common.MEStorageMenu;

import gripe._90.megacells.MEGACells;
import gripe._90.megacells.compression.DecompressionPatternItem;
import gripe._90.megacells.item.DecompressionPatternItem;
import gripe._90.megacells.item.cell.BulkCellItem;
import gripe._90.megacells.item.cell.MEGAPortableCell;
import gripe._90.megacells.item.part.CellDockPart;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gripe._90.megacells.compression;
package gripe._90.megacells.item;

import org.jetbrains.annotations.Nullable;

Expand All @@ -9,6 +9,7 @@
import appeng.crafting.pattern.EncodedPatternItem;

import gripe._90.megacells.MEGACells;
import gripe._90.megacells.misc.DecompressionPattern;

public class DecompressionPatternItem extends EncodedPatternItem {
public DecompressionPatternItem(Properties properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import appeng.api.storage.cells.ISaveProvider;
import appeng.api.storage.cells.StorageCell;

import gripe._90.megacells.compression.CompressionChain;
import gripe._90.megacells.compression.CompressionService;
import gripe._90.megacells.misc.CompressionChain;
import gripe._90.megacells.misc.CompressionService;

public class BulkCellInventory implements StorageCell {
private static final String KEY = "key";
Expand Down Expand Up @@ -231,7 +231,7 @@ public void getAvailableStacks(KeyCounter out) {
var compressionFactor = BigInteger.valueOf(variant.factor());
var key = variant.item();

if (count.divide(compressionFactor).signum() == 1 && variant != chain.last()) {
if (count.divide(compressionFactor).signum() == 1 && variant != chain.get(chain.size() - 1)) {
out.add(key, count.remainder(compressionFactor).longValue());
count = count.divide(compressionFactor);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import appeng.parts.PartModel;

import gripe._90.megacells.MEGACells;
import gripe._90.megacells.compression.DecompressionPattern;
import gripe._90.megacells.compression.DecompressionService;
import gripe._90.megacells.misc.DecompressionPattern;
import gripe._90.megacells.misc.DecompressionService;

public class DecompressionModulePart extends AEBasePart implements ICraftingProvider, IGridTickable {
@PartModels
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gripe._90.megacells.compression;
package gripe._90.megacells.misc;

import java.math.BigInteger;
import java.util.Collections;
Expand All @@ -10,9 +10,9 @@

import gripe._90.megacells.definition.MEGAConfig;

public class CompressionChain extends ObjectArrayList<CompressionVariant> {
public class CompressionChain extends ObjectArrayList<CompressionService.Variant> {
public void add(AEItemKey item, byte factor) {
this.add(new CompressionVariant(item, factor));
this.add(new CompressionService.Variant(item, factor));
}

public boolean containsVariant(AEItemKey item) {
Expand All @@ -30,15 +30,11 @@ public BigInteger unitFactor(AEItemKey item) {
return subChain.stream().map(v -> BigInteger.valueOf(v.factor())).reduce(BigInteger.ONE, BigInteger::multiply);
}

public CompressionVariant last() {
return get(size - 1);
}

public CompressionChain lastMultiplierSwapped() {
var multipliers = this.stream().map(CompressionVariant::factor).collect(Collectors.toList());
var multipliers = this.stream().map(CompressionService.Variant::factor).collect(Collectors.toList());
Collections.rotate(multipliers, -1);

var items = this.stream().map(CompressionVariant::item).toList();
var items = this.stream().map(CompressionService.Variant::item).toList();
var chain = new CompressionChain();

for (var i = 0; i < items.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gripe._90.megacells.compression;
package gripe._90.megacells.misc;

import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -116,22 +116,21 @@ private CompressionChain generateChain(
return chain;
}

private CompressionVariant getNextVariant(
Item item, List<CraftingRecipe> recipes, boolean compressed, RegistryAccess access) {
private Variant getNextVariant(Item item, List<CraftingRecipe> recipes, boolean compressed, RegistryAccess access) {
for (var override : overrides) {
if (compressed && override.smaller.equals(item)) {
return new CompressionVariant(override.larger, override.factor);
return new Variant(override.larger, override.factor);
}

if (!compressed && override.larger.equals(item)) {
return new CompressionVariant(override.smaller, override.factor);
return new Variant(override.smaller, override.factor);
}
}

for (var recipe : recipes) {
for (var input : recipe.getIngredients().get(0).getItems()) {
if (input.getItem().equals(item)) {
return new CompressionVariant(recipe.getResultItem(access).getItem(), (byte)
return new Variant(recipe.getResultItem(access).getItem(), (byte)
(compressed
? recipe.getIngredients().size()
: recipe.getResultItem(access).getCount()));
Expand Down Expand Up @@ -225,5 +224,11 @@ private boolean overrideRecipe(CraftingRecipe recipe, RegistryAccess access) {
return false;
}

public record Variant(AEItemKey item, byte factor) {
private Variant(Item item, byte factor) {
this(AEItemKey.of(item), factor);
}
}

private record Override(Item smaller, Item larger, byte factor) {}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gripe._90.megacells.compression;
package gripe._90.megacells.misc;

import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gripe._90.megacells.compression;
package gripe._90.megacells.misc;

import java.util.Collections;
import java.util.List;
Expand All @@ -8,7 +8,6 @@
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;

import appeng.api.crafting.IPatternDetails;
import appeng.api.implementations.blockentities.IChestOrDrive;
Expand Down Expand Up @@ -83,14 +82,14 @@ private Set<IPatternDetails> generatePatterns(BulkCellInventory cell) {
var decompressionChain = fullChain.limited().reversed();

for (var variant : decompressionChain) {
if (variant == decompressionChain.last()) {
if (variant == decompressionChain.get(decompressionChain.size() - 1)) {
continue;
}

var pattern = new ItemStack(MEGAItems.DECOMPRESSION_PATTERN);
var pattern = MEGAItems.DECOMPRESSION_PATTERN.stack();
var decompressed = decompressionChain.get(decompressionChain.indexOf(variant) + 1);

encodePattern(pattern.getOrCreateTag(), decompressed.item(), variant, false);
encode(pattern.getOrCreateTag(), decompressed.item(), variant, false);
patterns.add(new DecompressionPattern(pattern));
}

Expand All @@ -101,17 +100,17 @@ private Set<IPatternDetails> generatePatterns(BulkCellInventory cell) {
continue;
}

var pattern = new ItemStack(MEGAItems.DECOMPRESSION_PATTERN);
var pattern = MEGAItems.DECOMPRESSION_PATTERN.stack();
var decompressed = compressionChain.get(compressionChain.indexOf(variant) - 1);

encodePattern(pattern.getOrCreateTag(), decompressed.item(), variant, true);
encode(pattern.getOrCreateTag(), decompressed.item(), variant, true);
patterns.add(new DecompressionPattern(pattern));
}

return patterns;
}

private void encodePattern(CompoundTag tag, AEItemKey base, CompressionVariant variant, boolean toCompress) {
private void encode(CompoundTag tag, AEItemKey base, CompressionService.Variant variant, boolean toCompress) {
tag.put(DecompressionPattern.NBT_VARIANT, variant.item().toTag());
tag.put(DecompressionPattern.NBT_BASE, base.toTag());
tag.putInt(DecompressionPattern.NBT_FACTOR, variant.factor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import appeng.core.definitions.AEBlocks;
import appeng.init.InitVillager;

import gripe._90.megacells.compression.CompressionService;
import gripe._90.megacells.core.Addons;
import gripe._90.megacells.core.Loaders;
import gripe._90.megacells.core.Platform;
import gripe._90.megacells.misc.CompressionService;

public final class FabricPlatform implements Platform {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
import appeng.core.definitions.AEBlocks;
import appeng.init.InitVillager;

import gripe._90.megacells.compression.CompressionService;
import gripe._90.megacells.core.Addons;
import gripe._90.megacells.core.Loaders;
import gripe._90.megacells.core.Platform;
import gripe._90.megacells.misc.CompressionService;

public final class ForgePlatform implements Platform {
@Override
Expand Down

0 comments on commit d1c0647

Please sign in to comment.