Java Code Examples for org.spongepowered.api.entity.living.player.Player#getItemInHand()

The following examples show how to use org.spongepowered.api.entity.living.player.Player#getItemInHand() . 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: ItemloreCommand.java    From UltimateCore with MIT License 6 votes vote down vote up
@Override
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
    checkIfPlayer(sender);
    checkPermission(sender, ItemPermissions.UC_ITEM_ITEMLORE_BASE);
    Player p = (Player) sender;

    if (p.getItemInHand(HandTypes.MAIN_HAND).getType().equals(ItemTypes.NONE)) {
        throw new ErrorMessageException(Messages.getFormatted(p, "item.noiteminhand"));
    }
    ItemStack stack = p.getItemInHand(HandTypes.MAIN_HAND);

    Text unsplitlore = Messages.toText(args.<String>getOne("lore").get());
    stack.offer(Keys.ITEM_LORE, unsplitlore.toPlain().contains("|") ? TextUtil.split(unsplitlore, "|") : Arrays.asList(unsplitlore));
    p.setItemInHand(HandTypes.MAIN_HAND, stack);
    Messages.send(sender, "item.command.itemlore.success", "%arg%", unsplitlore);
    return CommandResult.success();
}
 
Example 2
Source File: ItemunbreakableCommand.java    From UltimateCore with MIT License 6 votes vote down vote up
@Override
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
    checkIfPlayer(sender);
    checkPermission(sender, ItemPermissions.UC_ITEM_ITEMUNBREAKABLE_BASE);
    Player p = (Player) sender;

    Boolean value = args.<Boolean>getOne("enabled/disabled").get();

    if (p.getItemInHand(HandTypes.MAIN_HAND).getType().equals(ItemTypes.NONE)) {
        throw new ErrorMessageException(Messages.getFormatted(p, "item.noiteminhand"));
    }
    ItemStack stack = p.getItemInHand(HandTypes.MAIN_HAND);

    if (!stack.supports(Keys.UNBREAKABLE)) {
        throw new ErrorMessageException(Messages.getFormatted(sender, "item.command.itemunbreakable.notsupported"));
    }

    stack.offer(Keys.UNBREAKABLE, value);
    p.setItemInHand(HandTypes.MAIN_HAND, stack);
    Messages.send(sender, "item.command.itemunbreakable.success", "%arg%", value);
    return CommandResult.success();
}
 
Example 3
Source File: ItemquantityCommand.java    From UltimateCore with MIT License 6 votes vote down vote up
@Override
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
    checkIfPlayer(sender);
    checkPermission(sender, ItemPermissions.UC_ITEM_ITEMDURABILITY_BASE);
    Player p = (Player) sender;

    if (p.getItemInHand(HandTypes.MAIN_HAND).getType().equals(ItemTypes.NONE)) {
        throw new ErrorMessageException(Messages.getFormatted(p, "item.noiteminhand"));
    }
    ItemStack stack = p.getItemInHand(HandTypes.MAIN_HAND);
    int quantity = args.<Integer>getOne("quantity").get();

    if (quantity > stack.getMaxStackQuantity()) {
        throw new ErrorMessageException(Messages.getFormatted(sender, "item.numberinvalid", "%number%", quantity));
    }

    stack.setQuantity(quantity);
    p.setItemInHand(HandTypes.MAIN_HAND, stack);
    Messages.send(sender, "item.command.itemquantity.success", "%arg%", quantity);
    return CommandResult.success();
}
 
Example 4
Source File: InteractPermListener.java    From Nations with MIT License 6 votes vote down vote up
@Listener(order=Order.FIRST, beforeModifications = true)
public void onInteract(InteractBlockEvent event, @First Player player)
{
	if (!ConfigHandler.getNode("worlds").getNode(player.getWorld().getName()).getNode("enabled").getBoolean())
	{
		return;
	}
	if (player.hasPermission("nations.admin.bypass.perm.interact"))
	{
		return;
	}
	Optional<ItemStack> optItem = player.getItemInHand(HandTypes.MAIN_HAND);
	if (optItem.isPresent() && (ConfigHandler.isWhitelisted("use", optItem.get().getItem().getId()) || optItem.get().getItem().equals(ItemTypes.GOLDEN_AXE) && ConfigHandler.getNode("others", "enableGoldenAxe").getBoolean(true)))
		return;
	event.getTargetBlock().getLocation().ifPresent(loc -> {
		if (!DataHandler.getPerm("interact", player.getUniqueId(), loc))
		{
			event.setCancelled(true);
			if (loc.getBlockType() != BlockTypes.STANDING_SIGN && loc.getBlockType() != BlockTypes.WALL_SIGN)
				player.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_PERM_INTERACT));
		}
	});
}
 
Example 5
Source File: ItemhidetagsCommand.java    From UltimateCore with MIT License 6 votes vote down vote up
@Override
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
    checkIfPlayer(sender);
    checkPermission(sender, ItemPermissions.UC_ITEM_ITEMHIDETAGS_BASE);
    Player p = (Player) sender;

    if (p.getItemInHand(HandTypes.MAIN_HAND).getType().equals(ItemTypes.NONE)) {
        throw new ErrorMessageException(Messages.getFormatted(p, "item.noiteminhand"));
    }
    ItemStack stack = p.getItemInHand(HandTypes.MAIN_HAND);

    Key<Value<Boolean>> key = args.<Key<Value<Boolean>>>getOne("tag").get();
    boolean value = args.<Boolean>getOne("enabled").get();

    stack.offer(key, value);
    p.setItemInHand(HandTypes.MAIN_HAND, stack);
    Messages.send(sender, "item.command.itemhidetags.success", "%tag%", key.getName(), "%status%", Messages.getFormatted(value ? "item.command.itemhidetags.hidden" : "item.command.itemhidetags.shown"));
    return CommandResult.success();
}
 
Example 6
Source File: ItemnameCommand.java    From UltimateCore with MIT License 6 votes vote down vote up
@Override
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
    checkIfPlayer(sender);
    checkPermission(sender, ItemPermissions.UC_ITEM_ITEMNAME_BASE);
    Player p = (Player) sender;

    if (p.getItemInHand(HandTypes.MAIN_HAND).getType().equals(ItemTypes.NONE)) {
        throw new ErrorMessageException(Messages.getFormatted(p, "item.noiteminhand"));
    }
    ItemStack stack = p.getItemInHand(HandTypes.MAIN_HAND);

    Text name = Messages.toText(args.<String>getOne("name").get());
    stack.offer(Keys.DISPLAY_NAME, name);
    p.setItemInHand(HandTypes.MAIN_HAND, stack);
    Messages.send(sender, "item.command.itemname.success", "%arg%", name);
    return CommandResult.success();
}
 
Example 7
Source File: ItemcanplaceonCommand.java    From UltimateCore with MIT License 6 votes vote down vote up
@Override
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
    checkIfPlayer(sender);
    checkPermission(sender, ItemPermissions.UC_ITEM_ITEMCANPLACEON_BASE);
    Player p = (Player) sender;

    if (p.getItemInHand(HandTypes.MAIN_HAND).getType().equals(ItemTypes.NONE)) {
        throw new ErrorMessageException(Messages.getFormatted(p, "item.noiteminhand"));
    }
    ItemStack stack = p.getItemInHand(HandTypes.MAIN_HAND);
    Set<BlockType> types = new HashSet<>(args.<BlockType>getAll("blocktypes"));

    stack.offer(Keys.PLACEABLE_BLOCKS, types);
    p.setItemInHand(HandTypes.MAIN_HAND, stack);
    Text items = Text.joinWith(Text.of(", "), types.stream().map(type -> Text.of(type.getName())).collect(Collectors.toList()));
    Messages.send(sender, "item.command.itemcanplaceon.success", "%arg%", items);
    return CommandResult.success();
}
 
Example 8
Source File: ItemdurabilityCommand.java    From UltimateCore with MIT License 6 votes vote down vote up
@Override
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
    checkIfPlayer(sender);
    checkPermission(sender, ItemPermissions.UC_ITEM_ITEMDURABILITY_BASE);
    Player p = (Player) sender;

    if (p.getItemInHand(HandTypes.MAIN_HAND).getType().equals(ItemTypes.NONE)) {
        throw new ErrorMessageException(Messages.getFormatted(p, "item.noiteminhand"));
    }
    ItemStack stack = p.getItemInHand(HandTypes.MAIN_HAND);
    int durability = args.<Integer>getOne("durability").get();

    if (!stack.supports(DurabilityData.class)) {
        throw new ErrorMessageException(Messages.getFormatted(sender, "item.command.itemdurability.notsupported"));
    }

    if (durability < stack.get(DurabilityData.class).get().durability().getMinValue() || durability > stack.get(DurabilityData.class).get().durability().getMaxValue()) {
        throw new ErrorMessageException(Messages.getFormatted(sender, "item.numberinvalid", "%number%", durability));
    }

    stack.offer(Keys.ITEM_DURABILITY, durability);
    p.setItemInHand(HandTypes.MAIN_HAND, stack);
    Messages.send(sender, "item.command.itemdurability.success", "%arg%", durability);
    return CommandResult.success();
}
 
Example 9
Source File: ItemcanbreakCommand.java    From UltimateCore with MIT License 6 votes vote down vote up
@Override
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
    checkIfPlayer(sender);
    checkPermission(sender, ItemPermissions.UC_ITEM_ITEMCANBREAK_BASE);
    Player p = (Player) sender;

    if (p.getItemInHand(HandTypes.MAIN_HAND).getType().equals(ItemTypes.NONE)) {
        throw new ErrorMessageException(Messages.getFormatted(p, "item.noiteminhand"));
    }
    ItemStack stack = p.getItemInHand(HandTypes.MAIN_HAND);
    Set<BlockType> types = new HashSet<>(args.<BlockType>getAll("blocktypes"));

    stack.offer(Keys.BREAKABLE_BLOCK_TYPES, types);
    p.setItemInHand(HandTypes.MAIN_HAND, stack);
    Text items = Text.joinWith(Text.of(", "), types.stream().map(type -> Text.of(type.getName())).collect(Collectors.toList()));
    Messages.send(sender, "item.command.itemcanbreak.success", "%arg%", items);
    return CommandResult.success();
}
 
Example 10
Source File: UCVHelper8.java    From UltimateChat with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ItemStack getItemInHand(Player sender) {
    if (!sender.getItemInHand(HandTypes.MAIN_HAND).isEmpty()) {
        return sender.getItemInHand(HandTypes.MAIN_HAND);
    } else if (!sender.getItemInHand(HandTypes.OFF_HAND).isEmpty()) {
        return sender.getItemInHand(HandTypes.OFF_HAND);
    }
    return ItemStack.empty();
}
 
Example 11
Source File: MoreCommand.java    From UltimateCore with MIT License 5 votes vote down vote up
@Override
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
    checkIfPlayer(sender);
    checkPermission(sender, ItemPermissions.UC_ITEM_MORE_BASE);
    Player p = (Player) sender;
    if (p.getItemInHand(HandTypes.MAIN_HAND).getType().equals(ItemTypes.NONE)) {
        throw new ErrorMessageException(Messages.getFormatted(p, "item.noiteminhand"));
    }
    ItemStack stack = p.getItemInHand(HandTypes.MAIN_HAND);
    stack.setQuantity(stack.getMaxStackQuantity());
    p.setItemInHand(HandTypes.MAIN_HAND, stack);
    Messages.send(p, "item.command.more.success", "%amount%", stack.getMaxStackQuantity());
    return CommandResult.success();
}
 
Example 12
Source File: ItemenchantCommand.java    From UltimateCore with MIT License 5 votes vote down vote up
@Override
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
    checkIfPlayer(sender);
    checkPermission(sender, ItemPermissions.UC_ITEM_ITEMENCHANT_BASE);
    Player p = (Player) sender;

    if (p.getItemInHand(HandTypes.MAIN_HAND).getType().equals(ItemTypes.NONE)) {
        throw new ErrorMessageException(Messages.getFormatted(p, "item.noiteminhand"));
    }
    ItemStack stack = p.getItemInHand(HandTypes.MAIN_HAND);

    EnchantmentType ench = args.<EnchantmentType>getOne("enchantment").get();
    int level = args.hasAny("level") ? args.<Integer>getOne("level").get() : 1;

    if (level > ItemPermissions.UC_ITEM_ITEMENCHANT_MAXLEVEL.getIntFor(p)) {
        throw Messages.error(p, "item.command.itemenchant.maxlevel", "%max%", ItemPermissions.UC_ITEM_ITEMENCHANT_MAXLEVEL.getIntFor(p));
    }

    List<Enchantment> enchs = stack.get(Keys.ITEM_ENCHANTMENTS).orElse(new ArrayList<>());
    if (level > 0) {
        enchs.add(Enchantment.builder().type(ench).level(level).build());
        stack.offer(Keys.ITEM_ENCHANTMENTS, enchs);
        p.setItemInHand(HandTypes.MAIN_HAND, stack);
        Messages.send(sender, "item.command.itemenchant.success", "%enchant%", ench.getTranslation().get(), "%level%", level);
        return CommandResult.success();
    } else {
        enchs = enchs.stream().filter(e -> !e.getType().equals(ench)).collect(Collectors.toList());
        stack.offer(Keys.ITEM_ENCHANTMENTS, enchs);
        p.setItemInHand(HandTypes.MAIN_HAND, stack);
        Messages.send(sender, "item.command.itemenchant.success2", "%enchant%", ench.getTranslation().get(), "%level%", level);
        return CommandResult.success();
    }
}
 
Example 13
Source File: VersionHelper8.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ItemStack getItemOffHand(Player player) {
    if (!player.getItemInHand(HandTypes.OFF_HAND).isEmpty())
        return player.getItemInHand(HandTypes.OFF_HAND);

    return ItemStack.empty();
}
 
Example 14
Source File: VersionHelper8.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ItemStack getItemMainHand(Player player) {
    if (!player.getItemInHand(HandTypes.MAIN_HAND).isEmpty())
        return player.getItemInHand(HandTypes.MAIN_HAND);

    return ItemStack.empty();
}
 
Example 15
Source File: HatExecutor.java    From EssentialCmds with MIT License 5 votes vote down vote up
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException
{
	if (src instanceof Player)
	{
		Player player = (Player) src;
		Optional<ItemStack> itemInHand = player.getItemInHand(HandTypes.MAIN_HAND);

		if (itemInHand.isPresent())
		{
			if (itemInHand.get().getQuantity() > 1)
			{
				ItemStack stack = itemInHand.get();
				stack.setQuantity(itemInHand.get().getQuantity() - 1);
				player.setItemInHand(HandTypes.MAIN_HAND, stack);
				stack.setQuantity(1);
				player.setHelmet(stack);
			}
			else
			{
				player.setHelmet(itemInHand.get());
				player.setItemInHand(HandTypes.MAIN_HAND, null);
			}
		}
		else
		{
			player.sendMessage(Text.of("No item selected in hotbar."));
		}
	}

	return CommandResult.success();
}
 
Example 16
Source File: GoldenAxeListener.java    From Nations with MIT License 5 votes vote down vote up
@Listener
public void onPlayerLeftClick(InteractBlockEvent.Primary.MainHand event, @First Player player)
{
	if (ConfigHandler.getNode("others", "enableGoldenAxe").getBoolean(true) == false)
	{
		return ;
	}
	Optional<ItemStack> optItem = player.getItemInHand(HandTypes.MAIN_HAND);
	if (!optItem.isPresent())
	{
		return;
	}
	if (optItem.get().getItem().equals(ItemTypes.GOLDEN_AXE))
	{
		event.setCancelled(true);
		Optional<Location<World>> optLoc = event.getTargetBlock().getLocation();
		if (!optLoc.isPresent())
		{
			return;
		}
		Point firstPoint = new Point(optLoc.get().getExtent(), optLoc.get().getBlockX(), optLoc.get().getBlockZ());
		DataHandler.setFirstPoint(player.getUniqueId(), firstPoint);
		Point secondPoint = DataHandler.getSecondPoint(player.getUniqueId());
		if (secondPoint != null && !secondPoint.getWorld().equals(firstPoint.getWorld()))
		{
			secondPoint = null;
			DataHandler.setSecondPoint(player.getUniqueId(), secondPoint);
		}
		
		String coord = firstPoint.getX() + " " + firstPoint.getY() + ")" + ((secondPoint != null) ? " (" + new Rect(secondPoint, firstPoint).size() + ")" : "");
		player.sendMessage(Text.of(TextColors.AQUA, LanguageHandler.AXE_FIRST.replaceAll("\\{COORD\\}", coord)));
	}
}
 
Example 17
Source File: GoldenAxeListener.java    From Nations with MIT License 5 votes vote down vote up
@Listener
public void onPlayerRightClick(InteractBlockEvent.Secondary.MainHand event, @First Player player)
{
	if (ConfigHandler.getNode("others", "enableGoldenAxe").getBoolean(true) == false)
	{
		return ;
	}
	Optional<ItemStack> optItem = player.getItemInHand(HandTypes.MAIN_HAND);
	if (!optItem.isPresent())
	{
		return;
	}
	if (optItem.get().getItem().equals(ItemTypes.GOLDEN_AXE))
	{
		event.setCancelled(true);
		Optional<Location<World>> optLoc = event.getTargetBlock().getLocation();
		if (!optLoc.isPresent())
		{
			return;
		}
		Point secondPoint = new Point(optLoc.get().getExtent(), optLoc.get().getBlockX(), optLoc.get().getBlockZ());
		DataHandler.setSecondPoint(player.getUniqueId(), secondPoint);
		Point firstPoint = DataHandler.getFirstPoint(player.getUniqueId());
		if (firstPoint != null && !firstPoint.getWorld().equals(secondPoint.getWorld()))
		{
			firstPoint = null;
			DataHandler.setFirstPoint(player.getUniqueId(), firstPoint);
		}
		
		String coord = secondPoint.getX() + " " + secondPoint.getY() + ")" + ((firstPoint != null) ? " (" + new Rect(firstPoint, secondPoint).size() + ")" : "");
		player.sendMessage(Text.of(TextColors.AQUA, LanguageHandler.AXE_SECOND.replaceAll("\\{COORD\\}", coord)));
	}
}
 
Example 18
Source File: SpongeDeathListener.java    From Plan with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Runnable handlePlayerKill(long time, UUID victimUUID, Player killer) {

        Optional<ItemStack> inMainHand = killer.getItemInHand(HandTypes.MAIN_HAND);
        ItemStack inHand = inMainHand.orElse(killer.getItemInHand(HandTypes.OFF_HAND).orElse(ItemStack.empty()));
        ItemType type = inHand.isEmpty() ? ItemTypes.AIR : inHand.getType();

        return victimUUID != null
                ? new PlayerKillProcessor(killer.getUniqueId(), time, victimUUID, new ItemNameFormatter().apply(type.getName()))
                : new MobKillProcessor(killer.getUniqueId());
    }
 
Example 19
Source File: DeathmessageListener.java    From UltimateCore with MIT License 4 votes vote down vote up
@Listener
public void onDeath(DestructEntityEvent event) {
    if (!(event.getTargetEntity() instanceof Player)) {
        return;
    }
    Player p = (Player) event.getTargetEntity();

    //Get the translation, because for some reason sponge hides it in a lot of children
    Text orgmessage = event.getOriginalMessage();
    while (!(orgmessage instanceof TranslatableText) && !orgmessage.getChildren().isEmpty()) {
        for (Text child : orgmessage.getChildren()) {
            if (child instanceof TranslatableText) {
                break;
            }
        }
        orgmessage = orgmessage.getChildren().get(0); //Safe because of while loop
    }

    if (orgmessage instanceof TranslatableText) {
        //Id
        TranslatableText org = (TranslatableText) orgmessage;
        String id_mc = org.getTranslation().getId();
        if (!id_mc.startsWith("death.")) {
            return;
        }
        String id_uc = id_mc.toLowerCase().replaceFirst("death\\.", "deathmessage.message.");

        //Item
        ItemStack stack = p.getItemInHand(HandTypes.MAIN_HAND);
        Text item = stack.get(Keys.DISPLAY_NAME).orElse(Text.of(stack.getTranslation().get()));

        //Final message //TODO hover
        Text message = Messages.getFormatted(id_uc, "%player%", VariableUtil.getNameSource(p), "%item%", item);

        //Custom args
        int i = 1;
        for (Object ob : org.getArguments()) {
            message = TextUtil.replace(message, "%arg" + i + "%", ob instanceof Text ? ((Text) ob) : Text.of(ob.toString()));
            i++;
        }

        event.setMessage(message);
    }
}