ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemBag.java

107 lines
4.3 KiB
Java
Raw Normal View History

/*
* This file ("ItemBag.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.items;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerBag;
import de.ellpeck.actuallyadditions.mod.inventory.GuiHandler.GuiTypes;
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA;
2017-06-17 00:55:55 +02:00
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2021-02-26 22:15:48 +01:00
import net.minecraft.entity.player.PlayerEntity;
2016-08-02 20:06:31 +02:00
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity;
2016-12-18 17:50:31 +01:00
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.Direction;
2021-02-26 22:15:48 +01:00
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
2018-07-28 02:08:42 +02:00
public class ItemBag extends ItemBase {
2017-06-17 00:55:55 +02:00
public final boolean isVoid;
2018-07-28 02:08:42 +02:00
public ItemBag(String name, boolean isVoid) {
super(name);
this.isVoid = isVoid;
this.setMaxStackSize(1);
}
2016-08-10 13:32:55 +02:00
@Override
public EnumActionResult onItemUse(PlayerEntity playerIn, World worldIn, BlockPos pos, Hand hand, Direction facing, float hitX, float hitY, float hitZ) {
2016-11-19 21:11:17 +01:00
ItemStack stack = playerIn.getHeldItem(hand);
2018-07-28 02:08:42 +02:00
if (!this.isVoid) {
TileEntity tile = worldIn.getTileEntity(pos);
2018-07-28 02:08:42 +02:00
if (tile != null && tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing)) {
if (!worldIn.isRemote) {
IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing);
2018-07-28 02:08:42 +02:00
if (handler != null) {
boolean changed = false;
ItemStackHandlerAA inv = new ItemStackHandlerAA(ContainerBag.getSlotAmount(this.isVoid));
ItemDrill.loadSlotsFromNBT(inv, stack);
2018-07-28 02:08:42 +02:00
for (int j = 0; j < inv.getSlots(); j++) {
ItemStack invStack = inv.getStackInSlot(j);
2018-07-28 02:08:42 +02:00
if (StackUtil.isValid(invStack)) {
for (int i = 0; i < handler.getSlots(); i++) {
ItemStack remain = handler.insertItem(i, invStack, false);
2018-07-28 02:08:42 +02:00
if (!ItemStack.areItemStacksEqual(remain, invStack)) {
inv.setStackInSlot(j, remain.copy());
changed = true;
2018-07-28 02:08:42 +02:00
if (!StackUtil.isValid(remain)) {
break;
}
2019-05-02 09:08:06 +02:00
invStack = remain;
}
}
}
}
2018-07-28 02:08:42 +02:00
if (changed) {
ItemDrill.writeSlotsToNBT(inv, stack);
}
}
}
return EnumActionResult.SUCCESS;
}
}
return EnumActionResult.PASS;
}
@Override
2021-02-26 22:15:48 +01:00
public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) {
if (!world.isRemote && hand == Hand.MAIN_HAND) {
player.openGui(ActuallyAdditions.INSTANCE, (this.isVoid
? GuiTypes.VOID_BAG
: GuiTypes.BAG).ordinal(), world, (int) player.posX, (int) player.posY, (int) player.posZ);
}
return new ActionResult<>(EnumActionResult.PASS, player.getHeldItem(hand));
}
2016-08-02 20:06:31 +02:00
@Override
2018-07-28 02:08:42 +02:00
public EnumRarity getRarity(ItemStack stack) {
2021-02-26 22:15:48 +01:00
return this.isVoid
? EnumRarity.RARE
: EnumRarity.UNCOMMON;
2016-08-02 20:06:31 +02:00
}
2018-07-28 02:08:42 +02:00
@Override
2021-02-26 22:15:48 +01:00
public CompoundNBT getNBTShareTag(ItemStack stack) {
return null;
}
}