Skip to content

Commit

Permalink
Add upgrades support to source cells
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Nov 6, 2023
1 parent 939c953 commit f43b5c5
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 29 deletions.
14 changes: 11 additions & 3 deletions src/main/java/gripe/_90/arseng/ArsEnergistique.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -56,14 +58,21 @@ 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);

StackWorldBehaviors.registerImportStrategy(SourceKeyType.TYPE, SourceStorageImportStrategy::new);
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);
Expand All @@ -74,7 +83,6 @@ public ArsEnergistique() {
}
}

@OnlyIn(Dist.CLIENT)
private static class Client {
private static void init() {
var bus = FMLJavaModLoadingContext.get().getModEventBus();
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/gripe/_90/arseng/item/PortableSourceCellItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,12 +60,22 @@ public void appendHoverText(ItemStack stack, Level level, List<Component> lines,
addCellInformationToTooltip(stack, lines);
}

@NotNull
@Override
public Optional<TooltipComponent> 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));
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/gripe/_90/arseng/item/SourceCellItem.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<Component> lines, @NotNull TooltipFlag advancedTooltips) {
addCellInformationToTooltip(is, lines);
}

@NotNull
@Override
public Optional<TooltipComponent> getTooltipImage(@NotNull ItemStack stack) {
return getCellTooltipImage(stack);
}
}
15 changes: 8 additions & 7 deletions src/main/java/gripe/_90/arseng/me/cell/ISourceCellItem.java
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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
Expand All @@ -38,4 +34,9 @@ default void addCellInformationToTooltip(ItemStack is, List<Component> lines) {
Preconditions.checkArgument(is.getItem() == this);
SourceCellHandler.INSTANCE.addCellInformationToTooltip(is, lines);
}

default Optional<TooltipComponent> getCellTooltipImage(ItemStack is) {
Preconditions.checkArgument(is.getItem() == this);
return SourceCellHandler.INSTANCE.getTooltipImage(is);
}
}
25 changes: 20 additions & 5 deletions src/main/java/gripe/_90/arseng/me/cell/SourceCellHandler.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<Component> lines) {
void addCellInformationToTooltip(ItemStack is, List<Component> lines) {
var handler = getCellInventory(is, null);

if (handler != null) {
lines.add(Tooltips.bytesUsed(handler.getUsedBytes(), handler.getTotalBytes()));
}
}

Optional<TooltipComponent> getTooltipImage(ItemStack is) {
var handler = getCellInventory(is, null);
if (handler == null) return Optional.empty();

var upgrades = new ArrayList<ItemStack>();

if (AEConfig.instance().isTooltipShowCellUpgrades()) {
handler.getUpgrades().forEach(upgrades::add);
}

return Optional.of(new StorageCellTooltipComponent(upgrades, Collections.emptyList(), false, false));
}
}
18 changes: 10 additions & 8 deletions src/main/java/gripe/_90/arseng/me/cell/SourceCellInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,6 +76,10 @@ public double getIdleDrain() {
return cell.getIdleDrain();
}

public IUpgradeInventory getUpgrades() {
return cell.getUpgrades(stack);
}

protected void saveChanges() {
isPersisted = false;

Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/gripe/_90/arseng/me/client/SourceRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
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;

import gripe._90.arseng.me.key.SourceKey;
import gripe._90.arseng.me.key.SourceKeyType;

@OnlyIn(Dist.CLIENT)
public class SourceRenderer implements AEKeyRenderHandler<SourceKey> {
public static final SourceRenderer INSTANCE = new SourceRenderer();
public static final Material SOURCE =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public class SourceContainerItemStrategy
implements ContainerItemStrategy<SourceKey, SourceContainerItemStrategy.Context> {
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
Expand Down

0 comments on commit f43b5c5

Please sign in to comment.