Search in sources :

Example 1 with PlayerInventory

use of org.bukkit.inventory.PlayerInventory in project Prism-Bukkit by prism.

the class WandBase method disable.

/**
	 * 
	 */
public void disable(Player player) {
    final PlayerInventory inv = player.getInventory();
    if (itemWasGiven()) {
        int itemSlot;
        // Likely is what they're holding
        if (inv.getItemInHand().getTypeId() == item_id && inv.getItemInHand().getDurability() == item_subid) {
            itemSlot = inv.getHeldItemSlot();
        } else {
            itemSlot = InventoryUtils.inventoryHasItem(inv, item_id, item_subid);
        }
        if (itemSlot > -1) {
            InventoryUtils.subtractAmountFromPlayerInvSlot(inv, itemSlot, 1);
            player.updateInventory();
        }
    }
    if (original_item != null) {
        InventoryUtils.moveItemToHand(inv, original_item.getTypeId(), (byte) original_item.getDurability());
    }
}
Also used : PlayerInventory(org.bukkit.inventory.PlayerInventory)

Example 2 with PlayerInventory

use of org.bukkit.inventory.PlayerInventory in project Denizen-For-Bukkit by DenizenScript.

the class dInventory method getEquipment.

public dList getEquipment() {
    ItemStack[] equipment = null;
    if (inventory instanceof PlayerInventory) {
        equipment = ((PlayerInventory) inventory).getArmorContents();
    } else if (inventory instanceof HorseInventory) {
        equipment = new ItemStack[] { ((HorseInventory) inventory).getSaddle(), ((HorseInventory) inventory).getArmor() };
    }
    if (equipment == null) {
        return null;
    }
    dList equipmentList = new dList();
    for (ItemStack item : equipment) {
        equipmentList.add(new dItem(item).identify());
    }
    return equipmentList;
}
Also used : PlayerInventory(org.bukkit.inventory.PlayerInventory) HorseInventory(org.bukkit.inventory.HorseInventory) ItemStack(org.bukkit.inventory.ItemStack)

Example 3 with PlayerInventory

use of org.bukkit.inventory.PlayerInventory in project Glowstone by GlowstoneMC.

the class HumanEntityStore method load.

// documented at http://minecraft.gamepedia.com/Player.dat_Format
// player data that does not correspond to HumanEntity is in PlayerStore
@Override
public void load(T entity, CompoundTag tag) {
    super.load(entity, tag);
    if (tag.isInt("XpSeed")) {
        entity.setXpSeed(tag.getInt("XpSeed"));
    }
    if (tag.isInt("playerGameType")) {
        GlowServer server = (GlowServer) Bukkit.getServer();
        if (!server.getConfig().getBoolean(ServerConfig.Key.FORCE_GAMEMODE)) {
            GameMode mode = GameMode.getByValue(tag.getInt("playerGameType"));
            if (mode != null) {
                entity.setGameMode(mode);
            }
        } else {
            entity.setGameMode(server.getDefaultGameMode());
        }
    }
    if (tag.isInt("SelectedItemSlot")) {
        entity.getInventory().setHeldItemSlot(tag.getInt("SelectedItemSlot"));
    }
    if (tag.isList("Inventory", TagType.COMPOUND)) {
        PlayerInventory inventory = entity.getInventory();
        List<CompoundTag> items = tag.getCompoundList("Inventory");
        inventory.setStorageContents(NbtSerialization.readInventory(items, 0, inventory.getSize() - 5));
        inventory.setArmorContents(NbtSerialization.readInventory(items, 100, 4));
        inventory.setExtraContents(NbtSerialization.readInventory(items, -106, 1));
    }
    if (tag.isList("EnderItems", TagType.COMPOUND)) {
        Inventory inventory = entity.getEnderChest();
        List<CompoundTag> items = tag.getCompoundList("EnderItems");
        inventory.setContents(NbtSerialization.readInventory(items, 0, inventory.getSize()));
    }
}
Also used : GameMode(org.bukkit.GameMode) PlayerInventory(org.bukkit.inventory.PlayerInventory) GlowServer(net.glowstone.GlowServer) CompoundTag(net.glowstone.util.nbt.CompoundTag) PlayerInventory(org.bukkit.inventory.PlayerInventory) Inventory(org.bukkit.inventory.Inventory)

Example 4 with PlayerInventory

use of org.bukkit.inventory.PlayerInventory in project Essentials by drtshock.

the class InventoryWorkaround method addOversizedItems.

// Returns what it couldn't store
// Set oversizedStack to below normal stack size to disable oversized stacks
public static Map<Integer, ItemStack> addOversizedItems(final Inventory inventory, final int oversizedStacks, final ItemStack... items) {
    if (isCombinedInventory(inventory)) {
        Inventory fakeInventory = makeTruncatedPlayerInventory((PlayerInventory) inventory);
        Map<Integer, ItemStack> overflow = addOversizedItems(fakeInventory, oversizedStacks, items);
        for (int i = 0; i < fakeInventory.getContents().length; i++) {
            inventory.setItem(i, fakeInventory.getContents()[i]);
        }
        return overflow;
    }
    final Map<Integer, ItemStack> leftover = new HashMap<>();
    /*
         * TODO: some optimization - Create a 'firstPartial' with a 'fromIndex' - Record the lastPartial per Material -
		 * Cache firstEmpty result
		 */
    // combine items
    final ItemStack[] combined = new ItemStack[items.length];
    for (ItemStack item : items) {
        if (item == null || item.getAmount() < 1) {
            continue;
        }
        for (int j = 0; j < combined.length; j++) {
            if (combined[j] == null) {
                combined[j] = item.clone();
                break;
            }
            if (combined[j].isSimilar(item)) {
                combined[j].setAmount(combined[j].getAmount() + item.getAmount());
                break;
            }
        }
    }
    for (int i = 0; i < combined.length; i++) {
        final ItemStack item = combined[i];
        if (item == null || item.getType() == Material.AIR) {
            continue;
        }
        while (true) {
            // Do we already have a stack of it?
            final int maxAmount = oversizedStacks > item.getType().getMaxStackSize() ? oversizedStacks : item.getType().getMaxStackSize();
            final int firstPartial = firstPartial(inventory, item, maxAmount);
            // Drat! no partial stack
            if (firstPartial == -1) {
                // Find a free spot!
                final int firstFree = inventory.firstEmpty();
                if (firstFree == -1) {
                    // No space at all!
                    leftover.put(i, item);
                    break;
                } else {
                    // More than a single stack!
                    if (item.getAmount() > maxAmount) {
                        final ItemStack stack = item.clone();
                        stack.setAmount(maxAmount);
                        inventory.setItem(firstFree, stack);
                        item.setAmount(item.getAmount() - maxAmount);
                    } else {
                        // Just store it
                        inventory.setItem(firstFree, item);
                        break;
                    }
                }
            } else {
                // So, apparently it might only partially fit, well lets do just that
                final ItemStack partialItem = inventory.getItem(firstPartial);
                final int amount = item.getAmount();
                final int partialAmount = partialItem.getAmount();
                // Check if it fully fits
                if (amount + partialAmount <= maxAmount) {
                    partialItem.setAmount(amount + partialAmount);
                    break;
                }
                // It fits partially
                partialItem.setAmount(maxAmount);
                item.setAmount(amount + partialAmount - maxAmount);
            }
        }
    }
    return leftover;
}
Also used : HashMap(java.util.HashMap) ItemStack(org.bukkit.inventory.ItemStack) PlayerInventory(org.bukkit.inventory.PlayerInventory) Inventory(org.bukkit.inventory.Inventory)

Example 5 with PlayerInventory

use of org.bukkit.inventory.PlayerInventory in project Essentials by drtshock.

the class Commandhat method run.

@Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
    if (args.length > 0 && (args[0].contains("rem") || args[0].contains("off") || args[0].equalsIgnoreCase("0"))) {
        final PlayerInventory inv = user.getBase().getInventory();
        final ItemStack head = inv.getHelmet();
        if (head == null || head.getType() == Material.AIR) {
            user.sendMessage(tl("hatEmpty"));
        } else {
            final ItemStack air = new ItemStack(Material.AIR);
            inv.setHelmet(air);
            InventoryWorkaround.addItems(user.getBase().getInventory(), head);
            user.sendMessage(tl("hatRemoved"));
        }
    } else {
        if (user.getBase().getItemInHand().getType() != Material.AIR) {
            final ItemStack hand = user.getBase().getItemInHand();
            if (hand.getType().getMaxDurability() == 0) {
                final PlayerInventory inv = user.getBase().getInventory();
                final ItemStack head = inv.getHelmet();
                inv.setHelmet(hand);
                inv.setItemInHand(head);
                user.sendMessage(tl("hatPlaced"));
            } else {
                user.sendMessage(tl("hatArmor"));
            }
        } else {
            user.sendMessage(tl("hatFail"));
        }
    }
}
Also used : PlayerInventory(org.bukkit.inventory.PlayerInventory) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

PlayerInventory (org.bukkit.inventory.PlayerInventory)12 ItemStack (org.bukkit.inventory.ItemStack)8 Inventory (org.bukkit.inventory.Inventory)4 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 Location (org.bukkit.Location)2 PotionEffect (org.bukkit.potion.PotionEffect)2 MapDisplayInfo (com.bergerkiller.bukkit.common.internal.CommonMapController.MapDisplayInfo)1 ResultSet (java.sql.ResultSet)1 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 GlowServer (net.glowstone.GlowServer)1 CompoundTag (net.glowstone.util.nbt.CompoundTag)1 GameMode (org.bukkit.GameMode)1 Material (org.bukkit.Material)1 HorseInventory (org.bukkit.inventory.HorseInventory)1 ItemMeta (org.bukkit.inventory.meta.ItemMeta)1