Help with Minigame .o/ !

Discussion in 'Plugin Development' started by OhYes, May 12, 2016.

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

    OhYes

    When the plugin is enabled, nothing happens. Also, /run crashes the server. Any help?
    Code:
    package me.ohyes.enderchase;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    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.PlayerMoveEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
        public static enum GAMESTATE{
             Loading(), CountdownLobby(), CountdownGame(), InGame(), PostGame();
        };
       
        public static GAMESTATE gameState;
    
        public int playerCount = Bukkit.getOnlinePlayers().size();
        public int maxPlayerCount = 1;
    
        public static String prefix = ChatColor.DARK_PURPLE + "[" + ChatColor.YELLOW + "EnderChase" + ChatColor.DARK_PURPLE
                + "] ";
       
        public final FileConfiguration config = this.getConfig();
       
        int loadinggame;
        int lobbycountdown;
        int gamecountdown;
       
        int countdowntimer1 = 10;
        int countdowntimer2 = 10;
       
        public ArrayList<Player> playersInGame = new ArrayList<Player>();
       
        ItemStack enderpearl = new ItemStack(Material.EYE_OF_ENDER);
    
        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            config.options().copyDefaults();
            saveDefaultConfig();
            initGame();
        }
    
        //Initialise Game
        private void initGame() {
            gameState = GAMESTATE.Loading;
    
            loadinggame = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    
                @Override
                public void run() {
                   
                    if(playerCount == maxPlayerCount){
                        gameState = GAMESTATE.CountdownLobby;
                        runCountdownLobby();
                        Bukkit.getScheduler().cancelTask(loadinggame);
                    }else{
                        return;
                    }
                   
                }
               
            }, 0L, 20L);
        }
    
        protected void runCountdownLobby() {
    
            if(gameState == GAMESTATE.CountdownLobby){
                lobbycountdown = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    
                    @Override
                    public void run() {
                       
                        if(playerCount != maxPlayerCount){
                            gameState = GAMESTATE.Loading;
                            initGame();
                        }else{
                       
                        if(countdowntimer1 > 0){
                            countdowntimer1 --;
                            Bukkit.broadcastMessage(prefix + ChatColor.YELLOW + "Game Starting in... " + countdowntimer1);
                            for(Player all : Bukkit.getOnlinePlayers()){
                                all.getWorld().playSound(all.getLocation(), Sound.BLOCK_NOTE_BASEDRUM, 5, 5);
                            }
                        }else if(countdowntimer1 <= 0){
                            gameState = GAMESTATE.CountdownGame;
                            runCountdownGame();
                            countdowntimer1 = 10;
                            Bukkit.getScheduler().cancelTask(countdowntimer1);
                        }
                       
                      }
                    }
                   
                }, 0L, 20L);
            }
           
        }
    
        protected void runCountdownGame() {
            for(Player all : Bukkit.getOnlinePlayers()){
                all.teleport(new Location(all.getWorld(),0,20,0));
                playersInGame.add(all);
                all.getInventory().clear();
            }
           
            gamecountdown = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    
                @Override
                public void run() {
                   
                   
                    if(playerCount != maxPlayerCount){
                        Bukkit.broadcastMessage(prefix + ChatColor.YELLOW + "Game stopped. Not enough Players.");
                        countdowntimer2 = 10;
                        return;
                    }else{
                        if(countdowntimer2 > 0){
                            countdowntimer2 --;
                            Bukkit.broadcastMessage(prefix + ChatColor.YELLOW + "The Game will begin in " + countdowntimer2 + " seconds...");
                        }else if(countdowntimer2 <= 0){
                            gameState = GAMESTATE.InGame;
                            runGame();
                            countdowntimer2 = 10;
                            Bukkit.getScheduler().cancelTask(gamecountdown);
                        }
                    }
                   
                }
               
            }, 0L, 20L);
           
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(cmd.getName().equalsIgnoreCase("run")){
                initGame();
            }
            return false;
        }
    
           
        public void runGame() {
                for(Player all : Bukkit.getOnlinePlayers()){
                    playersInGame.remove(all);
                    all.getInventory().clear();
                    all.getInventory().addItem(enderpearl);
                }
               
               
           
        }
    
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e){
            Player moved = e.getPlayer();
            if(playersInGame.contains(moved)){
                e.setCancelled(true);
            }
        }
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player joined = e.getPlayer();
            e.setJoinMessage(prefix + ChatColor.YELLOW + joined.getName() + ChatColor.LIGHT_PURPLE
                    + " has joined the game. (" + ChatColor.YELLOW + playerCount + "/" + maxPlayerCount + ChatColor.LIGHT_PURPLE + ")");
        }
    
        @EventHandler
        public void onPlayerLeave(PlayerQuitEvent e) {
            Player left = e.getPlayer();
            e.setQuitMessage(prefix + ChatColor.YELLOW + left.getName() + ChatColor.LIGHT_PURPLE
                    + " has left the game. (" + ChatColor.YELLOW + playerCount + "/" + maxPlayerCount + ChatColor.LIGHT_PURPLE + ")");
        }
    
    }
    
     
  2. Offline

    Zombie_Striker

    Dont name your main classes "main"

    This is only set once, and you are setting it before the server is even up. This will mean that PlayerCount will always be 0.

    Don't abuse static.

    1. You should use Sets and HashSets instead of arraylists
    2. Save the Player's UUID, not the whole instance. This could cause memory leaks.

    Besides from that: Your code is not organized and not (for lack of better words) "Optimized". Instead, create a GameStateManager, and create states such as "LobbyState" "Waiting State" "In-Game State" ect. Inside each state, write down what each state does, prints out,ect..
     
Thread Status:
Not open for further replies.

Share This Page