Solved Adding a potion effect to a player after respawning

Discussion in 'Plugin Development' started by stivi313131, Jul 24, 2016.

Thread Status:
Not open for further replies.
  1. My Problems:
    I am trying to make a plugin where the world border decreases after dying and the died player gets set into Spectator mode and gets some effects. It turned out that I need a tiny delay.
    After googling about delaying something in Bukkit I found this thing called "Scheduler". I tried using it and it showed an error that I can't fix.

    In Eclipse is Looks like that:
    Code:
                        
                        Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    
                            @Override
                            public void run() {
                                //Testing if Scheduler is working
                                Bukkit.getServer().broadcastMessage(ChatColor.RED+"Scheduler is working! YAY! :D");
                                //Trying to add a PotionEffect
                                player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 999, 255));
                            }
               
                        }, 20L);
    And "scheduleSyncDelayedTask" in the first (line 36 in whole class) line is underlined with the error:

    After I tested it anyways on a test server there was this error after clicking the respawn button:

    My Questions:
    How can I fix this error?
    or
    Is there another way to give a player some effects after respawning?


    My Code:
    Code:
    package de.Stephan.MiniVaro;
    
    //importing stuff
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    
    
    public class Eventlistener implements Listener {
       
       
        public Eventlistener (JavaPlugin plugin){
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
        //Adding a PlayerDeath checker
        @EventHandler
        public void onPlayerDeath (PlayerDeathEvent tod){
            //Checking if Round was started
            if(Variables.Round==1){
                //Checking if there are more than one player online
                if(Variables.OnlinePlayers>1){
                    //Checking if died Player was in SurvivalMode
                    if(tod.getEntity().getGameMode()==GameMode.SURVIVAL){
                        //Decreasing WorldBorder size
                        Variables.b.setSize(Bukkit.getWorld("world").getWorldBorder().getSize()-10,10);
                        //Telling everyone that the WorldBorder is decreasing size
                        Bukkit.getServer().broadcastMessage(ChatColor.AQUA+"The worldborder is shrinking 10 blocks.");
                        //setting GameMode of died player to SPECTATOR
                        tod.getEntity().setGameMode(GameMode.SPECTATOR);
                    }
                }
            }
        }
       
        //Adding a PlayerRespwn checker
        @EventHandler
        public void onPlayerSpawn(PlayerRespawnEvent spawn){
            //Testing if it works
            Bukkit.getServer().broadcastMessage("onPlayerSpawn is working! YAY :D");
            Player player = spawn.getPlayer();
            //Checking if Round was started
            if(Variables.Round==1){
                //Checking if there are more than one player online
                if(Variables.OnlinePlayers>1){
                    //Checking if respawned Player is in SECTATOR mode
                    if(player.getGameMode()==GameMode.SPECTATOR){
                        //Trying to add a PotionEffect
                        player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 999, 255));
                       
                        //Trying to give the PotionEffect 5sek later
                        Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    
                            @Override
                            public void run() {
                                //Testing if Scheduler is working
                                Bukkit.getServer().broadcastMessage(ChatColor.RED+"Scheduler is working! YAY! :D");
                                //Trying to add a PotionEffect
                                player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 999, 255));
                            }
               
                        }, 20L);
                       
                    }
                }
            }
        }
       
    }
     
  2. @stivi313131
    The problem is that you put in your eventlistener class as a Plugin, but you need to put in your Main class (the one which extends javaplugin).
     
  3. scheduleSyncDelayedTask: the first argument needs to be the instance of your main class (extending JavaPlugin)
     
  4. OK, now the Schedule error is gone thanks for that.
    But now I got many token errors in Eclipse and server:
    (line 36
    line 45: public void onPlayerSpawn(PlayerRespawnEvent spawn){
    line 65
    line 67: }, 20L);
    Code:
    /* TODO:
         + Custom Spawns
         + Schematics
         + Teams
         + Countdown
         + optimize plugin.yml
         + /MiniVaro-info
    */
    package de.Stephan.MiniVaro;
    //importing stuff
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    
    
    public class MiniVaro extends JavaPlugin{
      
      
      
        @Override
        public void onEnable() {
            super.onEnable();
          
            new Eventlistener(this);
            //set WorldBordersize to 100
            Variables.b.setSize(100);
            }
      
        //Trying to give the PotionEffect 5sek later
        Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    
            @Override
            public void run() {
                //Adding a PlayerRespwn checker
                @EventHandler
                public void onPlayerSpawn(PlayerRespawnEvent spawn){
                    //Testing if it works
                    Bukkit.getServer().broadcastMessage("onPlayerSpawn is working! YAY :D");
                    Player player = spawn.getPlayer();
                    //Checking if Round was started
                    if(Variables.Round==1){
                        //Checking if there are more than one player online
                        if(Variables.OnlinePlayers>1){
                            //Checking if respawned Player is in SECTATOR mode
                            if(player.getGameMode()==GameMode.SPECTATOR){
                                //Testing if Scheduler is working
                                Bukkit.getServer().broadcastMessage(ChatColor.RED+"Scheduler is working! YAY! :D");
                                //Trying to add a PotionEffect
                                player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 999, 255));
                              
                            }
                        }
                    }
                }
    
            }
    
        }, 20L);
      
        //Commands
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
          
            //Command /MiniVaro
            if(command.getName().equalsIgnoreCase("MiniVaro")){
                    //send some text to the sender of the Command /MiniVaro
                    sender.sendMessage(ChatColor.YELLOW+"Usage:");
                    sender.sendMessage(ChatColor.YELLOW+"Enter "+ChatColor.RED+"\"/MiniVaro-start\""+ChatColor.YELLOW+" to start a new Round." );
                    sender.sendMessage(ChatColor.YELLOW+"Enter "+ChatColor.RED+"\"/MiniVaro-stop\""+ChatColor.YELLOW+" to stop a Round." );
                    sender.sendMessage(ChatColor.YELLOW+"Enter "+ChatColor.RED+"\"/MiniVaro-info\""+ChatColor.YELLOW+" to get info about the MiniVaro plugin." );
            }
          
            //Command /MiniVaro-start
            if(command.getName().equalsIgnoreCase("MiniVaro-start")){
                //start a Round by setting Round to 1
                Variables.Round=1;;
                //Checking if setting Round to 1 has worked
                if(Variables.Round==1){
                    sender.sendMessage("Runde gestartet");
                }
            }
          
            //Command /MiniVaro-stop
            if(command.getName().equalsIgnoreCase("MiniVaro-stop")){
                //Stopping a Round by setting Round to 0
                Variables.Round=0;
                if(Variables.Round==0){
                    //Checking if setting Round to 0 has worked
                    sender.sendMessage("Runde gestoppt");
                }
            }return false;
          
          
          
        }
    
      
      
    }
     
  5. @stivi313131
    You cannot create a new method inside another method! What you need to do is make the eventhandler, and inside that, call your delayed task.
     
  6. Thanks.
    I don't know why I changed that *facepalm*
     
Thread Status:
Not open for further replies.

Share This Page