diff --git a/src/main/java/techreborn/client/container/IRightClickHandler.java b/src/main/java/techreborn/client/container/IRightClickHandler.java new file mode 100644 index 000000000..84c126b26 --- /dev/null +++ b/src/main/java/techreborn/client/container/IRightClickHandler.java @@ -0,0 +1,12 @@ +package techreborn.client.container; + +import net.minecraft.entity.player.EntityPlayer; +import techreborn.client.container.builder.BuiltContainer; + +/** + * Created by Mark on 12/04/2017. + */ +public interface IRightClickHandler { + + public boolean handleRightClick(int slotID, EntityPlayer player, BuiltContainer container); +} diff --git a/src/main/java/techreborn/client/container/builder/BuiltContainer.java b/src/main/java/techreborn/client/container/builder/BuiltContainer.java index 5a2497dad..5c8c7fa85 100644 --- a/src/main/java/techreborn/client/container/builder/BuiltContainer.java +++ b/src/main/java/techreborn/client/container/builder/BuiltContainer.java @@ -35,6 +35,7 @@ import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import reborncore.common.util.ItemUtils; +import techreborn.client.container.IRightClickHandler; import java.util.ArrayList; import java.util.List; @@ -104,6 +105,19 @@ public class BuiltContainer extends Container { this.craftEvents.forEach(consumer -> consumer.accept((InventoryCrafting) inv)); } + @Override + public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, EntityPlayer player) { + if(slotId > 0 && slotId < 1000){ + Slot slot = this.inventorySlots.get(slotId); + if(slot instanceof IRightClickHandler){ + if(((IRightClickHandler) slot).handleRightClick(slot.getSlotIndex(), player, this)){ + return ItemStack.EMPTY; + } + } + } + return super.slotClick(slotId, dragType, clickTypeIn, player); + } + @Override public void detectAndSendChanges() { super.detectAndSendChanges(); diff --git a/src/main/java/techreborn/client/container/builder/ContainerTileInventoryBuilder.java b/src/main/java/techreborn/client/container/builder/ContainerTileInventoryBuilder.java index e9d011f8d..d7d0c2330 100644 --- a/src/main/java/techreborn/client/container/builder/ContainerTileInventoryBuilder.java +++ b/src/main/java/techreborn/client/container/builder/ContainerTileInventoryBuilder.java @@ -46,6 +46,7 @@ import reborncore.common.powerSystem.TilePowerAcceptor; import techreborn.Core; import techreborn.client.container.builder.slot.FilteredSlot; +import techreborn.client.container.builder.slot.UpgradeSlot; import java.util.function.Consumer; import java.util.function.IntConsumer; @@ -119,8 +120,7 @@ public class ContainerTileInventoryBuilder { private ContainerTileInventoryBuilder upgradeSlots(IUpgradeable upgradeable){ if(upgradeable.canBeUpgraded()){ for (int i = 0; i < upgradeable.getUpgradeSlotCount(); i++) { - this.parent.slots.add(new FilteredSlot(upgradeable.getUpgradeInvetory(), i, -19, i * 18 + 12, 1) - .setFilter(stack -> stack.getItem() instanceof IUpgrade)); + this.parent.slots.add(new UpgradeSlot(upgradeable.getUpgradeInvetory(), i, -19, i * 18 + 12)); } } return this; diff --git a/src/main/java/techreborn/client/container/builder/slot/UpgradeSlot.java b/src/main/java/techreborn/client/container/builder/slot/UpgradeSlot.java new file mode 100644 index 000000000..1ca059ee6 --- /dev/null +++ b/src/main/java/techreborn/client/container/builder/slot/UpgradeSlot.java @@ -0,0 +1,79 @@ +/* + * This file is part of TechReborn, licensed under the MIT License (MIT). + * + * Copyright (c) 2017 TechReborn + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package techreborn.client.container.builder.slot; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import reborncore.api.tile.IUpgrade; +import reborncore.api.tile.IUpgradeable; +import reborncore.common.tile.TileBase; +import reborncore.common.util.Inventory; +import techreborn.client.container.IRightClickHandler; +import techreborn.client.container.builder.BuiltContainer; + +import java.util.function.Predicate; + +public class UpgradeSlot extends Slot implements IRightClickHandler { + + private Predicate filter; + + public UpgradeSlot(final IInventory inventory, final int index, final int xPosition, final int yPosition) { + super(inventory, index, xPosition, yPosition); + } + + + @Override + public boolean isItemValid(final ItemStack stack) { + return stack.getItem() instanceof IUpgrade; + } + + @Override + public int getSlotStackLimit() { + return 1; + } + + @Override + public boolean handleRightClick(int slotID, EntityPlayer player, BuiltContainer container) { + if(inventory instanceof Inventory){ + Inventory inv = (Inventory) inventory; + TileEntity tileEntity = inv.getTileBase(); + if(tileEntity instanceof IUpgradeable){ + IUpgradeable upgradeable = (IUpgradeable) tileEntity; + if(upgradeable.canBeUpgraded()){ + ItemStack stack = upgradeable.getUpgradeInvetory().getStackInSlot(slotID); + if(!stack.isEmpty() && stack.getItem() instanceof IUpgrade){ + //Called on both sides :) + ((IUpgrade) stack.getItem()).handleRightClick(tileEntity, stack, container); + } + } + } + } + + return true; + } +} diff --git a/src/main/java/techreborn/items/ItemUpgrades.java b/src/main/java/techreborn/items/ItemUpgrades.java index 23e803576..a76971d1f 100644 --- a/src/main/java/techreborn/items/ItemUpgrades.java +++ b/src/main/java/techreborn/items/ItemUpgrades.java @@ -42,6 +42,7 @@ import reborncore.common.recipes.RecipeCrafter; import reborncore.common.tile.TileLegacyMachineBase; import reborncore.common.util.InventoryHelper; import techreborn.client.TechRebornCreativeTabMisc; +import techreborn.client.container.builder.BuiltContainer; import techreborn.init.ModItems; import javax.annotation.Nonnull; @@ -149,4 +150,13 @@ public class ItemUpgrades extends ItemTRNoDestroy implements IUpgrade { } } } + + @Override + public void handleRightClick(TileEntity tile, ItemStack stack, BuiltContainer container) { + if(stack.getItemDamage() == 4){ + System.out.println("open a gui here"); + } else if(stack.getItemDamage() == 5){ + System.out.println("open a gui here"); + } + } } \ No newline at end of file