Search in sources :

Example 6 with PropertiesBackpack

use of net.mcft.copy.betterstorage.misc.PropertiesBackpack in project BetterStorage by copygirl.

the class TileEntityBackpack method unequip.

public void unequip(EntityLivingBase carrier, boolean despawn) {
    if (worldObj.isRemote)
        return;
    // Move items from the player backpack data to this tile entity.
    PropertiesBackpack backpackData = ItemBackpack.getBackpackData(carrier);
    if (backpackData.contents != null) {
        System.arraycopy(backpackData.contents, 0, contents, 0, Math.min(contents.length, backpackData.contents.length));
        backpackData.contents = null;
    }
    if (despawn)
        despawnTime = 0;
}
Also used : PropertiesBackpack(net.mcft.copy.betterstorage.misc.PropertiesBackpack)

Example 7 with PropertiesBackpack

use of net.mcft.copy.betterstorage.misc.PropertiesBackpack in project BetterStorage by copygirl.

the class BackpackHandler method onLivingDeath.

@SubscribeEvent
public void onLivingDeath(LivingDeathEvent event) {
    // If an entity wearing a backpack dies,
    // try to place it, or drop the items.
    EntityLivingBase entity = event.entityLiving;
    if (entity.worldObj.isRemote)
        return;
    EntityPlayer player = ((entity instanceof EntityPlayer) ? (EntityPlayer) entity : null);
    ItemStack backpack = ItemBackpack.getBackpack(entity);
    if (backpack == null)
        return;
    PropertiesBackpack backpackData = ItemBackpack.getBackpackData(entity);
    if (backpackData.contents == null)
        return;
    boolean keepInventory = entity.worldObj.getGameRules().getGameRuleBooleanValue("keepInventory");
    if ((player != null) && keepInventory) {
        // If keep inventory is on, instead temporarily save the contents
        // to the persistent NBT tag and get them back when the player respawns.
        NBTTagCompound compound = player.getEntityData();
        NBTTagCompound persistent;
        if (!compound.hasKey(EntityPlayer.PERSISTED_NBT_TAG)) {
            persistent = new NBTTagCompound();
            compound.setTag(EntityPlayer.PERSISTED_NBT_TAG, persistent);
        } else
            persistent = compound.getCompoundTag(EntityPlayer.PERSISTED_NBT_TAG);
        ;
        NBTTagCompound backpackCompound = new NBTTagCompound();
        backpackCompound.setInteger("count", backpackData.contents.length);
        backpackCompound.setTag("Items", NbtUtils.writeItems(backpackData.contents));
        if (!ItemBackpack.hasChestplateBackpackEquipped(entity))
            backpackCompound.setTag("Stack", backpack.writeToNBT(new NBTTagCompound()));
        persistent.setTag("Backpack", backpackCompound);
    } else {
        // Attempt to place the backpack as a block instead of dropping the items.
        if (BetterStorage.globalConfig.getBoolean(GlobalConfig.dropBackpackOnDeath)) {
            ForgeDirection orientation = DirectionUtils.getOrientation(entity);
            int recentlyHit = ReflectionUtils.get(EntityLivingBase.class, entity, "field_70718_bc", "recentlyHit");
            boolean despawn = ((player == null) && (recentlyHit <= 0));
            List<BlockCoordinate> coords = new ArrayList<BlockCoordinate>();
            for (int x = -2; x <= 2; x++) for (int z = -2; z <= 2; z++) coords.add(new BlockCoordinate(entity.posX, entity.posY, entity.posZ, x, 0, z));
            // Try to place the backpack on the ground nearby,
            // or look for a ground above or below to place it.
            Collections.sort(coords, blockDistanceComparator);
            while (!coords.isEmpty()) {
                Iterator<BlockCoordinate> iter = coords.iterator();
                while (iter.hasNext()) {
                    BlockCoordinate coord = iter.next();
                    if (ItemBackpack.placeBackpack(entity, player, backpack, coord.x, coord.y, coord.z, 1, orientation, despawn, true)) {
                        ItemBackpack.setBackpack(entity, null, null);
                        return;
                    }
                    boolean replacable = entity.worldObj.getBlock(coord.x, coord.y, coord.z).isReplaceable(entity.worldObj, coord.x, coord.y, coord.z);
                    coord.y += (replacable ? -1 : 1);
                    coord.moved += (replacable ? 1 : 5);
                    if ((coord.y <= 0) || (coord.y > entity.worldObj.getHeight()) || (coord.moved > 24 - coord.distance * 4))
                        iter.remove();
                }
            }
            // If backpack couldn't be placed and isn't equipped as armor, drop it.
            if (backpackData.backpack != null)
                WorldUtils.dropStackFromEntity(entity, backpack, 4.0F);
        }
        for (ItemStack stack : backpackData.contents) WorldUtils.dropStackFromEntity(entity, stack, 4.0F);
        backpackData.contents = null;
    }
}
Also used : EntityLivingBase(net.minecraft.entity.EntityLivingBase) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) ArrayList(java.util.ArrayList) EntityPlayer(net.minecraft.entity.player.EntityPlayer) PropertiesBackpack(net.mcft.copy.betterstorage.misc.PropertiesBackpack) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(cpw.mods.fml.common.eventhandler.SubscribeEvent)

Example 8 with PropertiesBackpack

use of net.mcft.copy.betterstorage.misc.PropertiesBackpack in project BetterStorage by copygirl.

the class ItemBackpack method setBackpack.

public static void setBackpack(EntityLivingBase entity, ItemStack backpack, ItemStack[] contents) {
    boolean setChestplate = (BetterStorage.globalConfig.getBoolean(GlobalConfig.backpackChestplate) || !(entity instanceof EntityPlayer) || hasChestplateBackpackEquipped(entity));
    PropertiesBackpack backpackData = getBackpackData(entity);
    if (!setChestplate)
        backpackData.backpack = backpack;
    else
        entity.setCurrentItemOrArmor(EquipmentSlot.CHEST, backpack);
    backpackData.contents = contents;
    ItemBackpack.updateHasItems(entity, backpackData);
}
Also used : EntityPlayer(net.minecraft.entity.player.EntityPlayer) PropertiesBackpack(net.mcft.copy.betterstorage.misc.PropertiesBackpack)

Example 9 with PropertiesBackpack

use of net.mcft.copy.betterstorage.misc.PropertiesBackpack in project BetterStorage by copygirl.

the class ItemBackpack method getBackpackItemsInternal.

protected IInventory getBackpackItemsInternal(EntityLivingBase carrier, EntityPlayer player) {
    PropertiesBackpack backpackData = getBackpackData(carrier);
    int size = (getBackpackColumns() * getBackpackRows());
    if (backpackData.contents == null)
        backpackData.contents = new ItemStack[size];
    else // the configuration file, update it here.
    if (backpackData.contents.length != size) {
        ItemStack[] newContents = new ItemStack[size];
        System.arraycopy(backpackData.contents, 0, newContents, 0, Math.min(size, backpackData.contents.length));
        backpackData.contents = newContents;
    }
    return new InventoryStacks(getBackpackName(), backpackData.contents);
}
Also used : PropertiesBackpack(net.mcft.copy.betterstorage.misc.PropertiesBackpack) ItemStack(net.minecraft.item.ItemStack) InventoryStacks(net.mcft.copy.betterstorage.inventory.InventoryStacks)

Aggregations

PropertiesBackpack (net.mcft.copy.betterstorage.misc.PropertiesBackpack)9 ItemStack (net.minecraft.item.ItemStack)5 SubscribeEvent (cpw.mods.fml.common.eventhandler.SubscribeEvent)3 EntityPlayer (net.minecraft.entity.player.EntityPlayer)3 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)3 InventoryStacks (net.mcft.copy.betterstorage.inventory.InventoryStacks)2 EntityLivingBase (net.minecraft.entity.EntityLivingBase)2 ArrayList (java.util.ArrayList)1 EntityFrienderman (net.mcft.copy.betterstorage.entity.EntityFrienderman)1 ItemBackpack (net.mcft.copy.betterstorage.item.ItemBackpack)1 EntityLiving (net.minecraft.entity.EntityLiving)1 ChestGenHooks (net.minecraftforge.common.ChestGenHooks)1 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)1