Skip to content

Commit

Permalink
Allow source cells to be dismantled back into their housing and compo…
Browse files Browse the repository at this point in the history
…nent

Addresses #9
  • Loading branch information
62832 committed Nov 9, 2023
1 parent 4d00e57 commit 670d735
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/main/java/gripe/_90/arseng/ArsEnergistique.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import gripe._90.arseng.definition.ArsEngCreativeTab;
import gripe._90.arseng.definition.ArsEngItems;
import gripe._90.arseng.item.PortableSourceCellItem;
import gripe._90.arseng.item.SourceCellItem;
import gripe._90.arseng.me.cell.CreativeSourceCellHandler;
import gripe._90.arseng.me.cell.SourceCellHandler;
import gripe._90.arseng.me.client.SourceRenderer;
Expand Down Expand Up @@ -86,7 +87,7 @@ public ArsEnergistique() {
private static class Client {
private static void init() {
var bus = FMLJavaModLoadingContext.get().getModEventBus();
bus.addListener(SourceCellHandler::initLED);
bus.addListener(SourceCellItem::initColours);
bus.addListener(PortableSourceCellItem::initColours);

AEKeyRendering.register(SourceKeyType.TYPE, SourceKey.class, SourceRenderer.INSTANCE);
Expand Down
67 changes: 63 additions & 4 deletions src/main/java/gripe/_90/arseng/item/SourceCellItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@
import org.jetbrains.annotations.NotNull;

import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraftforge.client.event.RegisterColorHandlersEvent;

import appeng.api.storage.StorageCells;
import appeng.api.upgrades.IUpgradeInventory;
import appeng.api.upgrades.UpgradeInventories;
import appeng.core.localization.PlayerMessages;
import appeng.items.AEBaseItem;
import appeng.items.storage.BasicStorageCell;
import appeng.items.storage.StorageTier;

import gripe._90.arseng.definition.ArsEngItems;
import gripe._90.arseng.me.cell.ISourceCellItem;

public class SourceCellItem extends AEBaseItem implements ISourceCellItem {
Expand All @@ -41,19 +51,68 @@ public double getIdleDrain() {
}

@Override
public IUpgradeInventory getUpgrades(ItemStack is) {
return UpgradeInventories.forItem(is, 1);
public IUpgradeInventory getUpgrades(ItemStack stack) {
return UpgradeInventories.forItem(stack, 1);
}

@NotNull
@Override
public InteractionResultHolder<ItemStack> use(
@NotNull Level level, @NotNull Player player, @NotNull InteractionHand hand) {
var inHand = player.getItemInHand(hand);
disassemble(inHand, level, player);
return new InteractionResultHolder<>(InteractionResult.sidedSuccess(level.isClientSide()), inHand);
}

@Override
public InteractionResult onItemUseFirst(ItemStack stack, UseOnContext context) {
return disassemble(stack, context.getLevel(), context.getPlayer())
? InteractionResult.sidedSuccess(context.getLevel().isClientSide())
: InteractionResult.PASS;
}

private boolean disassemble(ItemStack stack, Level level, Player player) {
if (player != null && player.isShiftKeyDown()) {
if (level.isClientSide()) return false;

var playerInv = player.getInventory();
var cellInv = StorageCells.getCellInventory(stack, null);

if (cellInv != null && playerInv.getSelected() == stack) {
if (cellInv.getAvailableStacks().isEmpty()) {
playerInv.setItem(playerInv.selected, ItemStack.EMPTY);
playerInv.placeItemBackInInventory(
tier.componentSupplier().get().getDefaultInstance());

for (var upgrade : getUpgrades(stack)) {
playerInv.placeItemBackInInventory(upgrade);
}

playerInv.placeItemBackInInventory(ArsEngItems.SOURCE_CELL_HOUSING.stack());

return true;
} else {
player.displayClientMessage(PlayerMessages.OnlyEmptyCellsCanBeDisassembled.text(), true);
}
}
}

return false;
}

@Override
public void appendHoverText(
@NotNull ItemStack is, Level level, @NotNull List<Component> lines, @NotNull TooltipFlag advancedTooltips) {
addCellInformationToTooltip(is, lines);
@NotNull ItemStack stack, Level level, @NotNull List<Component> lines, @NotNull TooltipFlag advTooltips) {
addCellInformationToTooltip(stack, lines);
}

@NotNull
@Override
public Optional<TooltipComponent> getTooltipImage(@NotNull ItemStack stack) {
return getCellTooltipImage(stack);
}

public static void initColours(RegisterColorHandlersEvent.Item event) {
ArsEngItems.getCells().forEach(cell -> event.register(BasicStorageCell::getColor, cell));
}
}
8 changes: 0 additions & 8 deletions src/main/java/gripe/_90/arseng/me/cell/SourceCellHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@
import net.minecraft.network.chat.Component;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.ItemStack;
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;

public class SourceCellHandler implements ICellHandler {
public static final SourceCellHandler INSTANCE = new SourceCellHandler();

Expand All @@ -37,10 +33,6 @@ public SourceCellInventory getCellInventory(ItemStack is, @Nullable ISaveProvide
return isCell(is) ? new SourceCellInventory((ISourceCellItem) is.getItem(), is, container) : null;
}

public static void initLED(RegisterColorHandlersEvent.Item event) {
ArsEngItems.getCells().forEach(cell -> event.register(BasicStorageCell::getColor, cell));
}

void addCellInformationToTooltip(ItemStack is, List<Component> lines) {
var handler = getCellInventory(is, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public IUpgradeInventory getUpgrades() {
return cell.getUpgrades(stack);
}

protected void saveChanges() {
private void saveChanges() {
isPersisted = false;

if (container != null) {
Expand Down

0 comments on commit 670d735

Please sign in to comment.