Minigame wont start

Discussion in 'Plugin Development' started by justin0399, Dec 16, 2014.

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

    justin0399

    The minigame starts the countdown then says game could not start.


    Code
    Show Spoiler


    Handlers
    Show Spoiler


    Game.java
    Show Spoiler
    Show Spoiler
    Show Spoiler
    Show Spoiler

    package com.mcpixelplex.handlers;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;

    import com.mcpixelplex.utils.LocationUtilities;

    public class Game {

    private static boolean canStart = false;
    private static boolean hasStarted = false;

    private static String[] teams = new String[] {"Red","Blue"};

    public static void start(){
    hasStarted = true;
    new Team(teams);

    int i = 0;
    for (Player player : Bukkit.getOnlinePlayers()){
    if(i > Team.getAllTeams().size())
    i = 1;
    Team.getAllTeams().get(i).add(player);
    LocationUtilities.teleportToGame(player, Team.getAllTeams().get(i));
    player.sendMessage(ChatColor.BLUE + "You are on " + Team.getTeam(player));
    Kit.getKit(player).giveKit(player);
    i++;
    }

    }

    public static void stop(Team team){
    hasStarted = false;
    }

    public static boolean canStart(){
    return false;
    }

    public static boolean hasStarted(){
    return hasStarted;
    }


    public static void setCanStart(boolean b){
    canStart = b;
    }
    }



    Kit.java
    Show Spoiler
    Show Spoiler

    package com.mcpixelplex.handlers;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;

    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.permissions.Permission;

    import com.mcpixelplex.utils.ChatUtilities;
    import com.mcpixelplex.utils.InventoryUtilities;

    public class Kit {

    private static List<Kit> allKits = new ArrayList<Kit>();

    private static HashMap<String, Kit> playerKits = new HashMap<String, Kit>();

    private String name, permission;
    private List<ItemStack> items = new ArrayList<ItemStack>();

    private int displayItem;

    public Kit(String name, List<String> items, int displayItem){
    // id:amount
    this.name = name;
    this.permission = "hardcorekitpvp.kit." + name;
    this.displayItem = displayItem;

    for(String s : items){
    int id = 0, amount = 1;
    if(s.contains(":")){
    String[] splitItem = s.split(":");
    id = Integer.valueOf(splitItem[0].trim());
    amount = Integer.valueOf(splitItem[1].trim());

    }else{
    id = Integer.valueOf(s.trim());
    }
    this.items.add(new ItemStack(id, amount));
    }
    allKits.add(this);
    }

    public static boolean isKit(String name){
    for(Kit k : allKits){
    if(name.equals(k.getName())){
    return true;
    }
    }
    return false;
    }

    public static Kit getKit(String name){
    for(Kit k : allKits){
    if(name.equalsIgnoreCase(name)){
    return k;
    }
    }
    return null;
    }


    public static void setKit(Player player, Kit kit){
    if(!player.hasPermission(kit.getPermissionNode())){
    ChatUtilities.sendMessage(player, "You dont have permssion to use this kit!");
    return;
    }
    playerKits.put(player.getName(), kit);
    }

    public static Kit getKit(Player player){
    return playerKits.get(player.getName())== null ? allKits.get(0) : playerKits.get(player.getName());
    }

    public void setKit(Player player){
    setKit(player, this);
    }

    public void giveKit(Player player){
    InventoryUtilities.clearInventory(player);
    for(ItemStack is : items){
    player.getInventory().addItem(is);
    }
    }

    public String getName() {
    return name;
    }

    public static List<Kit> getAllKits(){
    return allKits;
    }

    public ItemStack getDisplayItem(){
    ItemStack is = new ItemStack(displayItem);
    ItemMeta im = is.getItemMeta();
    im.setDisplayName(name);

    is.setItemMeta(im);
    return is;
    }

    public Permission getPermissionNode(){
    return new Permission(permission);
    }

    }



    Map.java
    Show Spoiler
    Show Spoiler

    package com.mcpixelplex.handlers;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;

    public class Map {

    private static Map activeMap = null;

    private static List<Map> allMaps = new ArrayList<Map>();

    private List<String> spawns;

    private String fileName, mapName;

    public Map(String mapName, String fileName, List<String> spawns){
    this.spawns = spawns;

    this.fileName = fileName;
    this.mapName = mapName;

    allMaps.add(this);
    }

    public static List<Map> getAllMaps(){
    return allMaps;
    }

    public static void setActiveMap(Map map){
    activeMap = map;
    }

    public static Map getActiveMap(){
    return activeMap;
    }

    public String getMapName(){
    return mapName;
    }

    public World getWorld(){
    return Bukkit.getWorld(fileName);
    }

    public Location getSpawn(int teamId){
    //
    for(Team t : Team.getAllTeams()){
    int tId = t.getId();
    //get team id
    if(tId != teamId)
    continue;
    //check if enough spawn points
    int actualTeamId = tId;
    if(spawns.get(tId) == null)
    actualTeamId = 0;

    String teamSpawn = spawns.get(actualTeamId);
    String[] teamSpawns = teamSpawn.split(",");
    return new Location(getWorld(),
    Integer.valueOf(teamSpawns[0])+0.5,
    Integer.valueOf(teamSpawns[1])+0.5,
    Integer.valueOf(teamSpawns[2])+0.5);
    }
    return null;
    }
    }



    Team.java
    Show Spoiler
    Show Spoiler

    package com.mcpixelplex.handlers;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;

    import org.bukkit.Location;
    import org.bukkit.entity.Player;

    import com.mcpixelplex.utils.ChatUtilities;



    public class Team {

    private static List<Team> allTeams = new ArrayList<Team>();
    private static List<Team> activeTeams = new ArrayList<Team>();

    private List<String> members = new ArrayList<String>();


    private static HashMap<String, Team> playerTeams = new HashMap<String, Team>();
    private String teamName;


    private Team(String teamName){
    this.teamName = teamName.trim();

    activeTeams.add(this);
    allTeams.add(this);
    }

    public int getId(){
    for(int i = 0; i < allTeams.size(); i++)
    if(this == allTeams.get(i))
    return i;
    return -1;
    }

    public String getName(){
    return teamName;
    }

    public Team(String[] teamNames){
    for(String s : teamNames){
    new Team(s);
    }
    }

    public Location getSpawn(){
    return Map.getActiveMap().getSpawn(getId());
    }

    public void add(Player player){
    playerTeams.put(player.getName(), this);
    members.add(player.getName());
    }

    public boolean remove(Player player){
    if(!hasTeam(player))
    return false;
    playerTeams.remove(player.getName());
    members.remove(player.getName());

    if(members.isEmpty()){
    activeTeams.remove(this);
    ChatUtilities.broadcast(getName() + "Team has been eradicated!");
    }

    if(activeTeams.size() == 1){
    Game.stop(activeTeams.get(0));
    }

    return true;
    }

    public static boolean hasTeam(Player player){
    return playerTeams.containsKey(player.getName());
    }

    public static Team getTeam(Player player){
    if(!hasTeam(player)){
    return null;
    }
    return playerTeams.get(player.getName());
    }

    public static Team getTeam(String name){
    for(Team t : allTeams)
    if(t.teamName.equalsIgnoreCase(name))
    return t;

    return null;
    }

    public static List<Team> getAllTeams(){
    return allTeams;
    }
    }





    Inventory
    Show Spoiler


    InventoryClick.java
    Show Spoiler
    Show Spoiler
    Show Spoiler

    package com.mcpixelplex.inventory;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.ItemStack;

    import com.mcpixelplex.NationInvation;
    import com.mcpixelplex.handlers.Kit;
    import com.mcpixelplex.listeners.MGListener;

    public class InventoryClick extends MGListener{

    public InventoryClick(NationInvation pl){
    super(pl);
    }

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event){
    if(!(event.getWhoClicked() instanceof Player)){
    return;
    }
    if(event.getInventory().getName().equalsIgnoreCase("Kit Selector")){
    return;
    }
    event.setCancelled(true);
    Player player = (Player)event.getWhoClicked();

    ItemStack i = event.getCurrentItem();

    if(i == null){
    return;
    }
    if(i.getType() == null || i.getType() == Material.AIR){
    return;
    }

    Kit kit = Kit.getKit(i.getItemMeta().getDisplayName());
    Kit.setKit(player, kit);
    player.sendMessage(ChatColor.GREEN + "Kit: " + kit.getName() + " selected!");
    }

    }





    Listeners
    Show Spoiler


    MGListener.Java
    Show Spoiler
    Show Spoiler
    Show Spoiler

    package com.mcpixelplex.listeners;

    import org.bukkit.event.Listener;

    import com.mcpixelplex.NationInvation;

    public class MGListener implements Listener{

    NationInvation plugin;

    public MGListener(NationInvation pl){
    plugin = pl;
    }

    }





    Player
    Show Spoiler


    AsyncPlayerPreLogin.Java
    Show Spoiler
    Show Spoiler
    Show Spoiler

    package com.mcpixelplex.player;

    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
    import org.bukkit.event.player.AsyncPlayerPreLoginEvent.Result;

    import com.mcpixelplex.NationInvation;
    import com.mcpixelplex.handlers.Game;
    import com.mcpixelplex.listeners.MGListener;



    public class AsyncPlayerPreLogin extends MGListener{

    public AsyncPlayerPreLogin(NationInvation pl){
    super(pl);
    }

    @EventHandler
    public void playerPreLogin(AsyncPlayerPreLoginEvent event){
    if (Game.hasStarted())
    event.disallow(Result.KICK_OTHER, ChatColor.RED + "The game has already started.");
    }

    }



    PlayerDeath.java
    Show Spoiler
    Show Spoiler

    package com.mcpixelplex.player;

    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.entity.PlayerDeathEvent;

    import com.mcpixelplex.NationInvation;
    import com.mcpixelplex.handlers.Team;
    import com.mcpixelplex.listeners.MGListener;

    public class PlayerDeath extends MGListener{

    public PlayerDeath(NationInvation pl){
    super(pl);
    }

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event){
    Player player = event.getEntity();
    Team.getTeam(player).remove(player);

    player.kickPlayer(ChatColor.RED + "You Died");

    }

    }



    PlayerInteract.java
    Show Spoiler
    Show Spoiler

    package com.mcpixelplex.player;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;

    import com.mcpixelplex.NationInvation;
    import com.mcpixelplex.handlers.Game;
    import com.mcpixelplex.handlers.Kit;
    import com.mcpixelplex.listeners.MGListener;

    public class PlayerInteract extends MGListener{

    public PlayerInteract(NationInvation pl){
    super(pl);
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event){
    if(event.getItem() == null){
    return;
    }
    if(event.getItem().getType() == Material.AIR || event.getItem().getType() == null){
    return;
    }
    if(event.getItem().getType() == Material.DIAMOND_SWORD && !Game.hasStarted()){
    Inventory inv = Bukkit.createInventory(null, 27, "Kit Selector");
    for(Kit k : Kit.getAllKits()){
    inv.addItem(k.getDisplayItem());
    }
    event.getPlayer().openInventory(inv);
    }
    }

    }



    PlayerJoin.java
    Show Spoiler
    Show Spoiler

    package com.mcpixelplex.player;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import com.mcpixelplex.NationInvation;
    import com.mcpixelplex.handlers.Game;
    import com.mcpixelplex.listeners.MGListener;
    import com.mcpixelplex.utils.InventoryUtilities;
    import com.mcpixelplex.utils.LocationUtilities;

    public class PlayerJoin extends MGListener{

    public PlayerJoin(NationInvation pl) {
    super(pl);
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event){
    Player player = event.getPlayer();
    Game.setCanStart(Bukkit.getOnlinePlayers().length >= 2);

    LocationUtilities.teleportToSpawn(player);
    InventoryUtilities.clearInventory(player);

    ItemStack is = new ItemStack(Material.DIAMOND_SWORD);
    ItemMeta im = is.getItemMeta();

    im.setDisplayName(ChatColor.GREEN + "Kits");
    is.setItemMeta(im);

    player.getInventory().addItem(is);
    player.updateInventory();

    }



    }



    PlayerQuit.java
    Show Spoiler
    Show Spoiler

    package com.mcpixelplex.player;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerQuitEvent;

    import com.mcpixelplex.GameState;
    import com.mcpixelplex.NationInvation;
    import com.mcpixelplex.handlers.Game;
    import com.mcpixelplex.handlers.Team;
    import com.mcpixelplex.listeners.MGListener;


    public class PlayerQuit extends MGListener{

    public PlayerQuit(NationInvation pl) {
    super(pl);
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event){
    if(GameState.isState(GameState.IN_LOBBY)){
    Game.setCanStart(Bukkit.getOnlinePlayers().length - 1 >= 2);
    }
    Player player = event.getPlayer();
    if(Game.hasStarted())
    Team.getTeam(player).remove(player);
    }
    }





    threads
    Show Spoiler


    StartCountdown.java
    Show Spoiler
    Show Spoiler
    Show Spoiler

    package com.mcpixelplex.player;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerQuitEvent;

    import com.mcpixelplex.GameState;
    import com.mcpixelplex.NationInvation;
    import com.mcpixelplex.handlers.Game;
    import com.mcpixelplex.handlers.Team;
    import com.mcpixelplex.listeners.MGListener;


    public class PlayerQuit extends MGListener{

    public PlayerQuit(NationInvation pl) {
    super(pl);
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event){
    if(GameState.isState(GameState.IN_LOBBY)){
    Game.setCanStart(Bukkit.getOnlinePlayers().length - 1 >= 2);
    }
    Player player = event.getPlayer();
    if(Game.hasStarted())
    Team.getTeam(player).remove(player);
    }
    }






    Utils
    Show Spoiler


    ChatUtilities.java
    Show Spoiler
    Show Spoiler
    Show Spoiler

    package com.mcpixelplex.utils;

    import static org.bukkit.ChatColor.*;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;

    public class ChatUtilities {

    public static void broadcast(String msg){
    for(Player player: Bukkit.getOnlinePlayers()){
    player.sendMessage(starter() + msg);
    }
    }

    public static void sendMessage(Player player, String msg){
    player.sendMessage(starter() + msg);
    }

    private static String starter(){
    return BLUE + "[" + RED + "NationInvation" + BLUE + "] " + WHITE;
    }
    }



    InventoryUtilities.java
    Show Spoiler
    Show Spoiler

    package com.mcpixelplex.utils;

    import org.bukkit.entity.Player;
    public class InventoryUtilities {

    public static void clearInventory(Player player){
    player.getInventory().clear();
    player.getInventory().setArmorContents(null);
    player.updateInventory();
    }
    }



    LocationUtilities.java
    Show Spoiler
    Show Spoiler

    package com.mcpixelplex.utils;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;

    import com.mcpixelplex.handlers.Team;

    public class LocationUtilities {

    private static Location spawnLocation = new Location(Bukkit.getWorld("Lobby"), 0.5, 64, 0.5);

    public static void teleportToSpawn(Player player){
    player.teleport(spawnLocation);
    }

    public static void teleportAllToSpawn(){
    for(Player p : Bukkit.getOnlinePlayers())
    teleportToSpawn(p);
    }

    public static void teleportToGame(Player player, Team team){
    player.teleport(team.getSpawn());
    }

    }





    GameState.java
    Show Spoiler
    Show Spoiler

    package com.mcpixelplex;

    public enum GameState {

    IN_LOBBY(true), IN_GAME(false), POST_GAME(false), RESETTING(false);

    private boolean canJoin;
    static GameState currentState;

    GameState(boolean canJoin){
    this.canJoin = canJoin;
    }

    public boolean canJoin(){
    return canJoin;
    }

    public static void setState(GameState state){
    GameState.currentState = state;
    }

    public static boolean isState(GameState state){
    return GameState.currentState == state;
    }

    public static GameState getState(){
    return currentState;
    }

    }



    NationInvation.java
    Show Spoiler
    Show Spoiler

    package com.mcpixelplex;

    import java.io.File;
    import java.util.Arrays;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    import com.mcpixelplex.handlers.Kit;
    import com.mcpixelplex.player.AsyncPlayerPreLogin;
    import com.mcpixelplex.player.PlayerDeath;
    import com.mcpixelplex.player.PlayerJoin;
    import com.mcpixelplex.player.PlayerQuit;
    import com.mcpixelplex.threads.StartCountdown;



    public class NationInvation extends JavaPlugin{

    public static int startCountdownId;


    public void onEnable(){
    GameState.setState(GameState.IN_LOBBY);

    setupConfig();
    registerKits();

    startCountdown();
    }

    public void registerListeners(){
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvents(new PlayerJoin(this), this);
    pm.registerEvents(new PlayerQuit(this), this);
    pm.registerEvents(new AsyncPlayerPreLogin(this), this);
    pm.registerEvents(new PlayerDeath(this), this);
    }

    public void setupConfig(){
    File resetFile = new File(getDataFolder(), "RESET.FILE");
    FileConfiguration config = getConfig();
    if(resetFile.exists()){
    return;
    }else{
    config.set("Kits.Archer.Items", Arrays.asList("272", "297:5"));
    config.set("Kits.Archer.Display Item", 272);

    config.set("Kits.Warrior.Items", Arrays.asList("72", "27:5"));
    config.set("Kits.Warrior.Display Item", 1);

    saveConfig();
    try{
    resetFile.createNewFile();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }

    public void registerKits(){
    FileConfiguration config = getConfig();
    for(String s : getConfig().getConfigurationSection("Kits").getKeys(false)){
    String path = "Kits." + s + ".";
    new Kit(s, config.getStringList(path + "Items"), config.getInt(path + "Display Item"));
    }
    }

    public void startCountdown(){
    StartCountdown.timeUntilStart = 60;
    startCountdownId = getServer().getScheduler().scheduleSyncRepeatingTask(this, new StartCountdown(this), 20l, 20l);
    }

    public void stopCountdown(){
    getServer().getScheduler().cancelTask(startCountdownId);
    }

    public void restartCountdown(){
    stopCountdown();
    startCountdown();
    }
    }



     
  2. Offline

    Darkpicasa

    I'm not reading through all that code, but console error of some sort?
     
  3. Offline

    SuperOriginal

    Use code tags (Insert > Code > Java) instead of spoilers.

    The ruined indentation is making my eyes bleed.
     
  4. Offline

    Jayke_

    With that amount of code, just post the jar file and I'll look at it.
     
  5. Offline

    justin0399

Thread Status:
Not open for further replies.

Share This Page