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
|
|
|
|
*
|
|
|
|
* © 2015-2016 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.tile.FilterSettings;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
|
2016-08-10 13:32:55 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
|
2016-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2016-08-10 13:32:55 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
|
2016-08-02 16:32:13 +02:00
|
|
|
import net.minecraft.entity.item.EntityItem;
|
|
|
|
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-08-02 16:32:13 +02:00
|
|
|
import net.minecraft.util.ActionResult;
|
|
|
|
import net.minecraft.util.EnumActionResult;
|
2016-08-07 01:11:01 +02:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2016-08-02 16:32:13 +02:00
|
|
|
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;
|
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
|
2016-08-02 20:06:31 +02:00
|
|
|
import net.minecraftforge.fml.common.eventhandler.Event;
|
2016-08-02 16:32:13 +02:00
|
|
|
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
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
|
|
|
|
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{
|
|
|
|
|
|
|
|
private final boolean isVoid;
|
|
|
|
|
|
|
|
public ItemBag(String name, boolean isVoid){
|
|
|
|
super(name);
|
|
|
|
this.isVoid = isVoid;
|
|
|
|
this.setMaxStackSize(1);
|
|
|
|
|
2016-08-07 01:11:01 +02:00
|
|
|
if(!this.isVoid){ //So that the event stuff only runs once because this class is initialized twice
|
|
|
|
MinecraftForge.EVENT_BUS.register(this);
|
|
|
|
}
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
|
|
|
|
2016-08-10 13:32:55 +02:00
|
|
|
@Override
|
|
|
|
public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced){
|
|
|
|
tooltip.add(TextFormatting.ITALIC+StringUtil.localize("tooltip."+ModUtil.MOD_ID+".previously"+(this.isVoid ? "VoidBag" : "Bag")));
|
|
|
|
}
|
|
|
|
|
2016-08-02 16:32:13 +02:00
|
|
|
@SubscribeEvent
|
|
|
|
public void onItemPickup(EntityItemPickupEvent event){
|
|
|
|
EntityPlayer player = event.getEntityPlayer();
|
|
|
|
EntityItem item = event.getItem();
|
|
|
|
if(item != null && !item.isDead){
|
|
|
|
ItemStack stack = item.getEntityItem();
|
2016-11-16 20:31:16 +01:00
|
|
|
if(StackUtil.isValid(stack)){
|
2016-08-02 16:32:13 +02:00
|
|
|
for(int i = 0; i < player.inventory.getSizeInventory(); i++){
|
|
|
|
if(i != player.inventory.currentItem){
|
|
|
|
|
|
|
|
ItemStack invStack = player.inventory.getStackInSlot(i);
|
2016-11-16 20:31:16 +01:00
|
|
|
if(StackUtil.isValid(invStack) && invStack.getItem() instanceof ItemBag && invStack.hasTagCompound()){
|
2016-08-02 16:32:13 +02:00
|
|
|
if(invStack.getTagCompound().getBoolean("AutoInsert")){
|
2016-08-02 20:06:31 +02:00
|
|
|
boolean changed = false;
|
2016-08-02 16:32:13 +02:00
|
|
|
|
|
|
|
boolean isVoid = ((ItemBag)invStack.getItem()).isVoid;
|
|
|
|
ItemStack[] inventory = new ItemStack[ContainerBag.getSlotAmount(isVoid)];
|
|
|
|
ItemDrill.loadSlotsFromNBT(inventory, invStack);
|
|
|
|
|
|
|
|
FilterSettings filter = new FilterSettings(0, 4, false, false, false, 0, 0);
|
|
|
|
filter.readFromNBT(invStack.getTagCompound(), "Filter");
|
|
|
|
if(filter.check(stack, inventory)){
|
2016-08-02 20:06:31 +02:00
|
|
|
if(isVoid){
|
2016-11-16 16:59:00 +01:00
|
|
|
stack = StackUtil.setStackSize(stack, 0);
|
2016-08-02 20:06:31 +02:00
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
for(int j = 4; j < inventory.length; j++){
|
|
|
|
ItemStack bagStack = inventory[j];
|
2016-11-16 20:31:16 +01:00
|
|
|
if(StackUtil.isValid(bagStack)){
|
2016-08-02 20:06:31 +02:00
|
|
|
if(ItemUtil.canBeStacked(bagStack, stack)){
|
2016-11-16 16:59:00 +01:00
|
|
|
int maxTransfer = Math.min(StackUtil.getStackSize(stack), stack.getMaxStackSize()-StackUtil.getStackSize(bagStack));
|
2016-08-02 20:06:31 +02:00
|
|
|
if(maxTransfer > 0){
|
2016-11-16 16:59:00 +01:00
|
|
|
inventory[j] = StackUtil.addStackSize(bagStack, maxTransfer);
|
|
|
|
stack = StackUtil.addStackSize(stack, -maxTransfer);
|
2016-08-02 20:06:31 +02:00
|
|
|
changed = true;
|
|
|
|
}
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-02 20:06:31 +02:00
|
|
|
else{
|
|
|
|
inventory[j] = stack.copy();
|
2016-11-16 16:59:00 +01:00
|
|
|
stack = StackUtil.setStackSize(stack, 0);
|
2016-08-02 20:06:31 +02:00
|
|
|
changed = true;
|
|
|
|
}
|
2016-08-02 16:32:13 +02:00
|
|
|
|
2016-11-16 16:59:00 +01:00
|
|
|
if(!StackUtil.isValid(stack)){
|
2016-08-02 20:06:31 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-02 20:06:31 +02:00
|
|
|
if(changed){
|
|
|
|
if(!isVoid){
|
|
|
|
ItemDrill.writeSlotsToNBT(inventory, invStack);
|
|
|
|
}
|
|
|
|
event.setResult(Event.Result.ALLOW);
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 16:59:00 +01:00
|
|
|
if(!StackUtil.isValid(stack)){
|
2016-08-02 16:32:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-16 16:59:00 +01:00
|
|
|
item.setEntityItemStack(stack);
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-07 01:11:01 +02:00
|
|
|
@Override
|
|
|
|
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ){
|
|
|
|
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;
|
|
|
|
|
|
|
|
ItemStack[] inventory = new ItemStack[ContainerBag.getSlotAmount(this.isVoid)];
|
|
|
|
ItemDrill.loadSlotsFromNBT(inventory, stack);
|
|
|
|
|
|
|
|
for(int j = 4; j < inventory.length; j++){
|
|
|
|
ItemStack invStack = inventory[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-11-16 20:31:16 +01:00
|
|
|
inventory[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){
|
|
|
|
ItemDrill.writeSlotsToNBT(inventory, stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return EnumActionResult.SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return EnumActionResult.PASS;
|
|
|
|
}
|
|
|
|
|
2016-08-02 16:32:13 +02:00
|
|
|
@Override
|
|
|
|
public ActionResult<ItemStack> onItemRightClick(ItemStack stack, World world, EntityPlayer player, EnumHand hand){
|
|
|
|
if(!world.isRemote){
|
|
|
|
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<ItemStack>(EnumActionResult.PASS, stack);
|
|
|
|
}
|
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
|
|
|
}
|