UHC problem.

Discussion in 'Plugin Development' started by BlueBearPvP, May 17, 2016.

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

    BlueBearPvP

    Hey Guys,

    So recently I been creating a UHC plugin for my server. I ran into a problem that I dont know how to fix and I try many solutions. So my problem is: For some reason when I have the correct amount of players to start the game, it wont start unless I type "reload" in console. That also goes same with when I type "/uhc startgame".

    Here is my code:

    BearUHCCore:

    Code:
    package com.bluebearpvp.core;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Server;
    import org.bukkit.block.Biome;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.bluebearpvp.core.Command.GameCommand;
    import com.bluebearpvp.core.Timer.GameTimer;
    import com.bluebearpvp.core.Utils.GameState;
    
    public class BearUHCCore extends JavaPlugin {
       
        private static GameState state;
        private static boolean invincible;
        private static Thread gameTimer;
        private static boolean started;
       
        public static Server server;
       
        public static int forcefieldMin;
        public static int forcefieldMax;
       
        public void onEnable() {
           
            started = false;
            forcefieldMin = -500;
            forcefieldMax = 500;
           
            BearUHCCore.setServer(getServer());
            getCommand("uhc").setExecutor(new GameCommand());
           
            BearUHCCore.setState(GameState.WAITING);
            if (BearUHCCore.getState() == GameState.IN_PROGRESS) {
                System.out.println("IN PROGRESS");
            } else if (BearUHCCore.getState() == GameState.WAITING){
                System.out.println("WAITING");
            }
            GameTimer game = new GameTimer(server);
            gameTimer = new Thread(game);
            gameTimer.start();
           
        }
       
        public static void startGame() { //Problem: Only detecting when restarting. Use runnable.
           
            BearUHCCore.started = true;
            BearUHCCore.setState(GameState.STARTING);
            Bukkit.broadcastMessage(ChatColor.BLUE + "Bear" + ChatColor.RED + "UHC " + ChatColor.GRAY + "> " + ChatColor.GREEN + "This game has been forced started. UHC is now beginning.. " );
           
        }
       
        public static Location generateRandomLocation(int min, int max, Player player, boolean ocean) {
           
            double newX = min + (int)(Math.random() * (max - min + 1));
            double newZ = min + (int)(Math.random() * (max - min + 1));
            double newY = player.getWorld().getHighestBlockAt((int)newX, (int)newZ).getY() + 0.5;
           
            Location location = new Location(player.getWorld(), newX, newY, newZ);
            player.teleport(location);
           
            if(!ocean) {
               
                while(player.getWorld().getBiome((int)newX, (int)newY) == Biome.OCEAN) {
                    newX = min + (int)(Math.random() * (max - min + 1));
                    newZ = min + (int)(Math.random() * (max - min + 1));
                    newY = player.getWorld().getHighestBlockAt((int)newX, (int)newZ).getY() + 0.5;
                   
                    location = new Location(player.getWorld(), newX, newY, newZ);
                    player.teleport(location);
                }
            }
           
            return location;
        }
    
       
        public static void setState(GameState state) {
            BearUHCCore.state = state;
           
        }
       
        public static boolean hasStarted() {
            return started;
           
        }
       
        public static void setInvincible(boolean i) {
            BearUHCCore.invincible = i;
           
        }
       
        public static GameState getState() {
            return state;
           
        }
       
        public static boolean isInvincible() {
            return invincible;
           
        }
       
        public static void setServer(Server newServer) {
            server = newServer;
           
        }
       
    }
    GameTimer:

    Code:
    package com.bluebearpvp.core.Timer;
    
    import java.util.concurrent.TimeUnit;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Server;
    import org.bukkit.entity.Firework;
    import org.bukkit.entity.Player;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import com.bluebearpvp.core.BearUHCCore;
    import com.bluebearpvp.core.Utils.GameState;
    
    
    public class GameTimer implements Runnable {
       
            private Server server;
            public GameTimer(Server srv) {
                server = srv;
            }
           
            @Override
            public void run() {
                try {
                   
                if (BearUHCCore.hasStarted() == false && Bukkit.getServer().getOnlinePlayers().size() >= 2 || BearUHCCore.getState() == GameState.STARTING) {
                                                                                               
                    int max = 450;
                    int min = -450;
                   
                   for (Player player : server.getOnlinePlayers()) {
                      
                        player.sendMessage(ChatColor.BLUE + "Bear" + ChatColor.RED + "UHC " + ChatColor.GRAY + "> " + ChatColor.GREEN + "Teleporting you in " + ChatColor.RED + "5 secs!" );   
                        TimeUnit.SECONDS.sleep(5);
                       
                        player.teleport(BearUHCCore.generateRandomLocation(min, max, player, true));
                   
                    Bukkit.broadcastMessage(ChatColor.RED + "90 Seconds until the match starts.");
                    TimeUnit.SECONDS.sleep(70);
                    Bukkit.broadcastMessage(ChatColor.RED + "" + "Your health and hunger has been reset.");
                    TimeUnit.SECONDS.sleep(5);
                   
                    Bukkit.broadcastMessage(ChatColor.RED + "You have 15 seconds until the game starts!");
                    TimeUnit.SECONDS.sleep(5);
                   
                    Bukkit.broadcastMessage(ChatColor.RED + "You have 10 seconds until the game starts!");
                    TimeUnit.SECONDS.sleep(5);
                   
                    Bukkit.broadcastMessage(ChatColor.RED + "You have 5 seconds until the game starts!");
                    TimeUnit.SECONDS.sleep(1);
                   
                    Bukkit.broadcastMessage(ChatColor.RED + "You have 4 seconds until the game starts!");
                    TimeUnit.SECONDS.sleep(1);
                   
                    Bukkit.broadcastMessage(ChatColor.RED + "You have 3 seconds until the game starts!");
                    TimeUnit.SECONDS.sleep(1);
                   
                    Bukkit.broadcastMessage(ChatColor.RED + "You have 2 seconds until the game starts!");
                    TimeUnit.SECONDS.sleep(1);
                   
                    Bukkit.broadcastMessage(ChatColor.RED + "You have 1 second until the game starts!");
                    TimeUnit.SECONDS.sleep(1);
                   
                    Bukkit.broadcastMessage(ChatColor.BLUE + "" + ChatColor.BOLD + "GO!");
                   
                    for (Player player1 : server.getOnlinePlayers()) {
                       
                        player1.setMaxHealth(40);
                        player1.setHealth(20);
                        player1.setExhaustion(0);
                        player1.setGameMode(GameMode.SURVIVAL);
                        player1.getDisplayName().startsWith(ChatColor.RED + "");
                       
                        for (Player player2 : server.getOnlinePlayers()) {
                            if(player1 == player2)
                                continue;
                           
                            player.hidePlayer(player2);
                        }
                        }
                   }
                   
                   
                   
                    Bukkit.broadcastMessage(ChatColor.DARK_PURPLE + "" + "All players are invincible for the first 5 minutes!");
                    Bukkit.broadcastMessage(ChatColor.DARK_PURPLE + "" + "While you're invincible, you're also invisible.");
                    BearUHCCore.setState(GameState.IN_PROGRESS);
                   
                    BearUHCCore.setInvincible(true);
                   
                    TimeUnit.MINUTES.sleep(4);
                   
                    Bukkit.broadcastMessage(ChatColor.RED + "" + "You have 1 minute until your invincibility wears off!");
                    TimeUnit.SECONDS.sleep(49);
                   
                    Bukkit.broadcastMessage(ChatColor.RED + "" + "10 seconds until invincibility wears off!");
               
                    TimeUnit.SECONDS.sleep(5);
                   
                    Bukkit.broadcastMessage(ChatColor.RED + "You have 5 seconds until you're no longer invincible!");
                    TimeUnit.SECONDS.sleep(1);
                   
                    Bukkit.broadcastMessage(ChatColor.RED + "You have 4 seconds until you're no longer invincible!");
                    TimeUnit.SECONDS.sleep(1);
                   
                    Bukkit.broadcastMessage(ChatColor.RED + "You have 3 seconds until you're no longer invincible!");
                    TimeUnit.SECONDS.sleep(1);
                   
                    Bukkit.broadcastMessage(ChatColor.RED + "You have 2 seconds until you're no longer invincible!");
                    TimeUnit.SECONDS.sleep(1);
                   
                    Bukkit.broadcastMessage(ChatColor.RED + "You have 1 second until you're no longer invincible!");
                    TimeUnit.SECONDS.sleep(1);
                   
                    Bukkit.broadcastMessage(ChatColor.GOLD + "" + "You're no longer invincible.");
                   
                    for(Player player : server.getOnlinePlayers()) {
                        for(Player player1 : server.getOnlinePlayers()) {
                            if(player == player1)
                                continue;
                           
                            player.showPlayer(player1);
                        }
                    }
               
                    BearUHCCore.setInvincible(false);
                    boolean battleBegun = false;
                   
                    while(true) {
                       
                        Thread.sleep(3000);
                       
                        if (Bukkit.getServer().getOnlinePlayers().size() == 1) {
                           
                            for(Player player : server.getOnlinePlayers()) {
                                player.sendMessage(ChatColor.BLUE + "Bear" + ChatColor.RED + "UHC " + ChatColor.GRAY + "> " + ChatColor.GREEN + "Congrats, You had won the UHC!" );
                                TimeUnit.SECONDS.sleep(2);
                                player.sendMessage(ChatColor.BLUE + "Bear" + ChatColor.RED + "UHC " + ChatColor.GRAY + "> " + ChatColor.GREEN + "Game restarting in 10sec! Saving stats..");
                                for(int i = 0; i < 10; i++) {
                                    player.giveExpLevels(i);
                                    Thread.sleep(100);
                                   
                                }
                               
                                TimeUnit.SECONDS.sleep(10);
                               
                                player.kickPlayer(ChatColor.BLUE + "Bear" + ChatColor.RED + "UHC " + ChatColor.GRAY + "> " + ChatColor.GREEN + "UHC is now restarting! Congrats! ");
                                BearUHCCore.setState(GameState.FINISHED);
                                Bukkit.reload();
                            }
                        }
                       
                        if (Bukkit.getServer().getOnlinePlayers().size() == 6 && battleBegun != true) {
                           
                            Bukkit.broadcastMessage(ChatColor.WHITE + "In 2 minutes, all players will be teleported within 50 blocks of the center of the map!");
                            TimeUnit.SECONDS.sleep(90);
                           
                            Bukkit.broadcastMessage(ChatColor.RED + "30 seconds left until the final battle begins!");
                            TimeUnit.SECONDS.sleep(20);
                           
                            Bukkit.broadcastMessage(ChatColor.RED + "10 seconds until the final battle begins!");
                           
                            TimeUnit.SECONDS.sleep(5);
                           
                            Bukkit.broadcastMessage(ChatColor.RED + "You have 5 seconds until the battle begins!");
                            TimeUnit.SECONDS.sleep(1);
                           
                            Bukkit.broadcastMessage(ChatColor.RED + "You have 4 seconds until the battle begins!");
                            TimeUnit.SECONDS.sleep(1);
                           
                            Bukkit.broadcastMessage(ChatColor.RED + "You have 3 seconds until the battle begins!");
                            TimeUnit.SECONDS.sleep(1);
                           
                            Bukkit.broadcastMessage(ChatColor.RED + "You have 2 seconds until the battle begins!");
                            TimeUnit.SECONDS.sleep(1);
                           
                            Bukkit.broadcastMessage(ChatColor.RED + "You have 1 second until the battle begins!");
                            TimeUnit.SECONDS.sleep(1);
                           
                            Bukkit.broadcastMessage(ChatColor.GOLD + "" + "The final battle has begun!");
                           
                            for(Player player : server.getOnlinePlayers()) {
                               
                                player.teleport(BearUHCCore.generateRandomLocation(-50, 50, player, true));
                               
                            }
                           
                            battleBegun = true;
                        }
                    }
                }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
            }
    }
    
    GameCommand:

    Code:
    package com.bluebearpvp.core.Command;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Listener;
    
    import com.bluebearpvp.core.BearUHCCore;
    import com.bluebearpvp.core.Utils.GameState;
    
    public class GameCommand implements Listener, CommandExecutor{
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if(cmd.getName().equalsIgnoreCase("uhc")) {
                if (args.length == 0) {
                    if (!sender.isOp()) {
                        sender.sendMessage(ChatColor.BOLD + "" + ChatColor.RED + "UltraHardcore: " + ChatColor.RESET + "Custom coded by BlueBearPvP");
                    return true;
                    } else {
                        sender.sendMessage("UHC Admin Command Help");
                        sender.sendMessage("/uhc startgame - Starts the game currently running on the server.");
                    return true;
                    }
                } else if (args.length == 1) {
                    if (args[0].equalsIgnoreCase("startgame")) {
                        if(!sender.isOp()) {
                            return false;
                        } else if (BearUHCCore.getState() == GameState.IN_PROGRESS) {
                            sender.sendMessage(ChatColor.RED + "Game in progress!");
                        } else {
                           
                        BearUHCCore.startGame();
                        return true;
                       
                        }
                       
                    }
                       
                    }
                }
           
            return false;
    
        }
    }
    
    PS: There is no error in console.
     
  2. Offline

    Zombie_Striker

    @BlueBearPvP
    You cannot use Thread.sleep if you are using the main thread, unless you want to lag your server. If you not using the main thread, then you cannot reference any of Bukkit's methods.

    Can you post the lines that check if the amount of players online.
     
  3. Offline

    BlueBearPvP

    @Zombie_Striker

    Its this part:

    Code:
     @Override
            public void run() {
                try {
               
                if (BearUHCCore.hasStarted() == false && Bukkit.getServer().getOnlinePlayers().size() >= 2 || BearUHCCore.getState() == GameState.STARTING) {
                                                                                               
    Do I have to do something else?
    No wonder why it was lagging/delay on ticks. :p
    EDIT: I will fix the thread stuff. Thanks for the help!
    EDIT2: The problem is also with when I use the command "/uhc startgame". It doesnt work and only when I reload, then it start the game. Its kinda weird. xd
     
  4. Offline

    Zombie_Striker

    @BlueBearPvP
    Your problem may be because you have not encapsulated. Try encapsulating two of the values inside the if statement that match what you want, because
    Code:
    if((Boolean1 && Boolean2) || Boolean3)
    is not the same as
    Code:
    if(Boolean1 && (Boolean2 || Boolean3))
     
  5. Offline

    BlueBearPvP

    @Zombie_Striker

    I had fixed what you told me:

    Code:
    if ((BearUHCCore.hasStarted() == false && Bukkit.getServer().getOnlinePlayers().size() >= 2) || BearUHCCore.getState() == GameState.STARTING) {
    But it still doesnt work. When 2 players joined, it doesnt do anything. When I reload the server, it start. It is also the same with when I do the command. It say in chat "This game had been forced started." but never starts. :(

    Is there any other way to make a timer? Or do you have another solution? Thanks for the help. Really appreciate the help.
     
  6. Offline

    Zombie_Striker

    @BlueBearPvP
    Are you sure BearUHCCore.hasStarted is equal to false? Are you sure that the state is GameState.STARTING? Print out what hasStarted and the gamestate are equal to before and after you reload the server.
     
Thread Status:
Not open for further replies.

Share This Page