Remove Item With meta 1.16.4

Discussion in 'Plugin Development' started by Julian., Feb 8, 2021.

Thread Status:
Not open for further replies.
  1. Offline

    Julian.

    Hi i wan't to do a plugin when you click on a item (with an meta) he get remove i try a lot of code but its never work

    Every time i click with the sword my local server crash but the sword was delete BUT the server crash

    Here you can see my actual code :

    *How to make this inside a text : code ? |*
    \/

    Code:
    package fr.julian.mob.events;
    
    
    
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class OnClick implements Listener {
    
        @SuppressWarnings({ "deprecation"})
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e){
            Player player = e.getPlayer();
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
                if(player.getItemInHand().hasItemMeta() && player.getItemInHand().getItemMeta().getDisplayName().equals("§cLoutre Magique")) {
                    player.sendMessage(ChatColor.GOLD + "Vous avez utulisé votre : " + ChatColor.RED + e.getItem().getType().toString().toUpperCase());
                
                      ItemStack sword = new ItemStack(Material.DIAMOND_SWORD, 1);
                    ItemMeta swordM = sword.getItemMeta();
                    swordM.addEnchant(Enchantment.DAMAGE_ALL, 5, true);
                    swordM.addEnchant(Enchantment.VANISHING_CURSE, 1, true);
                    swordM.setDisplayName("§cLoutre Magique");
                    sword.setItemMeta(swordM);
                    
                    player.getInventory().removeItem(new ItemStack[]{ new ItemStack  (sword)  }  );
                    player.updateInventory();
                }
        
            }
        }
    }
     
    Last edited by a moderator: Feb 8, 2021
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    Julian.

    Last edited by a moderator: Feb 8, 2021
  4. Online

    timtower Administrator Administrator Moderator

    @Julian. Why not do player.getInventory().removeItem(player.getItemInHand()) ?
     
  5. Offline

    Julian.

    https://pastebin.com/p5ma7xHm


    player.getInventory().removeItem(player.getItemInHand());
    i try this but don't work
     
  6. Online

    timtower Administrator Administrator Moderator

    @Julian. Never seen this, can't help you with this.
     
  7. Offline

    Newdel

    Try cancelling the event after removing the sword

    If this is a specific sword, create a method getSpecificSword() and compare the player's sword with the method's sword. Makes it easier to maintain your code. But as @timtower said, try player.getInventory().removeItem(player.getItemInHand()); instead. It should work
     
  8. Offline

    Julian.

    How i can do this method i am not a good coder i restart 2 week ago ^^'
     
  9. Offline

    Newdel

    Just create a method that creates and returns the ItemStack
     
  10. Offline

    Julian.

    i found how to fix it
    before :
    if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
    after :
    if(e.getAction() != Action.RIGHT_CLICK_AIR && e.getAction() != Action.RIGHT_CLICK_BLOCK){

    its just this
    thx for the help !

    How to make the post solved pls ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 8, 2021
Thread Status:
Not open for further replies.

Share This Page