diff --git a/src/main/java/gripe/_90/arseng/ArsEnergistique.java b/src/main/java/gripe/_90/arseng/ArsEnergistique.java index 37dcf8c..82a1a95 100644 --- a/src/main/java/gripe/_90/arseng/ArsEnergistique.java +++ b/src/main/java/gripe/_90/arseng/ArsEnergistique.java @@ -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; @@ -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); diff --git a/src/main/java/gripe/_90/arseng/item/SourceCellItem.java b/src/main/java/gripe/_90/arseng/item/SourceCellItem.java index 8350182..25a50ad 100644 --- a/src/main/java/gripe/_90/arseng/item/SourceCellItem.java +++ b/src/main/java/gripe/_90/arseng/item/SourceCellItem.java @@ -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 { @@ -41,14 +51,59 @@ 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 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 lines, @NotNull TooltipFlag advancedTooltips) { - addCellInformationToTooltip(is, lines); + @NotNull ItemStack stack, Level level, @NotNull List lines, @NotNull TooltipFlag advTooltips) { + addCellInformationToTooltip(stack, lines); } @NotNull @@ -56,4 +111,8 @@ public void appendHoverText( public Optional getTooltipImage(@NotNull ItemStack stack) { return getCellTooltipImage(stack); } + + public static void initColours(RegisterColorHandlersEvent.Item event) { + ArsEngItems.getCells().forEach(cell -> event.register(BasicStorageCell::getColor, cell)); + } } 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 e1537a6..874de9c 100644 --- a/src/main/java/gripe/_90/arseng/me/cell/SourceCellHandler.java +++ b/src/main/java/gripe/_90/arseng/me/cell/SourceCellHandler.java @@ -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(); @@ -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 lines) { var handler = getCellInventory(is, null); 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 e92bfe5..4ab37c4 100644 --- a/src/main/java/gripe/_90/arseng/me/cell/SourceCellInventory.java +++ b/src/main/java/gripe/_90/arseng/me/cell/SourceCellInventory.java @@ -80,7 +80,7 @@ public IUpgradeInventory getUpgrades() { return cell.getUpgrades(stack); } - protected void saveChanges() { + private void saveChanges() { isPersisted = false; if (container != null) {