2016-08-02 16:32:13 +02:00
|
|
|
/*
|
|
|
|
* 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
|
2016-08-02 16:32:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
2017-06-17 00:55:55 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerCustom;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
|
2017-06-17 00:48:49 +02:00
|
|
|
import net.minecraft.client.util.ITooltipFlag;
|
2016-08-02 16:32:13 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2016-08-02 20:06:31 +02:00
|
|
|
import net.minecraft.item.EnumRarity;
|
2016-08-02 16:32:13 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2016-08-07 01:11:01 +02:00
|
|
|
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.EnumFacing;
|
|
|
|
import net.minecraft.util.EnumHand;
|
2016-08-07 01:11:01 +02:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-08-10 13:32:55 +02:00
|
|
|
import net.minecraft.util.text.TextFormatting;
|
2016-08-02 16:32:13 +02:00
|
|
|
import net.minecraft.world.World;
|
2016-08-07 01:11:01 +02:00
|
|
|
import net.minecraftforge.items.CapabilityItemHandler;
|
|
|
|
import net.minecraftforge.items.IItemHandler;
|
2016-08-02 16:32:13 +02:00
|
|
|
|
2017-06-17 00:48:49 +02:00
|
|
|
import javax.annotation.Nullable;
|
2016-08-10 13:32:55 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2016-08-02 16:32:13 +02:00
|
|
|
public class ItemBag extends ItemBase{
|
|
|
|
|
2017-06-17 00:55:55 +02:00
|
|
|
public final boolean isVoid;
|
2016-08-02 16:32:13 +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
|
2017-06-17 00:48:49 +02:00
|
|
|
public void addInformation(ItemStack stack, @Nullable World playerIn, List<String> tooltip, ITooltipFlag advanced){
|
2016-12-04 00:10:52 +01:00
|
|
|
ItemStackHandlerCustom inv = new ItemStackHandlerCustom(ContainerBag.getSlotAmount(this.isVoid));
|
2016-11-27 11:37:55 +01:00
|
|
|
ItemDrill.loadSlotsFromNBT(inv, stack);
|
2016-11-25 22:05:19 +01:00
|
|
|
|
2016-12-04 00:10:52 +01:00
|
|
|
int slotsTotal = inv.getSlots();
|
2016-11-25 22:05:19 +01:00
|
|
|
int slotsFilled = 0;
|
|
|
|
|
2016-12-04 00:10:52 +01:00
|
|
|
for(int i = 0; i < inv.getSlots(); i++){
|
2016-11-27 11:37:55 +01:00
|
|
|
if(StackUtil.isValid(inv.getStackInSlot(i))){
|
2016-11-25 22:05:19 +01:00
|
|
|
slotsFilled++;
|
|
|
|
}
|
|
|
|
}
|
2017-06-17 00:48:49 +02:00
|
|
|
tooltip.add(TextFormatting.ITALIC+String.format("%d/%d %s", slotsFilled, slotsTotal, StringUtil.localize("item."+ModUtil.MOD_ID+".item_bag.storage")));
|
2016-08-10 13:32:55 +02:00
|
|
|
}
|
|
|
|
|
2016-08-07 01:11:01 +02:00
|
|
|
@Override
|
2016-11-19 21:11:17 +01:00
|
|
|
public EnumActionResult onItemUse(EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ){
|
|
|
|
ItemStack stack = playerIn.getHeldItem(hand);
|
2016-08-07 01:11:01 +02:00
|
|
|
if(!this.isVoid){
|
|
|
|
TileEntity tile = worldIn.getTileEntity(pos);
|
|
|
|
if(tile != null && tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing)){
|
|
|
|
if(!worldIn.isRemote){
|
|
|
|
IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing);
|
|
|
|
if(handler != null){
|
|
|
|
boolean changed = false;
|
|
|
|
|
2016-12-04 00:10:52 +01:00
|
|
|
ItemStackHandlerCustom inv = new ItemStackHandlerCustom(ContainerBag.getSlotAmount(this.isVoid));
|
2016-11-27 11:37:55 +01:00
|
|
|
ItemDrill.loadSlotsFromNBT(inv, stack);
|
2016-08-07 01:11:01 +02:00
|
|
|
|
2016-12-29 19:57:06 +01:00
|
|
|
for(int j = 0; j < inv.getSlots(); j++){
|
2016-11-27 11:37:55 +01:00
|
|
|
ItemStack invStack = inv.getStackInSlot(j);
|
2016-11-16 20:31:16 +01:00
|
|
|
if(StackUtil.isValid(invStack)){
|
2016-08-07 01:11:01 +02:00
|
|
|
for(int i = 0; i < handler.getSlots(); i++){
|
|
|
|
ItemStack remain = handler.insertItem(i, invStack, false);
|
|
|
|
if(!ItemStack.areItemStacksEqual(remain, invStack)){
|
2016-12-04 00:10:52 +01:00
|
|
|
inv.setStackInSlot(j, StackUtil.validateCopy(remain));
|
2016-08-07 01:11:01 +02:00
|
|
|
changed = true;
|
|
|
|
|
2016-11-16 20:31:16 +01:00
|
|
|
if(!StackUtil.isValid(remain)){
|
2016-08-07 01:11:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(changed){
|
2016-11-27 11:37:55 +01:00
|
|
|
ItemDrill.writeSlotsToNBT(inv, stack);
|
2016-08-07 01:11:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return EnumActionResult.SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return EnumActionResult.PASS;
|
|
|
|
}
|
|
|
|
|
2016-08-02 16:32:13 +02:00
|
|
|
@Override
|
2016-11-19 21:11:17 +01:00
|
|
|
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand){
|
2017-03-28 17:16:33 +02:00
|
|
|
if(!world.isRemote && hand == EnumHand.MAIN_HAND){
|
2016-08-02 16:32:13 +02:00
|
|
|
player.openGui(ActuallyAdditions.instance, (this.isVoid ? GuiTypes.VOID_BAG : GuiTypes.BAG).ordinal(), world, (int)player.posX, (int)player.posY, (int)player.posZ);
|
|
|
|
}
|
2016-11-19 21:11:17 +01:00
|
|
|
return new ActionResult<ItemStack>(EnumActionResult.PASS, player.getHeldItem(hand));
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
2016-08-02 20:06:31 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public EnumRarity getRarity(ItemStack stack){
|
|
|
|
return this.isVoid ? EnumRarity.RARE : EnumRarity.UNCOMMON;
|
|
|
|
}
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|