1.16.5 My plugin isn't working.

Discussion in 'Spigot Plugin Development' started by MrDefault, Feb 9, 2021.

  1. Hello everyone, I've tried to make a plugin Stamina system but It doesn't work. Please help.
    Edit: I am not sure about the method that I used, I kept the player data in hashmap and decrease them from it
    Code (Text):
    HashMap<Player,Float> enerny = new HashMap<>();
    Here is my code:
    Code (Java):
    package com.Stamina;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerToggleSprintEvent;
    import org.bukkit.scheduler.BukkitTask;
    import java.util.concurrent.TimeUnit;

    public class EnergyListener implements Listener {

        private final Main main;
        private int task;


        public EnergyListener(Main main) { this.main = main; }

        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            for (Player p : Bukkit.getOnlinePlayers()) {
                if(p.isOnline()) {
                    if(!main.enerny.containsKey(e.getPlayer())) {
                        main.enerny.put(e.getPlayer(),20.0F);
                    }
                }
                    if(main.enerny.containsValue(20.0F) && main.enerny.containsKey(e.getPlayer())) {
                        p.sendMessage("Work!");
                }
                    if(main.enerny.containsValue(0.0F) && main.enerny.containsKey(e.getPlayer())) {
                        p.sendMessage("ran out of stamina");
                }
            }
        }

        @EventHandler
        public void isSprints(PlayerToggleSprintEvent e) {
            Bukkit.getScheduler().runTaskTimerAsynchronously(main, new Runnable() {
                @Override
                public void run() {
                    while (e.isSprinting()) {
                        main.enerny.remove(null,2F);
                        if(main.enerny.containsValue(0)){
                            break;
                        }
                    }
                    if(main.enerny.containsValue(0.0F)) {
                        Player p = e.getPlayer();
                        p.sendMessage("ran out of stamina");
                    }
                }
            },0L ,100L);
        }
        @EventHandler
        public void rest(Player p) {
            task = Bukkit.getScheduler().scheduleAsyncRepeatingTask(main, new Runnable() {
                @Override
                public void run() {
                    for(Player p : Bukkit.getOnlinePlayers()) {
                        while(p.isOnGround()) {
                            main.enerny.put(p.getPlayer(),2.0F);
                        }
                        if(main.enerny.containsValue(20.0F)) {
                            Bukkit.getScheduler().cancelTask(task);
                            break;
                        }
                    }
                }
            }, 0L ,100L);
        }
        @EventHandler
        public void cancelrest(PlayerToggleSprintEvent e) {
            if(e.getPlayer().isSprinting()) {
                Bukkit.getScheduler().cancelTask(task);
            }
        }
    }
     
     
    #1 MrDefault, Feb 9, 2021
    Last edited: Feb 9, 2021
  2. You need to register your listeners :)
     
  3. I've already registered it in my main class.
     
  4. Then what exactly is not working?
     
  5. I am sorry for not explaining this clearly. the plugin is runnable but It has a problem with my code.
     
  6. Okay and what is the problem exactly?
     
  7. I tried to decrease players' stamina while they were running and it doesn't work.