use of org.bukkit.inventory.ItemStack in project Core by iConomy.
the class InventoryDB method readInventory.
private ItemStack[] readInventory(String name) {
try {
NBTInputStream in = new NBTInputStream(new FileInputStream(new File(dataDir, name + ".dat")));
CompoundTag tag = (CompoundTag) in.readTag();
in.close();
ListTag inventory = (ListTag) tag.getValue().get("Inventory");
ItemStack[] stacks = new ItemStack[40];
for (int i = 0; i < inventory.getValue().size(); ++i) {
CompoundTag item = (CompoundTag) inventory.getValue().get(i);
byte count = ((ByteTag) item.getValue().get("Count")).getValue();
byte slot = ((ByteTag) item.getValue().get("Slot")).getValue();
short damage = ((ShortTag) item.getValue().get("Damage")).getValue();
short id = ((ShortTag) item.getValue().get("id")).getValue();
stacks[slot] = new ItemStack(id, count, damage);
}
return stacks;
} catch (IOException ex) {
iConomy.Server.getLogger().log(Level.WARNING, "[iCo/InvDB] error reading inventory {0}: {1}", new Object[] { name, ex.getMessage() });
return null;
}
}
use of org.bukkit.inventory.ItemStack in project HawkEye by oliverwoodings.
the class ContainerEntry method rebuild.
@Override
public boolean rebuild(Block block) {
if (!(block instanceof InventoryHolder))
return false;
Inventory inv = ((InventoryHolder) block.getState()).getInventory();
List<HashMap<String, Integer>> ops = InventoryUtil.interpretDifferenceString(data);
//Handle the additions
if (ops.size() > 0) {
for (ItemStack stack : InventoryUtil.uncompressInventory(ops.get(0))) inv.addItem(stack);
}
//Handle subtractions
if (ops.size() > 1) {
for (ItemStack stack : InventoryUtil.uncompressInventory(ops.get(1))) inv.removeItem(stack);
}
return true;
}
use of org.bukkit.inventory.ItemStack in project HawkEye by oliverwoodings.
the class MonitorEntityListener method onEntityDeath.
/**
* Uses the lastAttacker field in the players {@link PlayerSession} to log the death and cause
*/
@HawkEvent(dataType = { DataType.PVP_DEATH, DataType.MOB_DEATH, DataType.OTHER_DEATH, DataType.ENTITY_KILL })
public void onEntityDeath(EntityDeathEvent event) {
Entity entity = event.getEntity();
if (entity instanceof Player) {
//Player death
Player victim = (Player) entity;
//Mob or PVP death
if (victim.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
Entity damager = ((EntityDamageByEntityEvent) (victim.getLastDamageCause())).getDamager();
if (damager instanceof Player) {
if (!Config.isLogged(DataType.PVP_DEATH) && !Config.LogDeathDrops)
return;
DataManager.addEntry(new DataEntry(victim, DataType.PVP_DEATH, victim.getLocation(), Util.getEntityName(damager)));
} else {
if (!Config.isLogged(DataType.MOB_DEATH) && !Config.LogDeathDrops)
return;
DataManager.addEntry(new DataEntry(victim, DataType.MOB_DEATH, victim.getLocation(), Util.getEntityName(damager)));
}
//Other death
} else {
if (!Config.isLogged(DataType.OTHER_DEATH) && !Config.LogDeathDrops)
return;
EntityDamageEvent dEvent = victim.getLastDamageCause();
String cause = dEvent == null ? "Unknown" : victim.getLastDamageCause().getCause().name();
String[] words = cause.split("_");
for (int i = 0; i < words.length; i++) words[i] = words[i].substring(0, 1).toUpperCase() + words[i].substring(1).toLowerCase();
cause = Util.join(Arrays.asList(words), " ");
DataManager.addEntry(new DataEntry(victim, DataType.OTHER_DEATH, victim.getLocation(), cause));
}
//Log item drops
if (Config.LogDeathDrops) {
String data = null;
for (ItemStack stack : event.getDrops()) {
if (stack.getData() != null)
data = stack.getAmount() + "x " + stack.getTypeId() + ":" + stack.getData().getData();
else
data = stack.getAmount() + "x " + stack.getTypeId();
DataManager.addEntry(new DataEntry(victim, DataType.ITEM_DROP, victim.getLocation(), data));
}
}
} else {
//Mob Death
if (!Config.isLogged(DataType.ENTITY_KILL))
return;
if (entity.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
Entity damager = ((EntityDamageByEntityEvent) entity.getLastDamageCause()).getDamager();
//Only interested in player kills
if (!(damager instanceof Player))
return;
Player player = (Player) damager;
DataManager.addEntry(new DataEntry(player, DataType.ENTITY_KILL, entity.getLocation(), Util.getEntityName(entity)));
}
}
}
use of org.bukkit.inventory.ItemStack in project HawkEye by oliverwoodings.
the class BlockUtil method itemStringToStack.
/**
* Converts an item string into an ItemStack
* @param item item string representing the material and data
* @param amount
* @return an ItemStack
*/
public static ItemStack itemStringToStack(String item, Integer amount) {
String[] itemArr = item.split(":");
ItemStack stack = new ItemStack(Integer.parseInt(itemArr[0]), amount);
if (itemArr.length > 1)
stack.setData(new MaterialData(Integer.parseInt(itemArr[0]), Byte.parseByte(itemArr[1])));
return stack;
}
use of org.bukkit.inventory.ItemStack in project TotalFreedomMod by TotalFreedom.
the class Command_dispfill method run.
@Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) {
if (args.length == 2) {
int radius;
try {
radius = Math.max(5, Math.min(25, Integer.parseInt(args[0])));
} catch (NumberFormatException ex) {
sender.sendMessage("Invalid radius.");
return true;
}
final List<ItemStack> items = new ArrayList<>();
final String[] itemsRaw = StringUtils.split(args[1], ",");
for (final String searchItem : itemsRaw) {
Material material = Material.matchMaterial(searchItem);
if (material == null) {
try {
material = DepreciationAggregator.getMaterial(Integer.parseInt(searchItem));
} catch (NumberFormatException ex) {
}
}
if (material != null) {
items.add(new ItemStack(material, 64));
} else {
sender.sendMessage("Skipping invalid item: " + searchItem);
}
}
final ItemStack[] itemsArray = items.toArray(new ItemStack[items.size()]);
int affected = 0;
final Location centerLocation = playerSender.getLocation();
final Block centerBlock = centerLocation.getBlock();
for (int xOffset = -radius; xOffset <= radius; xOffset++) {
for (int yOffset = -radius; yOffset <= radius; yOffset++) {
for (int zOffset = -radius; zOffset <= radius; zOffset++) {
final Block targetBlock = centerBlock.getRelative(xOffset, yOffset, zOffset);
if (targetBlock.getLocation().distanceSquared(centerLocation) < (radius * radius)) {
if (targetBlock.getType().equals(Material.DISPENSER)) {
sender.sendMessage("Filling dispenser @ " + FUtil.formatLocation(targetBlock.getLocation()));
setDispenserContents(targetBlock, itemsArray);
affected++;
}
}
}
}
}
sender.sendMessage("Done. " + affected + " dispenser(s) filled.");
} else {
return false;
}
return true;
}
Aggregations