当前位置: 首页>>代码示例>>Java>>正文


Java PlayerInventory.getItem方法代码示例

本文整理汇总了Java中org.bukkit.inventory.PlayerInventory.getItem方法的典型用法代码示例。如果您正苦于以下问题:Java PlayerInventory.getItem方法的具体用法?Java PlayerInventory.getItem怎么用?Java PlayerInventory.getItem使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.bukkit.inventory.PlayerInventory的用法示例。


在下文中一共展示了PlayerInventory.getItem方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getConsumeSlots

import org.bukkit.inventory.PlayerInventory; //导入方法依赖的package包/类
public static List<Integer> getConsumeSlots(Player player, ItemStack consumeItem) {
	if(consumeItem == null) return null;

	List<Integer> list = new ArrayList<Integer>();
	PlayerInventory inventory = player.getInventory();
	int inventorySize = inventory.getSize();
	int requiredAmount = consumeItem.getAmount();

	try {
		for(int i = 0; i < inventorySize && requiredAmount > 0; i++) {
			ItemStack item = inventory.getItem(i);

			if(item == null || !consumeItem.isSimilar(item)) {
				continue;
			}

			list.add(i);

			requiredAmount -= item.getAmount();
		}
       } catch (Exception ex) {
       }

	return requiredAmount > 0 ? null :list;
}
 
开发者ID:Aleksey-Terzi,项目名称:CastleGates,代码行数:26,代码来源:Helper.java

示例2: save

import org.bukkit.inventory.PlayerInventory; //导入方法依赖的package包/类
/**
 * Saves the inventory of given player into a map
 *
 * @param player The player
 */
public static void save(Player player){
    PlayerInventory inventory = player.getInventory();

    List<SimplePair<ItemStack, Integer>> contents = new ArrayList<>();
    for(int i = 0; i < inventory.getSize(); i++){
        ItemStack item = inventory.getItem(i);

        if(item == null || item.getType() == Material.AIR){
            continue;
        }

        contents.add(new SimplePair<>(item, i));
    }

    contentMap.put(player, contents);
}
 
开发者ID:Superioz,项目名称:CastleRush,代码行数:22,代码来源:InventoryBackup.java

示例3: giveItem

import org.bukkit.inventory.PlayerInventory; //导入方法依赖的package包/类
public static void giveItem(Player player, ItemStack item) {
	PlayerInventory inv = player.getInventory();
	for (int i = 0; i < inv.getSize(); i++) {
		if (inv.getItem(i) == null) {
			inv.setItem(i, item);
			return;
		}
	}
	player.getWorld().dropItem(player.getLocation(), item);
}
 
开发者ID:bobmandude9889,项目名称:iZenith-PVP,代码行数:11,代码来源:Util.java

示例4: onPlayerItemHoldEvent

import org.bukkit.inventory.PlayerInventory; //导入方法依赖的package包/类
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerItemHoldEvent(final PlayerItemHeldEvent event) {
    final Player bukkitPlayer = event.getPlayer();
    if (bukkitPlayer.isSneaking()) {
        return;
    }
    FawePlayer<Object> fp = FawePlayer.wrap(bukkitPlayer);
    com.sk89q.worldedit.entity.Player player = fp.getPlayer();
    LocalSession session = fp.getSession();
    Tool tool = session.getTool(player);
    if (tool instanceof ScrollTool) {
        final int slot = event.getNewSlot();
        final int oldSlot = event.getPreviousSlot();
        final int ri;
        if ((((slot - oldSlot) <= 4) && ((slot - oldSlot) > 0)) || (((slot - oldSlot) < -4))) {
            ri = 1;
        } else {
            ri = -1;
        }
        ScrollTool scrollable = (ScrollTool) tool;
        if (scrollable.increment(player, ri)) {
            if (Settings.IMP.EXPERIMENTAL.PERSISTENT_BRUSHES) {
                bukkitPlayer.getInventory().setHeldItemSlot(oldSlot);
            } else {
                final PlayerInventory inv = bukkitPlayer.getInventory();
                final ItemStack item = inv.getItem(slot);
                final ItemStack newItem = inv.getItem(oldSlot);
                inv.setItem(slot, newItem);
                inv.setItem(oldSlot, item);
                bukkitPlayer.updateInventory();
            }
        }
    }
}
 
开发者ID:boy0001,项目名称:FastAsyncWorldedit,代码行数:35,代码来源:BrushListener.java

示例5: getMapSlot

import org.bukkit.inventory.PlayerInventory; //导入方法依赖的package包/类
private int getMapSlot(Player player) {
    PlayerInventory inventory = player.getInventory();
    for (int i = 0; i < 9; i++) {
        ItemStack item = inventory.getItem(i);
        if (item != null && item.getType() == Material.MAP) {
            return i;
        }
    }
    return -1;
}
 
开发者ID:boy0001,项目名称:FastAsyncWorldedit,代码行数:11,代码来源:BukkitImageViewer.java

示例6: update

import org.bukkit.inventory.PlayerInventory; //导入方法依赖的package包/类
public void update(final Player player) {
    final PlayerInventory inv = player.getInventory();
    final ItemData[] updated = new ItemData[equipped.length];
    for (int i = 0; i < updated.length; i++) {
        final ItemStack item = i < slots.length ? inv.getItem(slots[i]) : inv.getItemInMainHand();
        updated[i] = ItemData.from(item);
    }
    swap(player, updated);
}
 
开发者ID:Eniripsa96,项目名称:EnchantmentAPI,代码行数:10,代码来源:PlayerEquips.java

示例7: save

import org.bukkit.inventory.PlayerInventory; //导入方法依赖的package包/类
/**
 * Save Emeralds in the team treasure.
 * @param amount
 * @return
 */
public boolean save(int amount) {
    Player player = toOnlinePlayer();
    if (amount < 0) {
        Messages.sendMessage(Messages.color(amount) + " is not a valid amount of emeralds to store in the team treasure.", Messages.messageType.INGAME, player);
        return false;
    } else {
        int amountToSave = amount;
        PlayerInventory playerInventory = player.getInventory();
        if (playerInventory.contains(Material.EMERALD, amount)) {
            do {
                int index = playerInventory.first(Material.EMERALD);
                ItemStack itemStack = playerInventory.getItem(index);
                if (itemStack.getAmount() > amount) {
                    itemStack.setAmount(itemStack.getAmount() - amount);
                    playerInventory.setItem(index, itemStack);
                    amount = 0;
                } else {
                    amount -= itemStack.getAmount();
                    itemStack.setAmount(0);
                    playerInventory.setItem(index, itemStack);
                }
            } while (amount != 0);
            _team.earnMoney(amountToSave);
            player.updateInventory();
        } else {
            Messages.sendMessage("Sorry, you do not have " + Messages.color(amount) + " emerald(s) in your inventory !", Messages.messageType.INGAME, player);
            return false;
        }
    }
    _team.sendMessage(Messages.color(_name) + " saved " + Messages.color(amount) + " emerald(s) in the team treasure !");
    Messages.sendMessage("The treasure value is now " + Messages.color(_team.get_money()) + ".", Messages.messageType.INGAME, player);
    return true;
}
 
开发者ID:lolopasdugato,项目名称:MCWarClan,代码行数:39,代码来源:MCWarClanPlayer.java

示例8: saferItemGive

import org.bukkit.inventory.PlayerInventory; //导入方法依赖的package包/类
public static void saferItemGive(PlayerInventory playerInventory, ItemStack item) 
    {
    	// After bukkit is fixed:
    	// player.getInventory().addItem(givingItems);
		
        while (true) 
        {
            // Do we already have a stack of it?
            int firstPartial = firstPartial(item, (ItemStack[])playerInventory.getContents());

            // Drat! no partial stack
            if (firstPartial == -1) 
            {
                // Find a free spot!
                int firstFree = firstEmpty((ItemStack[])playerInventory.getContents());

                if (firstFree == -1) 
                {
                    // No space at all!
                    // Bukkit returns unplaced items, but floAuction only calls this after checking for space.
                    break;
                } 
                else 
                {
                	// Again, floAuction checks for this elsewhere before calling this...technically this would never occur, no reason to code for it.
                	
                    // More than a single stack!
/*                    if (item.getAmount() > getMaxStackSize(item)) {
                        CraftItemStack stack = new CraftItemStack(item.getTypeId(), getMaxStackSize(item), item.getDurability());
                        stack.addUnsafeEnchantments(item.getEnchantments());
                        playerInventory.setItem(firstFree, stack);
                        item.setAmount(item.getAmount() - getMaxStackSize(item));
                    } else {*/
                        // Just store it
                    	playerInventory.setItem(firstFree, item);
                        break;
//                    }
                }
            } 
            else 
            {
                // So, apparently it might only partially fit, well lets do just that
                ItemStack partialItem = playerInventory.getItem(firstPartial);

                int amount = item.getAmount();
                int partialAmount = partialItem.getAmount();
                int maxAmount = partialItem.getMaxStackSize();

                // 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);
            }
        }		
		
	}
 
开发者ID:virustotalop,项目名称:ObsidianAuctions,代码行数:62,代码来源:Items.java

示例9: remove

import org.bukkit.inventory.PlayerInventory; //导入方法依赖的package包/类
public static void remove(String playerName, int amount, ItemStack compareItem){
	Player player = Bukkit.getPlayer(playerName);
	if (player != null) {
		PlayerInventory inventory = player.getInventory();
    	
    	// Remove held item first:
		if (isSameItem(compareItem, player.getItemInHand())) { 
	    	int heldAmount = player.getItemInHand().getAmount();
	    	if (heldAmount <= amount) {
	    		amount -= heldAmount;
	    		inventory.clear(inventory.getHeldItemSlot());
	    	} else {
	    		player.getItemInHand().setAmount(heldAmount - amount);
	    		amount = 0;
	    	}
		}
    	
    	int counter = amount;
    	int leftover = 0;

    	// Remove from other stacks:
    	for (int invIndex = 0; invIndex < inventory.getSize(); invIndex++) {
    		ItemStack current = inventory.getItem(invIndex);

    		if (current == null || current.getAmount() <= 0) 
    			continue;
    		
    		if (!isSameItem(compareItem, current)) 
    			continue;

    		if (current.getAmount() > counter) {
    			leftover = current.getAmount() - counter;
    		}

    		if (leftover != 0) {
    			current.setAmount(leftover);
    			counter = 0;
    			break;
    		} else {
    			counter -= current.getAmount();
    			inventory.clear(invIndex);
    		}
    	}
	}
}
 
开发者ID:virustotalop,项目名称:ObsidianAuctions,代码行数:46,代码来源:Items.java

示例10: saveInventory

import org.bukkit.inventory.PlayerInventory; //导入方法依赖的package包/类
private void saveInventory(PlayerInventory inv) {
    Map<String, Object> inventory = new HashMap<String, Object>();
    for(int slot = 0; slot < inv.getSize(); slot++) {
        ItemStack item = inv.getItem(slot);
        if(item != null) {
            inventory.put(String.valueOf(slot), inv.getItem(slot).serialize());
        }
    }

    ItemStack helmet = inv.getHelmet();
    ItemStack chestplate = inv.getChestplate();
    ItemStack leggings = inv.getLeggings();
    ItemStack boots = inv.getBoots();

    if(helmet != null) {
        inventory.put("helmet", helmet.serialize());
    }

    if(chestplate != null) {
        inventory.put("chestplate", chestplate.serialize());
    }

    if(leggings != null) {
        inventory.put("leggings", leggings.serialize());
    }

    if(boots != null) {
        inventory.put("boots", boots.serialize());
    }

    try {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        BukkitObjectOutputStream out = new BukkitObjectOutputStream(byteOut);
        out.writeObject(inventory);
        out.close();

        this.database.setValue(this.key + ".inventory", byteOut.toByteArray());
    } catch(IOException e) {
        this.module.getLogger().log(Level.SEVERE, "Failed to save inventory.", e);
    }
}
 
开发者ID:Steveice10,项目名称:Peacecraft,代码行数:42,代码来源:WorldPlayerData.java

示例11: run

import org.bukkit.inventory.PlayerInventory; //导入方法依赖的package包/类
@Override
public void run() {
    boolean didWork = false;

    while (!didWork) {
        // first, some validation & sanity checking...
        SwapRecord rec = queue.poll();
        if (rec == null) {
            cancel();
            return;
        }

        if (!rec.player.isOnline()) {
            continue;
        }
        Block b = rec.block;
        if (b.getType() == rec.target.getItemType() && b.getData() == rec.target.getData()
                || rec.builder.getCharge() < rec.chargeNeeded
                || !canReplace(rec.player, rec.block)) {
            continue;
        }

        // (hopefully) take materials from the player...
        int slot = rec.slot;
        PlayerInventory inventory = rec.player.getInventory();
        if (slot < 0 || inventory.getItem(slot) == null) {
            slot = getSlotForItem(rec.player, rec.target);
            if (slot == -1) {
                // player is out of materials to swap: scan the queue and remove any other
                // records for this player & material, to avoid constant inventory rescanning
                Iterator<SwapRecord> iter = queue.iterator();
                while (iter.hasNext()) {
                    SwapRecord r = iter.next();
                    if (r.player.equals(rec.player) && r.target.equals(rec.target)) {
                        iter.remove();
                    }
                }
                continue;
            }
        }
        ItemStack item = inventory.getItem(slot);
        item.setAmount(item.getAmount() - 1);
        inventory.setItem(slot, item.getAmount() > 0 ? item : null);

        // take SCU from the multibuilder...
        rec.builder.setCharge(rec.builder.getCharge() - rec.chargeNeeded);
        ItemStack builderItem = rec.builder.toItemStack();
        rec.player.setItemInHand(builderItem);

        // give materials to the player...
        if (builderItem.getEnchantmentLevel(Enchantment.SILK_TOUCH) == 1) {
            tool.addEnchantment(Enchantment.SILK_TOUCH, 1);
        } else {
            tool.removeEnchantment(Enchantment.SILK_TOUCH);
        }
        for (ItemStack stack : STBUtil.calculateDrops(b, tool)) {
            STBUtil.giveItems(rec.player, stack);
        }

        // make the actual in-world swap
        b.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, b.getType());
        b.setTypeIdAndData(rec.target.getItemTypeId(), rec.target.getData(), true);

        // queue up the next set of blocks
        if (rec.layersLeft > 0) {
            for (int x = -1; x <= 1; x++) {
                for (int y = -1; y <= 1; y++) {
                    for (int z = -1; z <= 1; z++) {
                        Block b1 = b.getRelative(x, y, z);
                        if ((x != 0 || y != 0 || z != 0) && b1.getType() == rec.source.getItemType() && b1.getData() == rec.source.getData() && STBUtil.isExposed(b1)) {
                            queue.offer(new SwapRecord(rec.player, b1, rec.source, rec.target, rec.layersLeft - 1, rec.builder, slot, rec.chargeNeeded));
                        }
                    }
                }
            }
        }

        didWork = true;
    }
}
 
开发者ID:desht,项目名称:sensibletoolbox,代码行数:81,代码来源:MultiBuilder.java

示例12: walkInventory

import org.bukkit.inventory.PlayerInventory; //导入方法依赖的package包/类
@SuppressWarnings("deprecation")
public void walkInventory(PlayerInventory inventory, Player player, ItemStack is, int slot) {
  db.openConnection();

  int amount = is.getAmount();
  short durability = is.getDurability();
  int type = is.getTypeId();
  byte data = is.getData().getData();
  
  db.saveItem(slot, player.getName(), amount, durability, type, data);
  
  NBTContainerItem con = null;
  
  switch (slot) {
  case -100:
    con = new NBTContainerItem(inventory.getHelmet());
    break;

  case -101:
    con = new NBTContainerItem(inventory.getChestplate());
    break;

  case -102:
    con = new NBTContainerItem(inventory.getLeggings());
    break;
    
  case -103:
    con = new NBTContainerItem(inventory.getBoots());
    break;

  default:
    con = new NBTContainerItem(inventory.getItem(slot));
    break;
  }
  
  NBTTagCompound compound = con.getTag();
  if (compound == null) return;

  try {
    saveExtraNBTData(compound, slot, -1, player);
  } catch (SQLException e) {
    e.printStackTrace();
  }
}
 
开发者ID:nsg,项目名称:SyncNBT,代码行数:45,代码来源:NBTData.java


注:本文中的org.bukkit.inventory.PlayerInventory.getItem方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。