Java Code Examples for org.bukkit.entity.Player#setItemOnCursor()

The following examples show how to use org.bukkit.entity.Player#setItemOnCursor() . 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
Source File: ClearInventoryAction.java    From UHC with 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 2
Source File: Trade.java    From TradePlus with GNU General Public License v3.0 5 votes vote down vote up
private void giveOnCursor(Player player) {
  if (player.getItemOnCursor().getType() != Material.AIR) {
    player
        .getInventory()
        .addItem(player.getItemOnCursor())
        .forEach((i, j) -> player.getWorld().dropItemNaturally(player.getLocation(), j));
    player.setItemOnCursor(null);
  }
}
 
Example 3
Source File: ClearInventoryAction.java    From UHC with MIT License 5 votes vote down vote up
@Override
protected void revert(Player player) {
    final PlayerInventory inv = player.getInventory();

    inv.setContents(contents);
    inv.setArmorContents(armourContents);
    player.setItemOnCursor(onCursor);

    if (crafting.isPresent()) {
        // add back to main inventory instead of the crafting slots
        inv.addItem(crafting.get());
    }
}
 
Example 4
Source File: PlayerStateHolder.java    From HeavySpleef with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Applies the default state to the player
 * and discards the current one<br><br>
 * 
 * Warning: This deletes the entire inventory
 * and all other various player attributes
 * 
 * It is recommended to save the player state
 * with {@link PlayerStateHolder#create(Player, GameMode)}
 * and store a reference to it before invoking this method
 * 
 * @param player
 */
public static void applyDefaultState(Player player, boolean adventureMode) {
	player.setGameMode(adventureMode ? GameMode.ADVENTURE : GameMode.SURVIVAL);
	player.getInventory().clear();
	player.getInventory().setArmorContents(new ItemStack[4]);
	player.setItemOnCursor(null);
	player.updateInventory();
       player.setMaxHealth(20.0);
	player.setHealth(20.0);
	player.setFoodLevel(20);
	player.setLevel(0);
	player.setExp(0f);
	player.setAllowFlight(false);
	player.setFlying(false);
	player.setFallDistance(0);
	player.setFireTicks(0);
	
	Collection<PotionEffect> effects = player.getActivePotionEffects();
	for (PotionEffect effect : effects) {
		player.removePotionEffect(effect.getType());
	}
	
	for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
		if (player.canSee(player)) {
			continue;
		}
		
		player.showPlayer(onlinePlayer);
	}
}
 
Example 5
Source File: AnvilListener.java    From MineTinker with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onInventoryClick(InventoryClickEvent event) {
	HumanEntity he = event.getWhoClicked();

	if (!(he instanceof Player && event.getClickedInventory() instanceof AnvilInventory)) {
		return;
	}

	AnvilInventory inv = (AnvilInventory) event.getClickedInventory();
	Player player = (Player) he;

	ItemStack tool = inv.getItem(0);
	ItemStack modifier = inv.getItem(1);
	ItemStack newTool = inv.getItem(2);

	if (tool == null || modifier == null || newTool == null) {
		return;
	}

	if (event.getSlot() != 2) {
		return;
	}

	if (Lists.WORLDS.contains(player.getWorld().getName())) {
		return;
	}

	if (!(modManager.isToolViable(tool) || modManager.isArmorViable(tool))) {
		return;
	}

	//boolean deleteAllItems = false;
	if (event.getCursor() != null && !event.getCursor().getType().equals(Material.AIR)) {
		return;
	}

	if (!modManager.isModifierItem(modifier)) { //upgrade
		if (tool.getType().equals(newTool.getType())) return; //Not an upgrade

		if (new Random().nextInt(100) < MineTinker.getPlugin().getConfig().getInt("ChanceToFailToolUpgrade")) {
			newTool = tool;
			Bukkit.getPluginManager().callEvent(new ToolUpgradeEvent(player, newTool, false));
		} else {
			Bukkit.getPluginManager().callEvent(new ToolUpgradeEvent(player, newTool, true));
		}

		// ------ upgrade
		if (event.isShiftClick()) {
			if (player.getInventory().addItem(newTool).size() != 0) { //adds items to (full) inventory and then case if inventory is full
				event.setCancelled(true); //cancels the event if the player has a full inventory
				return;
			} // no else as it gets added in if-clause

			inv.clear();
			return;
		}

		player.setItemOnCursor(newTool);
		inv.clear();
	} else { //is modifier
		Modifier mod = modManager.getModifierFromItem(modifier);

		if (mod == null) {
			return;
		}

		modifier.setAmount(modifier.getAmount() - 1);

		if (new Random().nextInt(100) < MineTinker.getPlugin().getConfig().getInt("ChanceToFailModifierApply")) {
			newTool = tool;
			Bukkit.getPluginManager().callEvent(new ModifierFailEvent(player, tool, mod, ModifierFailCause.PLAYER_FAILURE, false));
		} else {
			Bukkit.getPluginManager().callEvent(new ModifierApplyEvent(player, tool, mod, modManager.getFreeSlots(newTool), false));
		}

		if (event.isShiftClick()) {
			if (player.getInventory().addItem(newTool).size() != 0) { //adds items to (full) inventory and then case if inventory is full
				event.setCancelled(true); //cancels the event if the player has a full inventory
				return;
			} // no else as it gets added in if-clause

			inv.clear();
			inv.setItem(1, modifier);

			return;
		}

		player.setItemOnCursor(newTool);

		inv.clear();
		inv.setItem(1, modifier);
	}
}
 
Example 6
Source File: PlayerStateHolder.java    From HeavySpleef with GNU General Public License v3.0 4 votes vote down vote up
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);
}