From f43b5c5b8b117d2b176cc015baf05d8129496871 Mon Sep 17 00:00:00 2001 From: 90 Date: Mon, 6 Nov 2023 15:47:46 +0000 Subject: [PATCH] Add upgrades support to source cells --- .../gripe/_90/arseng/ArsEnergistique.java | 14 ++++++++--- .../arseng/item/PortableSourceCellItem.java | 20 ++++++++++++--- .../gripe/_90/arseng/item/SourceCellItem.java | 15 +++++++++++ .../_90/arseng/me/cell/ISourceCellItem.java | 15 +++++------ .../_90/arseng/me/cell/SourceCellHandler.java | 25 +++++++++++++++---- .../arseng/me/cell/SourceCellInventory.java | 18 +++++++------ .../_90/arseng/me/client/SourceRenderer.java | 3 --- .../strategy/SourceContainerItemStrategy.java | 4 +++ 8 files changed, 85 insertions(+), 29 deletions(-) diff --git a/src/main/java/gripe/_90/arseng/ArsEnergistique.java b/src/main/java/gripe/_90/arseng/ArsEnergistique.java index b4db19e..37dcf8c 100644 --- a/src/main/java/gripe/_90/arseng/ArsEnergistique.java +++ b/src/main/java/gripe/_90/arseng/ArsEnergistique.java @@ -3,7 +3,6 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.ModLoadingContext; import net.minecraftforge.fml.common.Mod; @@ -16,6 +15,9 @@ import appeng.api.client.AEKeyRendering; import appeng.api.client.StorageCellModels; import appeng.api.storage.StorageCells; +import appeng.api.upgrades.Upgrades; +import appeng.core.definitions.AEItems; +import appeng.core.localization.GuiText; import appeng.parts.automation.StackWorldBehaviors; import gripe._90.arseng.definition.ArsEngBlocks; @@ -56,6 +58,13 @@ public ArsEnergistique() { StorageCells.addCellHandler(SourceCellHandler.INSTANCE); StorageCells.addCellHandler(CreativeSourceCellHandler.INSTANCE); + ArsEngItems.getCells() + .forEach(cell -> Upgrades.add(AEItems.VOID_CARD, cell, 1, GuiText.StorageCells.getTranslationKey())); + ArsEngItems.getPortables().forEach(cell -> { + Upgrades.add(AEItems.ENERGY_CARD, cell, 2, GuiText.PortableCells.getTranslationKey()); + Upgrades.add(AEItems.VOID_CARD, cell, 1, GuiText.PortableCells.getTranslationKey()); + }); + bus.addListener(ArsEngCapabilities::register); MinecraftForge.EVENT_BUS.addGenericListener(BlockEntity.class, ArsEngCapabilities::attach); @@ -63,7 +72,7 @@ public ArsEnergistique() { StackWorldBehaviors.registerExportStrategy(SourceKeyType.TYPE, SourceStorageExportStrategy::new); StackWorldBehaviors.registerExternalStorageStrategy(SourceKeyType.TYPE, SourceExternalStorageStrategy::new); - ContainerItemStrategy.register(SourceKeyType.TYPE, SourceKey.class, new SourceContainerItemStrategy()); + ContainerItemStrategy.register(SourceKeyType.TYPE, SourceKey.class, SourceContainerItemStrategy.INSTANCE); GenericSlotCapacities.register(SourceKeyType.TYPE, (long) SourceContainerItemStrategy.MAX_SOURCE); bus.addListener(ArsEngItems::initP2PAttunement); @@ -74,7 +83,6 @@ public ArsEnergistique() { } } - @OnlyIn(Dist.CLIENT) private static class Client { private static void init() { var bus = FMLJavaModLoadingContext.get().getModEventBus(); diff --git a/src/main/java/gripe/_90/arseng/item/PortableSourceCellItem.java b/src/main/java/gripe/_90/arseng/item/PortableSourceCellItem.java index 29cab7a..f4dc3aa 100644 --- a/src/main/java/gripe/_90/arseng/item/PortableSourceCellItem.java +++ b/src/main/java/gripe/_90/arseng/item/PortableSourceCellItem.java @@ -2,18 +2,22 @@ import java.util.List; import java.util.Objects; +import java.util.Optional; + +import org.jetbrains.annotations.NotNull; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.inventory.tooltip.TooltipComponent; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.level.Level; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.client.event.RegisterColorHandlersEvent; import appeng.api.storage.StorageCells; import appeng.api.storage.cells.CellState; +import appeng.api.upgrades.IUpgradeInventory; +import appeng.api.upgrades.UpgradeInventories; import appeng.api.upgrades.Upgrades; import appeng.items.storage.StorageTier; import appeng.items.tools.powered.AbstractPortableCell; @@ -56,12 +60,22 @@ public void appendHoverText(ItemStack stack, Level level, List lines, addCellInformationToTooltip(stack, lines); } + @NotNull + @Override + public Optional getTooltipImage(@NotNull ItemStack stack) { + return getCellTooltipImage(stack); + } + @Override public double getChargeRate(ItemStack stack) { return 80D * (Upgrades.getEnergyCardMultiplier(getUpgrades(stack)) + 1); } - @OnlyIn(Dist.CLIENT) + @Override + public IUpgradeInventory getUpgrades(ItemStack is) { + return UpgradeInventories.forItem(is, 3, this::onUpgradesChanged); + } + public static void initColours(RegisterColorHandlersEvent.Item event) { ArsEngItems.getPortables().forEach(portable -> event.register(PortableSourceCellItem::getColor, portable)); } diff --git a/src/main/java/gripe/_90/arseng/item/SourceCellItem.java b/src/main/java/gripe/_90/arseng/item/SourceCellItem.java index 3ae61d5..8350182 100644 --- a/src/main/java/gripe/_90/arseng/item/SourceCellItem.java +++ b/src/main/java/gripe/_90/arseng/item/SourceCellItem.java @@ -1,14 +1,18 @@ package gripe._90.arseng.item; import java.util.List; +import java.util.Optional; import org.jetbrains.annotations.NotNull; import net.minecraft.network.chat.Component; +import net.minecraft.world.inventory.tooltip.TooltipComponent; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.level.Level; +import appeng.api.upgrades.IUpgradeInventory; +import appeng.api.upgrades.UpgradeInventories; import appeng.items.AEBaseItem; import appeng.items.storage.StorageTier; @@ -36,9 +40,20 @@ public double getIdleDrain() { return tier.idleDrain(); } + @Override + public IUpgradeInventory getUpgrades(ItemStack is) { + return UpgradeInventories.forItem(is, 1); + } + @Override public void appendHoverText( @NotNull ItemStack is, Level level, @NotNull List lines, @NotNull TooltipFlag advancedTooltips) { addCellInformationToTooltip(is, lines); } + + @NotNull + @Override + public Optional getTooltipImage(@NotNull ItemStack stack) { + return getCellTooltipImage(stack); + } } diff --git a/src/main/java/gripe/_90/arseng/me/cell/ISourceCellItem.java b/src/main/java/gripe/_90/arseng/me/cell/ISourceCellItem.java index 61cbb03..0cfc736 100644 --- a/src/main/java/gripe/_90/arseng/me/cell/ISourceCellItem.java +++ b/src/main/java/gripe/_90/arseng/me/cell/ISourceCellItem.java @@ -1,15 +1,16 @@ package gripe._90.arseng.me.cell; import java.util.List; +import java.util.Optional; import com.google.common.base.Preconditions; import net.minecraft.network.chat.Component; +import net.minecraft.world.inventory.tooltip.TooltipComponent; import net.minecraft.world.item.ItemStack; import appeng.api.config.FuzzyMode; import appeng.api.storage.cells.ICellWorkbenchItem; -import appeng.util.ConfigInventory; public interface ISourceCellItem extends ICellWorkbenchItem { long getTotalBytes(); @@ -18,12 +19,7 @@ public interface ISourceCellItem extends ICellWorkbenchItem { @Override default boolean isEditable(ItemStack is) { - return false; - } - - @Override - default ConfigInventory getConfigInventory(ItemStack is) { - return null; + return true; } @Override @@ -38,4 +34,9 @@ default void addCellInformationToTooltip(ItemStack is, List lines) { Preconditions.checkArgument(is.getItem() == this); SourceCellHandler.INSTANCE.addCellInformationToTooltip(is, lines); } + + default Optional getCellTooltipImage(ItemStack is) { + Preconditions.checkArgument(is.getItem() == this); + return SourceCellHandler.INSTANCE.getTooltipImage(is); + } } diff --git a/src/main/java/gripe/_90/arseng/me/cell/SourceCellHandler.java b/src/main/java/gripe/_90/arseng/me/cell/SourceCellHandler.java index ad15313..e1537a6 100644 --- a/src/main/java/gripe/_90/arseng/me/cell/SourceCellHandler.java +++ b/src/main/java/gripe/_90/arseng/me/cell/SourceCellHandler.java @@ -1,19 +1,23 @@ package gripe._90.arseng.me.cell; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.Optional; import org.jetbrains.annotations.Nullable; import net.minecraft.network.chat.Component; +import net.minecraft.world.inventory.tooltip.TooltipComponent; import net.minecraft.world.item.ItemStack; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.client.event.RegisterColorHandlersEvent; import appeng.api.storage.cells.ICellHandler; import appeng.api.storage.cells.ISaveProvider; +import appeng.core.AEConfig; import appeng.core.localization.Tooltips; import appeng.items.storage.BasicStorageCell; +import appeng.items.storage.StorageCellTooltipComponent; import gripe._90.arseng.definition.ArsEngItems; @@ -33,17 +37,28 @@ public SourceCellInventory getCellInventory(ItemStack is, @Nullable ISaveProvide return isCell(is) ? new SourceCellInventory((ISourceCellItem) is.getItem(), is, container) : null; } - @OnlyIn(Dist.CLIENT) public static void initLED(RegisterColorHandlersEvent.Item event) { ArsEngItems.getCells().forEach(cell -> event.register(BasicStorageCell::getColor, cell)); } - @OnlyIn(Dist.CLIENT) - public void addCellInformationToTooltip(ItemStack is, List lines) { + void addCellInformationToTooltip(ItemStack is, List lines) { var handler = getCellInventory(is, null); if (handler != null) { lines.add(Tooltips.bytesUsed(handler.getUsedBytes(), handler.getTotalBytes())); } } + + Optional getTooltipImage(ItemStack is) { + var handler = getCellInventory(is, null); + if (handler == null) return Optional.empty(); + + var upgrades = new ArrayList(); + + if (AEConfig.instance().isTooltipShowCellUpgrades()) { + handler.getUpgrades().forEach(upgrades::add); + } + + return Optional.of(new StorageCellTooltipComponent(upgrades, Collections.emptyList(), false, false)); + } } diff --git a/src/main/java/gripe/_90/arseng/me/cell/SourceCellInventory.java b/src/main/java/gripe/_90/arseng/me/cell/SourceCellInventory.java index 888768b..e92bfe5 100644 --- a/src/main/java/gripe/_90/arseng/me/cell/SourceCellInventory.java +++ b/src/main/java/gripe/_90/arseng/me/cell/SourceCellInventory.java @@ -13,6 +13,8 @@ import appeng.api.storage.cells.CellState; import appeng.api.storage.cells.ISaveProvider; import appeng.api.storage.cells.StorageCell; +import appeng.api.upgrades.IUpgradeInventory; +import appeng.core.definitions.AEItems; import gripe._90.arseng.me.key.SourceKey; import gripe._90.arseng.me.key.SourceKeyType; @@ -74,6 +76,10 @@ public double getIdleDrain() { return cell.getIdleDrain(); } + public IUpgradeInventory getUpgrades() { + return cell.getUpgrades(stack); + } + protected void saveChanges() { isPersisted = false; @@ -87,22 +93,18 @@ protected void saveChanges() { @Override public long insert(AEKey what, long amount, Actionable mode, IActionSource source) { - if (amount == 0 || !(what instanceof SourceKey) || sourceAmount == getMaxSource()) { + if (amount == 0 || !(what instanceof SourceKey)) { return 0; } - long remainingAmount = Math.max(0, getMaxSource() - sourceAmount); - - if (amount > remainingAmount) { - amount = remainingAmount; - } + var inserted = Math.min(amount, Math.max(0, getMaxSource() - sourceAmount)); if (mode == Actionable.MODULATE) { - sourceAmount += amount; + sourceAmount += inserted; saveChanges(); } - return amount; + return getUpgrades().isInstalled(AEItems.VOID_CARD) ? amount : inserted; } @Override diff --git a/src/main/java/gripe/_90/arseng/me/client/SourceRenderer.java b/src/main/java/gripe/_90/arseng/me/client/SourceRenderer.java index da958ee..8e5ab0f 100644 --- a/src/main/java/gripe/_90/arseng/me/client/SourceRenderer.java +++ b/src/main/java/gripe/_90/arseng/me/client/SourceRenderer.java @@ -12,8 +12,6 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.world.inventory.InventoryMenu; import net.minecraft.world.level.Level; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.api.distmarker.OnlyIn; import appeng.api.client.AEKeyRenderHandler; import appeng.client.gui.style.Blitter; @@ -21,7 +19,6 @@ import gripe._90.arseng.me.key.SourceKey; import gripe._90.arseng.me.key.SourceKeyType; -@OnlyIn(Dist.CLIENT) public class SourceRenderer implements AEKeyRenderHandler { public static final SourceRenderer INSTANCE = new SourceRenderer(); public static final Material SOURCE = diff --git a/src/main/java/gripe/_90/arseng/me/strategy/SourceContainerItemStrategy.java b/src/main/java/gripe/_90/arseng/me/strategy/SourceContainerItemStrategy.java index cc340f8..9d085e0 100644 --- a/src/main/java/gripe/_90/arseng/me/strategy/SourceContainerItemStrategy.java +++ b/src/main/java/gripe/_90/arseng/me/strategy/SourceContainerItemStrategy.java @@ -22,6 +22,10 @@ public class SourceContainerItemStrategy implements ContainerItemStrategy { public static final int MAX_SOURCE = 10000; + public static final SourceContainerItemStrategy INSTANCE = new SourceContainerItemStrategy(); + + private SourceContainerItemStrategy() {} + private boolean isSourceJar(ItemStack stack) { return stack != null && stack.getItem() instanceof BlockItem blockItem