From 8b8aaab4f4334a1a22b25afe9a7b4778b7fd9cb0 Mon Sep 17 00:00:00 2001 From: Michael Hillcox Date: Sat, 5 Dec 2020 13:41:00 +0000 Subject: [PATCH] Drill container, container rewrites, Drill augments, minor code cleaning --- .../common/blocks/building/CrystalBlock.java | 2 +- .../capability/CrystalFluxProvider.java | 2 +- .../common/container/ActuallyContainers.java | 2 +- .../common/container/DrillContainer.java | 68 +++++++++++++++---- .../common/container/InventoryContainer.java | 49 ------------- .../common/items/ActuallyItems.java | 19 +++--- .../common/items/misc/DrillAugmentItem.java | 28 ++++++++ .../common/items/useables/AllInOneTool.java | 2 +- .../common/items/useables/DrillItem.java | 11 ++- .../common/utilities/ContainerHelper.java | 25 +++++++ 10 files changed, 130 insertions(+), 78 deletions(-) delete mode 100644 src/main/java/de/ellpeck/actuallyadditions/common/container/InventoryContainer.java create mode 100644 src/main/java/de/ellpeck/actuallyadditions/common/items/misc/DrillAugmentItem.java create mode 100644 src/main/java/de/ellpeck/actuallyadditions/common/utilities/ContainerHelper.java diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/blocks/building/CrystalBlock.java b/src/main/java/de/ellpeck/actuallyadditions/common/blocks/building/CrystalBlock.java index 0b9e00485..36bb0ac3a 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/blocks/building/CrystalBlock.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/blocks/building/CrystalBlock.java @@ -7,7 +7,7 @@ import net.minecraft.item.ItemStack; import net.minecraftforge.common.ToolType; public class CrystalBlock extends ActuallyBlock { - private boolean isEmpowered; + private final boolean isEmpowered; public CrystalBlock(boolean isEmpowered) { super(Properties.create(Material.ROCK) diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/capability/CrystalFluxProvider.java b/src/main/java/de/ellpeck/actuallyadditions/common/capability/CrystalFluxProvider.java index 156cfe54f..588c8ee43 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/capability/CrystalFluxProvider.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/capability/CrystalFluxProvider.java @@ -16,7 +16,7 @@ public class CrystalFluxProvider implements ICapabilityProvider { private int energy; private int transfer; - private LazyOptional capability = LazyOptional.of(() -> new CrystalFluxStorage(stack, energy, transfer)); + private final LazyOptional capability = LazyOptional.of(() -> new CrystalFluxStorage(stack, energy, transfer)); public CrystalFluxProvider(ItemStack stack, int energy) { this.stack = stack; diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/container/ActuallyContainers.java b/src/main/java/de/ellpeck/actuallyadditions/common/container/ActuallyContainers.java index c5985acb9..e87be7273 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/container/ActuallyContainers.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/container/ActuallyContainers.java @@ -11,5 +11,5 @@ public final class ActuallyContainers { public static final DeferredRegister> CONTAINERS = DeferredRegister.create(ForgeRegistries.CONTAINERS, ActuallyAdditions.MOD_ID); public static final RegistryObject> DRILL_CONTAINER - = CONTAINERS.register("drill_container", () -> IForgeContainerType.create(DrillContainer::new)); + = CONTAINERS.register("drill_container", () -> IForgeContainerType.create(DrillContainer::fromNetwork)); } diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/container/DrillContainer.java b/src/main/java/de/ellpeck/actuallyadditions/common/container/DrillContainer.java index 5b9d2c18f..798c494e1 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/container/DrillContainer.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/container/DrillContainer.java @@ -1,17 +1,48 @@ package de.ellpeck.actuallyadditions.common.container; +import de.ellpeck.actuallyadditions.common.items.misc.DrillAugmentItem; import de.ellpeck.actuallyadditions.common.items.useables.DrillItem; +import de.ellpeck.actuallyadditions.common.utilities.ContainerHelper; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.inventory.container.ClickType; +import net.minecraft.inventory.container.Container; +import net.minecraft.item.ItemStack; import net.minecraft.network.PacketBuffer; +import net.minecraftforge.items.ItemStackHandler; +import net.minecraftforge.items.SlotItemHandler; +import net.minecraftforge.items.wrapper.InvWrapper; -public class DrillContainer extends InventoryContainer { - public DrillContainer(int windowId, PlayerInventory inv, PacketBuffer data) { - super(windowId, inv, data); +import javax.annotation.Nonnull; + +public class DrillContainer extends Container { + private final ItemStackHandler handler; + private final PlayerInventory inv; + private final ItemStack stack; + + public static DrillContainer fromNetwork(int windowId, PlayerInventory inv, PacketBuffer data) { + // Seeing as this container only works on the main hand, this works nicely + return new DrillContainer(windowId, inv, inv.player.getHeldItemMainhand()); } - public DrillContainer(int windowId, PlayerInventory inv) { - super(windowId, inv); + public DrillContainer(int windowId, PlayerInventory inv, ItemStack stack) { + super(ActuallyContainers.DRILL_CONTAINER.get(), windowId); + + this.inv = inv; + this.stack = stack; + + ContainerHelper.setupPlayerInventory(new InvWrapper(inv), 0, ContainerHelper.DEFAULT_SLOTS_X, 116, this::addSlot); + + this.handler = new ItemStackHandler(5) { + @Override + public boolean isItemValid(int slot, @Nonnull ItemStack stack) { + return stack.getItem() instanceof DrillAugmentItem; + } + }; + + this.handler.deserializeNBT(this.stack.getOrCreateChildTag("augments")); + + this.addContainerSlots(); } @Override @@ -20,18 +51,29 @@ public class DrillContainer extends InventoryContainer { return playerIn.getHeldItemMainhand().getItem() instanceof DrillItem; } + protected void addContainerSlots() { + for (int i = 0; i < 5; i ++) { + addSlot(new SlotItemHandler(handler, i, 44 + (i * ContainerHelper.SLOT_SPACING), 19)); + } + } + + @Nonnull @Override - protected void addContainerSlots(int index) { -// addSlot(new Slot()); + public ItemStack slotClick(int slotId, int dragType, @Nonnull ClickType clickTypeIn, @Nonnull PlayerEntity player) { + if ((clickTypeIn == ClickType.SWAP || clickTypeIn == ClickType.THROW || clickTypeIn == ClickType.PICKUP) + && slotId >= 0 + && slotId <= this.inventorySlots.size() + && inv.getStackInSlot(slotId).getItem() instanceof DrillItem) { + return ItemStack.EMPTY; + } + + return super.slotClick(slotId, dragType, clickTypeIn, player); } @Override - protected int getSlotX() { - return DEFAULT_SLOTS_X; - } + public void onContainerClosed(@Nonnull PlayerEntity playerIn) { + super.onContainerClosed(playerIn); - @Override - protected int getSlotY() { - return 116; + this.stack.getOrCreateTag().put("augments", this.handler.serializeNBT()); } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/container/InventoryContainer.java b/src/main/java/de/ellpeck/actuallyadditions/common/container/InventoryContainer.java deleted file mode 100644 index 688523055..000000000 --- a/src/main/java/de/ellpeck/actuallyadditions/common/container/InventoryContainer.java +++ /dev/null @@ -1,49 +0,0 @@ -package de.ellpeck.actuallyadditions.common.container; - -import net.minecraft.entity.player.PlayerInventory; -import net.minecraft.inventory.container.Container; -import net.minecraft.network.PacketBuffer; -import net.minecraftforge.items.IItemHandler; -import net.minecraftforge.items.SlotItemHandler; -import net.minecraftforge.items.wrapper.InvWrapper; - -public abstract class InventoryContainer extends Container { - public static final int DEFAULT_SLOTS_X = 8; - - protected final IItemHandler playerInventory; - - public InventoryContainer(int windowId, PlayerInventory inv, PacketBuffer data) { - this(windowId, inv); - } - - public InventoryContainer(int windowId, PlayerInventory inv) { - super(ActuallyContainers.DRILL_CONTAINER.get(), windowId); - this.playerInventory = new InvWrapper(inv); - - this.setupInventory(); - } - - private void setupInventory() { - int index = 0, x = getSlotX(), y = getSlotY(); - - // Build the players inventory first, building from bottom to top, right to left. The (i>0) magic handles the - // space between the hotbar inventory and the players remaining inventory. - for (int i = 0; i < 4; i++) { - boolean isHotbar = i < 1; - for (int j = 0; j < 9; j++) { - addSlot(new SlotItemHandler(playerInventory, index, x + (j * 18), isHotbar ? y : ((y - 76) + (i * 18)))); - index++; - } - } - - this.addContainerSlots(index); - } - - /** - * This method will always carry on the index after setting up the players inventory - */ - protected abstract void addContainerSlots(int index); - - protected abstract int getSlotX(); - protected abstract int getSlotY(); -} diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/items/ActuallyItems.java b/src/main/java/de/ellpeck/actuallyadditions/common/items/ActuallyItems.java index 1e712952e..6397b0ff7 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/items/ActuallyItems.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/items/ActuallyItems.java @@ -2,6 +2,7 @@ package de.ellpeck.actuallyadditions.common.items; import com.google.common.collect.ImmutableSet; import de.ellpeck.actuallyadditions.common.ActuallyAdditions; +import de.ellpeck.actuallyadditions.common.items.misc.DrillAugmentItem; import de.ellpeck.actuallyadditions.common.items.useables.*; import de.ellpeck.actuallyadditions.common.materials.ArmorMaterials; import de.ellpeck.actuallyadditions.common.materials.ToolMaterials; @@ -128,15 +129,15 @@ public final class ActuallyItems { public static final RegistryObject DRILL_RED = ITEMS.register("drill_red", DrillItem::new); public static final RegistryObject DRILL_WHITE = ITEMS.register("drill_white", DrillItem::new); public static final RegistryObject DRILL_YELLOW = ITEMS.register("drill_yellow", DrillItem::new); - public static final RegistryObject DRILL_SPEED_AUGMENT_I = ITEMS.register("drill_speed_augment_i", basicItem()); - public static final RegistryObject DRILL_SPEED_AUGMENT_II = ITEMS.register("drill_speed_augment_ii", basicItem()); - public static final RegistryObject DRILL_SPEED_AUGMENT_III = ITEMS.register("drill_speed_augment_iii", basicItem()); - public static final RegistryObject DRILL_SILK_TOUCH_AUGMENT = ITEMS.register("drill_silk_touch_augment", basicItem()); - public static final RegistryObject DRILL_FORTUNE_AUGMENT_I = ITEMS.register("drill_fortune_augment_i", basicItem()); - public static final RegistryObject DRILL_FORTUNE_AUGMENT_II = ITEMS.register("drill_fortune_augment_ii", basicItem()); - public static final RegistryObject DRILL_MINING_AUGMENT_I = ITEMS.register("drill_mining_augment_i", basicItem()); - public static final RegistryObject DRILL_MINING_AUGMENT_II = ITEMS.register("drill_mining_augment_ii", basicItem()); - public static final RegistryObject DRILL_BLOCK_PLACING_AUGMENT = ITEMS.register("drill_block_placing_augment", basicItem()); + public static final RegistryObject DRILL_SPEED_AUGMENT_I = ITEMS.register("drill_speed_augment_i", () -> new DrillAugmentItem(DrillAugmentItem.AugmentType.SPEED_AUGMENT_I)); + public static final RegistryObject DRILL_SPEED_AUGMENT_II = ITEMS.register("drill_speed_augment_ii", () -> new DrillAugmentItem(DrillAugmentItem.AugmentType.SPEED_AUGMENT_II)); + public static final RegistryObject DRILL_SPEED_AUGMENT_III = ITEMS.register("drill_speed_augment_iii", () -> new DrillAugmentItem(DrillAugmentItem.AugmentType.SPEED_AUGMENT_III)); + public static final RegistryObject DRILL_SILK_TOUCH_AUGMENT = ITEMS.register("drill_silk_touch_augment", () -> new DrillAugmentItem(DrillAugmentItem.AugmentType.SILK_TOUCH_AUGMENT)); + public static final RegistryObject DRILL_FORTUNE_AUGMENT_I = ITEMS.register("drill_fortune_augment_i", () -> new DrillAugmentItem(DrillAugmentItem.AugmentType.FORTUNE_AUGMENT_I)); + public static final RegistryObject DRILL_FORTUNE_AUGMENT_II = ITEMS.register("drill_fortune_augment_ii", () -> new DrillAugmentItem(DrillAugmentItem.AugmentType.FORTUNE_AUGMENT_II)); + public static final RegistryObject DRILL_MINING_AUGMENT_I = ITEMS.register("drill_mining_augment_i", () -> new DrillAugmentItem(DrillAugmentItem.AugmentType.MINING_AUGMENT_I)); + public static final RegistryObject DRILL_MINING_AUGMENT_II = ITEMS.register("drill_mining_augment_ii", () -> new DrillAugmentItem(DrillAugmentItem.AugmentType.MINING_AUGMENT_II)); + public static final RegistryObject DRILL_BLOCK_PLACING_AUGMENT = ITEMS.register("drill_block_placing_augment", () -> new DrillAugmentItem(DrillAugmentItem.AugmentType.BLOCK_PLACING_AUGMENT)); public static final RegistryObject FERTILIZER = ITEMS.register("fertilizer", basicItem()); public static final RegistryObject CUP_WITH_COFFEE = ITEMS.register("cup_with_coffee", basicItem()); public static final RegistryObject PHANTOM_CONNECTOR = ITEMS.register("phantom_connector", basicItem()); diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/items/misc/DrillAugmentItem.java b/src/main/java/de/ellpeck/actuallyadditions/common/items/misc/DrillAugmentItem.java new file mode 100644 index 000000000..740eea417 --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/common/items/misc/DrillAugmentItem.java @@ -0,0 +1,28 @@ +package de.ellpeck.actuallyadditions.common.items.misc; + +import de.ellpeck.actuallyadditions.common.items.ActuallyItem; + +public class DrillAugmentItem extends ActuallyItem { + private final AugmentType type; + + public DrillAugmentItem(AugmentType type) { + super(baseProps().maxStackSize(1)); + this.type = type; + } + + public AugmentType getType() { + return type; + } + + public enum AugmentType { + SPEED_AUGMENT_I, + SPEED_AUGMENT_II, + SPEED_AUGMENT_III, + SILK_TOUCH_AUGMENT, + FORTUNE_AUGMENT_I, + FORTUNE_AUGMENT_II, + MINING_AUGMENT_I, + MINING_AUGMENT_II, + BLOCK_PLACING_AUGMENT + } +} diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/AllInOneTool.java b/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/AllInOneTool.java index 11e669985..f42fefb6b 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/AllInOneTool.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/AllInOneTool.java @@ -9,7 +9,7 @@ import net.minecraft.util.ActionResultType; import net.minecraftforge.common.ToolType; public class AllInOneTool extends ToolItem implements IActuallyItem { - private IItemTier tier; + private final IItemTier tier; public AllInOneTool(IItemTier tier) { super( diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/DrillItem.java b/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/DrillItem.java index aeabae6f0..2015f0045 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/DrillItem.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/DrillItem.java @@ -29,13 +29,18 @@ public class DrillItem extends CrystalFluxItem { @Override public ActionResult onItemRightClick(World worldIn, PlayerEntity player, Hand handIn) { - if (player.isSneaking() && !worldIn.isRemote && handIn == Hand.MAIN_HAND) { + ItemStack stack = player.getHeldItem(handIn); + if (worldIn.isRemote()) { + return ActionResult.resultPass(stack); + } + + if (player.isSneaking() && handIn == Hand.MAIN_HAND) { NetworkHooks.openGui((ServerPlayerEntity) player, new SimpleNamedContainerProvider( - (windowId, playerInv, playerEntity) -> new DrillContainer(windowId, playerInv), + (windowId, playerInv, playerEntity) -> new DrillContainer(windowId, playerInv, stack), Help.trans("gui.name.drill") )); } - return super.onItemRightClick(worldIn, player, handIn); + return ActionResult.resultSuccess(stack); } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/utilities/ContainerHelper.java b/src/main/java/de/ellpeck/actuallyadditions/common/utilities/ContainerHelper.java new file mode 100644 index 000000000..65735397a --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/common/utilities/ContainerHelper.java @@ -0,0 +1,25 @@ +package de.ellpeck.actuallyadditions.common.utilities; + +import net.minecraft.inventory.container.Slot; +import net.minecraftforge.items.IItemHandler; +import net.minecraftforge.items.SlotItemHandler; + +import java.util.function.Consumer; + +public class ContainerHelper { + public static final int DEFAULT_SLOTS_X = 8; + public static final int SLOT_SPACING = 18; + + public static void setupPlayerInventory(IItemHandler handler, int index, int x, int y, Consumer consumer) { + // Build the players inventory first, building from bottom to top, right to left. The (i>0) magic handles the + // space between the hotbar inventory and the players remaining inventory. + for (int i = 0; i < 4; i++) { + boolean isHotbar = i < 1; + for (int j = 0; j < 9; j++) { + consumer.accept(new SlotItemHandler(handler, index, x + (j * SLOT_SPACING), isHotbar ? y : ((y - 76) + (i * SLOT_SPACING)))); + index++; + } + } + } + +}