Java Code Examples for org.bukkit.inventory.PlayerInventory#getContents()

The following examples show how to use org.bukkit.inventory.PlayerInventory#getContents() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Gets the slot of the given item through iteration.
 * 
 * @param player The inventory to iterate through.
 * @param item The ItemStack to check for.
 * @return The found slot in the inventory.
 */
public static int getItemSlot(PlayerInventory inventory, ItemStack item)
{
	int slot = 0;
	ItemStack[] contents = inventory.getContents();
	
	for(int i = 0; i < contents.length; i++)
	{
		ItemStack current = contents[i];
		
		if(current == null)
		{
			continue;
		}
		
		if(current.equals(item))
		{
			slot = i;
			break;
		}
	}
	
	return slot;
}
 
Example 2
Source Project: UHC   File: ClearInventoryAction.java    License: MIT License 6 votes vote down vote up
@Override
protected void run(Player player) {
    final PlayerInventory inv = player.getInventory();

    // clear main inventory
    contents = inv.getContents();
    inv.clear();

    // clear armour slots
    armourContents = inv.getArmorContents();
    inv.setArmorContents(null);

    // clear if they have something on their cursour currently
    onCursor = player.getItemOnCursor();
    player.setItemOnCursor(new ItemStack(Material.AIR));

    // if they have a crafting inventory open clear items from it too
    // stops storing items in crafting slots bypassing clear inventories
    final InventoryView openInventory = player.getOpenInventory();
    if (openInventory.getType() == InventoryType.CRAFTING) {
        crafting = Optional.of(openInventory.getTopInventory().getContents());
        openInventory.getTopInventory().clear();
    } else {
        crafting = Optional.absent();
    }
}
 
Example 3
private double redeemItems(ScoreBox box, PlayerInventory inventory) {
  ItemStack[] notArmor = inventory.getContents();
  ItemStack[] armor = inventory.getArmorContents();

  double points = redeemItems(box, notArmor) + redeemItems(box, armor);

  if (points != 0) {
    inventory.setContents(notArmor);
    inventory.setArmorContents(armor);
  }

  return points;
}
 
Example 4
/**
 * Add an instructional booklet to a player's inventory
 * - Will prevent duplicates
 */
public static void getBooklet(GPlayer gPlayer) {
    Player onlinePlayer = gPlayer.getOnlinePlayer();
    if (onlinePlayer != null) {
        //Prevent duplicates:
        // - Note that empty inventory slots will be NULL
        boolean isDuplicate = false;
        PlayerInventory inventory = onlinePlayer.getInventory();
        for (ItemStack item : inventory.getContents()) {
            if (item != null &&
                    item.getType().equals(Material.WRITTEN_BOOK) &&
                    item.getItemMeta().getDisplayName().equals(Lang.WIKI_NAME.get())) {
                gPlayer.sendMsg(Lang.WIKI_ALREADYADDED);
                isDuplicate = true;
                break;
            }
        }

        //Add the booklet:
        if (!isDuplicate) {
            ItemStack wiki = new ItemStack(Material.WRITTEN_BOOK);
            final BookMeta meta = (BookMeta) wiki.getItemMeta();
            if (meta != null) {
                meta.setDisplayName(Lang.WIKI_NAME.get());
                meta.setAuthor(Lang.WIKI_AUTHOR.get());
                meta.setTitle(Lang.WIKI_LORE.get());
                meta.setPages(
                        Lang.WIKI_INTRODUCTION.get(),
                        Lang.WIKI_SCORES.get(),
                        Lang.WIKI_EFFECTS.get(),
                        Lang.WIKI_BOUNTY.get(),
                        Lang.WIKI_OTHER.get()
                        );

                //Create the book and add to inventory:
                wiki.setItemMeta(meta);
                if (onlinePlayer.getInventory().addItem(wiki).isEmpty()) {
                    //Added:
                    gPlayer.sendMsg(Lang.WIKI_ADDED);
                } else {
                    //Inventory full:
                    gPlayer.sendMsg(Lang.GENERIC_INVENTORYFULL);
                }
            }
        }
    }
}
 
Example 5
public void apply(Player player, boolean teleport) {
	PlayerInventory playerInv = player.getInventory();
	boolean is1_9 = MinecraftVersion.getImplementationVersion().compareTo(MinecraftVersion.V1_9) >= 0;
       boolean isSimpleSize = playerInv.getContents().length <= SIMPLE_INVENTORY_SIZE;

       ItemStack[] inventoryContents = new ItemStack[is1_9 && !isSimpleSize ? playerInv.getSize() : SIMPLE_INVENTORY_SIZE];
       System.arraycopy(inventory, 0, inventoryContents, 0, inventoryContents.length);

       if (!is1_9 || isSimpleSize) {
           ItemStack[] armorContents = new ItemStack[ARMOR_INVENTORY_SIZE];
           System.arraycopy(inventory, inventory.length - ARMOR_INVENTORY_SIZE, armorContents, 0, armorContents.length);
           playerInv.setArmorContents(armorContents);
       }
	
	playerInv.setContents(inventoryContents);

	player.setItemOnCursor(null);
	Map<Integer, ItemStack> exceeded = playerInv.addItem(onCursor);
	for (ItemStack stack : exceeded.values()) {
           if (stack.getType() == Material.AIR) {
               continue;
           }
           
		player.getWorld().dropItem(player.getLocation(), stack);
	}
	
	player.updateInventory();

       player.setMaxHealth(maxHealth);
	player.setHealth(health);
	player.setFoodLevel(foodLevel);
	player.setLevel(level);
	player.setExp(experience);
	player.setAllowFlight(allowFlight);
	player.setFlying(isFlying);
	
	/* Remove current potion effects */
	Collection<PotionEffect> effects = player.getActivePotionEffects();
	for (PotionEffect effect : effects) {
		player.removePotionEffect(effect.getType());
	}
	player.addPotionEffects(activeEffects);
	
	player.setExhaustion(exhaustion);
	player.setSaturation(saturation);
	player.setFallDistance(fallDistance);
	player.setFireTicks(fireTicks);
	
	if (scoreboard != player.getScoreboard()) {
		Scoreboard showBoard = scoreboard;
		if (scoreboard == null) {
			showBoard = Bukkit.getScoreboardManager().getMainScoreboard();
		}
		
		player.setScoreboard(showBoard);
	}
	
	if (teleport) {
		player.teleport(location);
	}
	
	Location compassTarget = this.compassTarget;
	
	if (compassTarget == null) {
		compassTarget = player.getWorld().getSpawnLocation();
	}
	
	player.setCompassTarget(compassTarget);
	
	for (WeakReference<Player> ref : cantSee) {
		Player cantSeePlayer = ref.get();
		
		if (cantSeePlayer == null) {
			// Player object has been garbage-collected
			continue;
		}
		
		if (!cantSeePlayer.isOnline()) {
			continue;
		}
		
		player.hidePlayer(cantSeePlayer);
	}
	
	player.setGameMode(gamemode);
}
 
Example 6
public void updateState(Player player, boolean location, GameMode mode) {
	PlayerInventory inventory = player.getInventory();
       boolean is1_9 = MinecraftVersion.getImplementationVersion().compareTo(MinecraftVersion.V1_9) >= 0;
       boolean isSimpleSize = inventory.getContents().length <= SIMPLE_INVENTORY_SIZE;

       ItemStack[] contents = inventory.getContents();
       ItemStack[] inventoryArray = new ItemStack[is1_9 && !isSimpleSize ? inventory.getSize() : INVENTORY_SIZE];

       if (!is1_9 || isSimpleSize) {
           ItemStack[] armor = inventory.getArmorContents();
           System.arraycopy(contents, 0, inventoryArray, 0, contents.length);
           System.arraycopy(armor, 0, inventoryArray, inventoryArray.length - ARMOR_INVENTORY_SIZE, armor.length);
       } else {
           System.arraycopy(contents, 0, inventoryArray, 0, contents.length);
       }
	
	/* Initialize the state with the current player state */
	setInventory(inventoryArray);
	setOnCursor(player.getItemOnCursor());
	setGamemode(mode != null ? mode : player.getGameMode());
       setMaxHealth(player.getMaxHealth());
	setHealth(player.getHealth(), player.getMaxHealth());
	setFoodLevel(player.getFoodLevel());
	setLevel(player.getLevel());
	setExperience(player.getExp());
	setAllowFlight(player.getAllowFlight());
	setFlying(player.isFlying());
	setActiveEffects(player.getActivePotionEffects());
	setExhaustion(player.getExhaustion());
	setSaturation(player.getSaturation());
	setFallDistance(player.getFallDistance());
	setFireTicks(player.getFireTicks());
	
	if (location) {
		setLocation(player.getLocation());
	}
	
	setScoreboard(player.getScoreboard());
	setCompassTarget(player.getCompassTarget());
	
	List<WeakReference<Player>> cantSee = Lists.newArrayList();
	for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
		if (player.canSee(onlinePlayer)) {
			continue;
		}
		
		WeakReference<Player> ref = new WeakReference<Player>(onlinePlayer);
		cantSee.add(ref);
	}
	
	setCantSee(cantSee);
}