Many items are given instead of one

Discussion in 'Spigot Plugin Help' started by grenka12, Jul 16, 2021.

  1. One item must be awarded after completing the achievement. Depending on the achievement - they are given (1-10)+ items.
    upload_2021-7-16_11-21-33.png

    Code (Java):
    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.");
        }
    }

     
    check the " @EventHandler public void PlayerAdvancementDone(PlayerAdvancementDoneEvent event)"
     
  2. You never checked if it's the proper advancement you want - each advancement you see on screen can fire dozens of other hidden advancements (advancement isn't just what shows on screen and in the achievement screen). Check which advancement it is and make sure it's the one you want, THEN do your thing.
     
  3. hmm, I get it. But I did not understand how to use only those that are visible in the game.
     
  4. Here's what I'd do: in the "PlayerAdvancementDone" function that listens to that event print out (to console or broadcast it in-game) the event's criteria (plural, it's a collection). This way you can see what criteria (plural) are being used, and you can choose the proper one (according to their name), and change the plugin afterwards to give the item ONLY when that specific criterion is met.