Plugin keeps crashing my server

Discussion in 'Plugin Development' started by puppet172X, May 24, 2020.

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

    puppet172X

    Hi,
    Everytime i run my plugin is crashes my server. The goal of the program is every 2 minutes it swaps the players locations. The only idea i have why its crashing is that the timer is somehow messed up, but i can't seem to fix it.

    Main:
    Code:
    package me.puppet172.death;
    
    import org.bukkit.plugin.java.JavaPlugin;
    import me.puppet172.death.commands.*;
    
    public class Main extends JavaPlugin
    {
      
        @Override
        public void onEnable()
        {
            new DeathCommand(this);
        }
    }
    
    Command File:
    Code:
    package me.puppet172.death.commands;
    
    import org.bukkit.command.CommandExecutor;
    import me.puppet172.death.Main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class DeathCommand implements CommandExecutor
    {
    @SuppressWarnings("unused")
    private Main plugin;
    
    private int pX= 0;
    private int pY = 0;
    private int pZ = 0;
    
    private int p1X= 0;
    private int p1Y = 0;
    private int p1Z = 0;
    private Player p;
    private Player p1;
    private int secondspassed= 0;
    private Timer time = new Timer();
    private TimerTask task = new TimerTask()
    //double check timer is set up correctly
    {
    
        @Override
        public void run()
        {
        secondspassed++;  
        Bukkit.broadcastMessage("Time: "+ secondspassed);
          
        }
      
    };
    public void start()
    {
        time.scheduleAtFixedRate(task, 1000, 1000);
    }
      
        public DeathCommand(Main plugin)
        {
            this.plugin = plugin;
            plugin.getCommand("death").setExecutor(this);
        }
        @Override
        public boolean onCommand(CommandSender send, Command cmd,String label, String[] args) {
        {
      
            send.sendMessage("The game has Started");
            p = (Player) send;
            p1= Bukkit.getPlayer(args[0]);
            start();
            while((p.getHealth()>0) && (p1.getHealth()>0))
            {
              
              
                if(secondspassed>110)
                {
                    int time = 10;
                    Bukkit.broadcastMessage("Time left: "+String.valueOf(time));
                    time--;
                }
          
                while(secondspassed >= 120)
                {
                  
                  
                  
                    secondspassed = 0;
                    location();
                    p.teleport(new Location(p1.getWorld(),p1X,p1Y,p1Z));
                    p1.teleport(new Location(p.getWorld(),pX,pY,pZ));
              
                }
            }
            if(p.getHealth()==0)
            {
                p.sendMessage("You Lost");
                p1.sendMessage("You Won");
            }else
            {
                p.sendMessage("You Won");
                p1.sendMessage("You Lost");
              
            }
          
            return true;
        }
      
    }
        public void location()
        {
            pX = p.getLocation().getBlockX();
            pY = p.getLocation().getBlockY();
            pZ = p.getLocation().getBlockZ();
          
            p1X = p1.getLocation().getBlockX();
            p1Y = p1.getLocation().getBlockY();
            p1Z = p1.getLocation().getBlockZ();
          
        }
    }
    
    plugin.yml:
    Code:
    name: DeathS
    version: 1.15.2
    main: me.puppet172.death.Main
    
    commands:
      death:
        aliasis: [death]
        description: DeathSwap Plugin
    
     
  2. Online

    timtower Administrator Administrator Moderator

    @puppet172X Don't use a while loop like that in your code.
    It will freeze the server.
    Use a BukkitRunnable instead.
     
Thread Status:
Not open for further replies.

Share This Page