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;
}
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;
}
}
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);
}
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);
}
Aggregations