Many items are given instead of one

Discussion in 'Plugin Development' started by GRENKA, Jul 16, 2021.

  1. Offline

    GRENKA

    One item must be awarded after completing the achievement. Depending on the achievement - they are given (1-10)+ items.
    Code:
    package plugin.grenka.plugin;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    
    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.*;
    import org.bukkit.inventory.ItemStack;
    
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Timer;
    
    
    public final class Plugin extends JavaPlugin implements Listener {
        boolean ibb = true;
    
    
    
        @Override
        public void onEnable() {
            // Plugin startup logic
            System.out.println("The plugin is starting up.");
            getServer().getPluginManager().registerEvents(this, this);
    
    
        }
        @EventHandler
        public void onPlayer(PlayerInteractEvent event) throws InterruptedException {
            Player player = event.getPlayer();
            Action action = event.getAction();
            Timer timer = new Timer();
            if (action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK)) {
                if (player.getItemInHand().getType() == Material.TRIDENT) {
                    player.getInventory().getItemInMainHand().setAmount(player.getInventory().getItemInMainHand().getAmount() - 1);
                    player.updateInventory();
                    player.setGameMode(GameMode.CREATIVE);
                    Bukkit.getScheduler().runTaskLater(this, () -> {
                        player.setGameMode(GameMode.SURVIVAL);
                    }, 40L);
    
    
    
                }
            }
        }
    
    
        @EventHandler
        public void PlayerAdvancementDone(PlayerAdvancementDoneEvent event){
            Player player = event.getPlayer();
            ItemStack item = new ItemStack(Material.TRIDENT);
            ItemMeta im = item.getItemMeta();
    
            im.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&a&lCreative"));
            List<String> lore = new ArrayList<String>();
            lore.add("Bla-bla-bla");
            im.setLore(lore);
            item.setItemMeta(im);
    
            player.getInventory().addItem(item);
            player.sendMessage(ChatColor.GREEN + "Give.");
        }
    }
    
    
     

    Attached Files:

  2. Offline

    Kars

    Looks like that event gets called multiple times.
    You can solve this by having a collection of player ids and adding the player to it when the event fires and give them the diamond, but only if their id is not on the list already.
     
  3. Offline

    Strahan

    Also
    Code:
    System.out.println("The plugin is starting up.");
    this is unnecessary log clutter. The server already puts plugin initialization messaging in the log. You really should also be using the logger, not println.
     

Share This Page