Minigame Essentials

Discussion in 'Plugin Development' started by Dirtcraft24, Jun 1, 2015.

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

    Dirtcraft24

    So after a few months of coding in Java with the Bukkit API ive made a few simple 'minigames' and Im going to attempt a larger scale project.

    In the past Ive used ArrayLists with player names to add them to teams but i need a brief rundown or tutorial on how to use multiple arenas running at once. For example on some servers they can have seperate arenas for different games. How would I go about this?
     
  2. Offline

    caderape

    @Dirtcraft24 it's not easy to explain. So i will show you an example.

    Code:
    public class GameManager implements Listener {
    
      
      
        public static ArrayList<Game> games = new ArrayList<>();
      
      
      
      
        public Game getGame(Player player)
        {
            for (Game game : games)
            {
                if (game.getPlayers().contains(player.getUniqueId()))
                    return game;
            }
            return null;
        }
      
      
        public Game getGame(Location spawn)
        {
            for (Game game : games)
            {
                if (game.getSpawn().equals(spawn))
                    return game;
            }
            return null;
        }
      
      
      
      
      
      
        @EventHandler
        public void onDeath(PlayerDeathEvent e)
        {
            Game game = getGame(e.getEntity());
            if (game != null) {
                game.getPlayers().remove(e.getEntity().getUniqueId());
                if (game.getPlayers().size() == 1) {
                    Bukkit.getPlayer(game.getPlayers().get(0)).sendMessage("You won the game !");
                    games.remove(game); // endgame
                }
            }
        }
      
      
      
      
        public class Game {
          
            private List<UUID> players = new ArrayList<>();
          
            private Location spawn;
          
          
          
            public Game(Location spawn)
            {
                this.spawn = spawn;
                GameManager.games.add(this);
            }
    
    
    
          
          
            public List<UUID> getPlayers() {
                return players;
            }
    
    
    
            public void setPlayers(List<UUID> players) {
                this.players = players;
            }
    
    
    
            public Location getSpawn() {
                return spawn;
            }
    
    
    
            public void setSpawn(Location spawn) {
                this.spawn = spawn;
            }
        }
    }
    
    Here i created a game Last Man Standing. Every instance of the game is stored in the arraylist 'games'. I get back infos on the playerDeathEvent with a loops. Everytime you will call the constructor 'Game game = new Game(location)' , you will create an instance of the game. You will just have to put in the constructor the spawn you want for this arena, add players, and start the game. This is basically how it works. You don't have to put the classGame in the clas GameManager, it was just for make it short.
     
  3. Offline

    Dirtcraft24


    Very confusing for me but I feel like i get the jist of it. Is the proper way about creating join signs with coordinates?
     
  4. Offline

    caderape

    @Dirtcraft24 It's a way to do it. Set the values in the sign and get back the location when the player click the sign.
    It may be better to store the location of your arenas in the config file. and check which one is free when the player click on the sign
     
  5. Offline

    westjet

    Last edited: Jun 2, 2015
  6. This is some of the code from my Titanfall plugin, sorry for the spoon-feeding. This is also made using Java 8, so if you're using Java 6/7, you may have to change some stuff, I don't know.

    Code:
    import org.bukkit.*;
    import org.bukkit.block.Sign;
    import org.bukkit.configuration.serialization.ConfigurationSerializable;
    import org.bukkit.entity.Player;
    import org.bukkit.scoreboard.*;
    
    import java.util.*;
    
    public class Arena implements ConfigurationSerializable, Iterable<Player> {
    
        private static Map<Integer, Arena> registeredArenas = new HashMap<>();
    
        private int arenaID = 0;
        private int maxPlayers = 12;
    
        private List<UUID> uuidList = null;
        private boolean gameStarted = false;
    
        public Arena(int arenaID) {
            this.uuidList = new ArrayList<>();
    
            this.arenaID = arenaID;
            this.gameStarted = false;
        }
    
        public void addPlayer(Player player) {
            if (player != nul && !this.uuidList.contains(player.getUniqueId()))
                this.uuidList.add(player.getUniqueId());
        }
    
        public void clearPlayers() {
            this.uuidList.clear();
        }
    
        public void endGame() {
            // Do something.
        }
    
        public int getID() {
            return this.arenaID;
        }
    
        public int getMaxPlayers() {
            return this.maxPlayers;
        }
    
        public List<Player> getPlayers() {
            List<Player> playerList = new ArrayList<>();
            for (UUID uuid : this.uuidList) {
                Player player = Bukkit.getServer().getPlayer(uuid);
                if (player != null) playerList.add(player);
            }
            return playerList;
        }
    
        public List<UUID> getUserList() {
            return this.uuidList;
        }
    
        public boolean hasStarted() {
            return this.gameStarted;
        }
    
        public boolean isFull() {
            return this.uuidList.size() >= this.maxPlayers;
        }
    
        public void removePlayer(Player player) {
            if (player != null) this.uuidList.remove(player.getUniqueId());
        }
    
        public void setMaxPlayers(int maxPlayers) {
            this.maxPlayers = maxPlayers;
        }
    
        public void setStarted(boolean flag) {
            this.gameStarted = flag;
        }
    
        public void startGame() {
            // Do something.
        }
    
        @Override
        public ListIterator<Player> iterator() {
            return this.getPlayers().listIterator();
        }
    
        @Override
        public Map<String, Object> serialize() {
            Map<String, Object> serializedArena = new HashMap<>();
            serializedArena.put("ID", this.arenaID);
            serializedArena.put("Max players", this.maxPlayers);
    
            return serializedArena;
        }
    
        public static Arena deserialize(Map<String, Object> serializedArena) {
            if (serializedArena != null && serializedArena.containsKey("ID")) {
                int arenaID = (int) serializedArena.get("ID");
                if (arenaID < 0) return null;
                int maxPlayers = (int) serializedArena.get("Max players");
    
                Arena arena = new Arena(arenaID);
                arena.setMaxPlayers(maxPlayers);
                return arena;
            }
            return null;
        }
    
        public static void clearArenas() {
            registeredArenas.clear();
        }
    
        public static Arena getArena(int arenaID) {
            return registeredArenas.get(arenaID);
        }
    
        public static Arena getArena(Player player) {
            for (Arena arena : registeredArenas.values()) {
                if (arena.getUserList().contains(player.getUniqueId())) return arena;
            }
            return null;
        }
    
        public static List<Arena> getArenas() {
            return new ArrayList<>(registeredArenas.values());
        }
    
        public static void registerArena(Arena arena) throws NullPointerException {
            if (arena != null) {
                if (!registeredArenas.containsKey(arena.getID())) registeredArenas.put(arena.getID(), arena);
            } else {
                throw new NullPointerException("Arena cannot be null!");
            }
        }
    
    }
    
    You could change this to your needs, or just look at it so you gain knowledge I guess...

    This allows you to save Arenas to the configuration (config.set("Arena 2", arena.serialize())), and load them up when the server starts.

    For sign locations, make your own class, extending Location, that serializes to a Map, or a String. I prefer using a String for Locations as it is all in one line.

    For example:
    Code:
    import org.bukkit.*;
    
    public class SuperLocation extends Location {
    
        public SuperLocation(Location location) {
            this(location.getWorld(), location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
        }
    
        public SuperLocation(World world, double x, double y, double z) {
            this(world, x, y, z, 0F, 0F);
        }
    
        public SuperLocation(World world, double x, double y, double z, float yaw, float pitch) {
            super(world, x, y, z, yaw, pitch);
        }
    
        public String toString() {
            return (this.getWorld() != null ? this.getWorld().getName() : "null") + ":" + this.getX() + ":" + this.getY() + ":" + this.getZ() + ":" + this.getYaw() + ":" + this.getPitch();
        }
    
        public static SuperLocation fromString(String serializedLocation) {
            try {
                if (serializedLocation != null) {
                    if (serializedLocation.contains(":")) {
                        String[] locationSplit = serializedLocation.split(":");
                        if (locationSplit.length == 5) {
                            String strWorld = locationSplit[0];
                            double xPosition = Double.parseDouble(locationSplit[1]);
                            double yPosition = Double.parseDouble(locationSplit[2]);
                            double zPosition = Double.parseDouble(locationSplit[3]);
                            float yaw = Float.parseFloat(locationSplit[4]);
                            float pitch = Float.parseFloat(locationSplit[5]);
                            return new SuperLocation(!strWorld.equals("null") ? Bukkit.getServer().getWorld(strWorld) : (!Bukkit.getServer().getWorlds().isEmpty() ? Bukkit.getServer().getWorlds().get(0) : null), xPosition, yPosition, zPosition, yaw, pitch);
                        }
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return null;
        }
    
    }
    
    Usage: SuperLocation.fromString(serializedArena.get("Sign location").toString());
     
    Last edited: Jun 2, 2015
Thread Status:
Not open for further replies.

Share This Page