Advertisement
NullChips

MinigameManager.java

Mar 4th, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.41 KB | None | 0 0
  1. package com.pvpmadness.pvpmadness.minigames.minigameutils;
  2.  
  3. import com.pvpmadness.pvpmadness.PvPMadness;
  4. import com.pvpmadness.pvpmadness.minigames.Arena;
  5. import com.pvpmadness.pvpmadness.minigames.Minigame;
  6. import com.pvpmadness.pvpmadness.minigames.ffa.FFA;
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.Location;
  9. import org.bukkit.World;
  10. import org.bukkit.configuration.file.FileConfiguration;
  11.  
  12. import java.util.ArrayList;
  13.  
  14. /**
  15.  * Copyright (c) NullChips 2017. All rights reserved.
  16.  * All code contained within this document is the
  17.  * sole property of NullChips. Distribution, reproduction,
  18.  * taking snippets or claiming any contents as your own will
  19.  * break the terms of the license, and void any agreements with
  20.  * you, the third party.
  21.  * Thanks.
  22.  */
  23. public class MinigameManager {
  24.  
  25.     private MinigameManager() {
  26.     }
  27.  
  28.     private static MinigameManager instance = new MinigameManager();
  29.  
  30.     public static MinigameManager getInstance() {
  31.         return instance;
  32.     }
  33.  
  34.     FileConfiguration amf = PvPMadness.getArenaManagementFile().getFile();
  35.  
  36.     private ArrayList<Minigame> minigames = new ArrayList<Minigame>();
  37.     private int maxSpawnpointTries;
  38.  
  39.  
  40.     public ArrayList<Minigame> getMinigames() {
  41.         return minigames;
  42.     }
  43.     private ArrayList<String> minigameTypes;
  44.  
  45.     public void addMinigame(Minigame m) {
  46.         this.minigames.add(m);
  47.     }
  48.  
  49.     public void clearMinigames() {
  50.         this.minigames.clear();
  51.     }
  52.  
  53.     public void registerMinigames() {
  54.  
  55.         registerTypes();
  56.  
  57.         int index = 1;
  58.         for(index = 1; index < maxSpawnpointTries; index ++) {
  59.             if(isMinigameInConfig(String.valueOf(index))) {
  60.  
  61.                 //REGISTER FFA GAMES.
  62.                 if(amf.get(index + ".minigame-type") == "ffa" ) {
  63.  
  64.                     boolean playable = getBooleanFromArenaFile(index + ".playable");
  65.                     int minPlayers = amf.getInt(index + ".min-players");
  66.                     String displayName = amf.getString(index + ".display-name");
  67.  
  68.                     ArrayList<Location> locations = getLocations(String.valueOf(index), maxSpawnpointTries);
  69.  
  70.                     Arena arena = new Arena(String.valueOf(index), locations);
  71.  
  72.                     FFA ffa = new FFA(arena, minPlayers, playable, index);
  73.  
  74.                 }
  75.             }
  76.         }
  77.     }
  78.  
  79.     private boolean getBooleanFromArenaFile(String s) {
  80.         if(amf.getString(s).equalsIgnoreCase("true")) {
  81.             return true;
  82.         } else {
  83.             return false;
  84.         }
  85.     }
  86.  
  87.     private void registerTypes() {
  88.  
  89.         minigameTypes.clear();
  90.  
  91.         FFA ffa = new FFA(null, 0, false, 0);
  92.         minigameTypes.add(ffa.getMinigameType());
  93.         ffa = null;
  94.     }
  95.  
  96.  
  97.     public ArrayList<Location> getLocations(String arenaId, int maxSpawnpointTries) {
  98.         int spawnpointIndex = 1;
  99.  
  100.         ArrayList<Location> locations = new ArrayList<Location>();
  101.  
  102.         for(spawnpointIndex = 1; spawnpointIndex < maxSpawnpointTries; spawnpointIndex++) {
  103.  
  104.             if (isSpawnpointInConfig(arenaId, spawnpointIndex)) {
  105.                 double x, y, z;
  106.                 World w;
  107.  
  108.                 x = amf.getDouble(arenaId + ".spawnpoint" + spawnpointIndex + ".x");
  109.                 y = amf.getDouble(arenaId + ".spawnpoint" + spawnpointIndex + ".y");
  110.                 z = amf.getDouble(arenaId + ".spawnpoint" + spawnpointIndex + ".z");
  111.  
  112.                 w = Bukkit.getServer().getWorld(amf.getString(arenaId + ".spawnpoint" + spawnpointIndex + "world"));
  113.  
  114.                 Location l = new Location(w, x, y, z);
  115.  
  116.                 locations.add(l);
  117.             }
  118.         }
  119.         if(locations.size() != 0) {
  120.             return locations;
  121.         } else {
  122.             return null;
  123.         }
  124.     }
  125.  
  126.     public boolean isWorld(String worldName) {
  127.         return Bukkit.getServer().getWorld(worldName) != null;
  128.     }
  129.  
  130.     public boolean isSpawnpointInConfig(String minigameId, int index) {
  131.         if(amf.contains(minigameId + ".spawnpoint" + index + ".x") &&  amf.contains(minigameId + ".spawnpoint" + index + ".y")
  132.                 && amf.contains(minigameId + ".spawnpoint" + index + ".z") && amf.contains(minigameId + ".spawnpoint" + index + ".world") &&
  133.                 amf.get(minigameId + ".spawnpoint" + index + ".x") instanceof Double && amf.get(minigameId + ".spawnpoint" + index + ".y") instanceof Double &&
  134.                 amf.get(minigameId + ".spawnpoint" + index + ".z") instanceof Double && isWorld(amf.getString(minigameId + ".spawnpoint" + index + "world"))) {
  135.             return true;
  136.         } else {
  137.             return false;
  138.         }
  139.     }
  140.    
  141.     public boolean isMinigameInConfig(String minigameId) {
  142.         if (amf.contains(minigameId + ".min-players") && amf.get(minigameId + ".min-players") instanceof Integer &&
  143.                 amf.contains(minigameId + ".playable") && amf.get(minigameId + ".playable") instanceof String &&
  144.                 amf.contains(minigameId + ".minigame-type") && amf.get(minigameId + ".minigame-type") instanceof String &&
  145.                 amf.contains(minigameId + ".display-name") && amf.get(minigameId + ".display-name") instanceof String) {
  146.  
  147.             if (minigameTypes.contains(amf.getString(minigameId + ".minigame-type"))) {
  148.                 return true;
  149.             }
  150.         }
  151.         return false;
  152.     }
  153.  
  154.     public void setMaxSpawnpointTries(int maxSpawnpointTries) {
  155.         this.maxSpawnpointTries = maxSpawnpointTries;
  156.     }
  157.  
  158.     public int getMaxSpawnpointTries() {
  159.         return maxSpawnpointTries;
  160.     }
  161.  
  162.     public String getNewMinigameId() {
  163.         int i;
  164.         for(i = 1; i < maxSpawnpointTries; i++) {
  165.             if(!amf.contains((i + ".playable"))) {
  166.                 break;
  167.             }
  168.         }
  169.  
  170.         if(i == maxSpawnpointTries - 1) {
  171.             return null;
  172.         }
  173.  
  174.         return String.valueOf(i);
  175.     }
  176.  
  177.     public Minigame getMinigameFromId(String id) {
  178.         int i = Integer.parseInt(id);
  179.  
  180.         for(Minigame m : this.minigames) {
  181.             if (m.getId() == i) {
  182.                 return m;
  183.             }
  184.         }
  185.         return null;
  186.     }
  187.  
  188.     public int getNewSpawnId(String minigameId) {
  189.         int newSpawnId;
  190.         for(newSpawnId = 1; newSpawnId < maxSpawnpointTries; newSpawnId++) {
  191.             if (!isSpawnpointInConfig(minigameId, newSpawnId)) {
  192.                 return newSpawnId;
  193.             }
  194.         }
  195.         return 0;
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement