implement config re-loading
This commit is contained in:
		
							
								
								
									
										3
									
								
								TODO.md
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								TODO.md
									
									
									
									
									
								
							| @@ -9,13 +9,14 @@ | ||||
| # Malloc Beta | ||||
|  | ||||
| [X] One arena config | ||||
| [ ] Config reload | ||||
| [X] Config reload | ||||
| [X] Leave games | ||||
| [X] Join games | ||||
| [ ] Lobby with instructions | ||||
| [ ] Drop back to lobby on game over | ||||
| [ ] Grist drops | ||||
| [ ] Item shoppes | ||||
| [ ] Command tab completion | ||||
|  | ||||
| # Scaled waves | ||||
|  | ||||
|   | ||||
| @@ -2,6 +2,9 @@ package gg.malloc.defense; | ||||
|  | ||||
| import gg.malloc.defense.engine.GameRunner; | ||||
|  | ||||
| import java.util.Collection; | ||||
| import java.util.HashSet; | ||||
|  | ||||
| import org.bukkit.event.EventHandler; | ||||
| import org.bukkit.event.Listener; | ||||
| import org.bukkit.event.entity.EntityDeathEvent; | ||||
| @@ -12,20 +15,32 @@ import org.bukkit.event.player.PlayerQuitEvent; | ||||
| import org.bukkit.entity.Player; | ||||
|  | ||||
| public class GameEventHandler implements Listener { | ||||
|   GameRunner m_runner; | ||||
|   Collection<GameRunner> m_runners; | ||||
|  | ||||
|   public GameEventHandler(GameRunner runner) { | ||||
|     m_runner = runner; | ||||
|   public GameEventHandler() { | ||||
|     m_runners = new HashSet<GameRunner>(); | ||||
|   } | ||||
|  | ||||
|   public void addRunner(GameRunner runner) { | ||||
|     m_runners.add(runner); | ||||
|   } | ||||
|  | ||||
|   public void clear() { | ||||
|     m_runners.clear(); | ||||
|   } | ||||
|  | ||||
|   @EventHandler | ||||
|   public void onEntityDeath(EntityDeathEvent evt) { | ||||
|     m_runner.handleEntityDeath(evt.getEntity()); | ||||
|     for(GameRunner r : m_runners) { | ||||
|       r.handleEntityDeath(evt.getEntity()); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @EventHandler | ||||
|   public void onPlayerQuit(PlayerQuitEvent evt) { | ||||
|     m_runner.removePlayer(evt.getPlayer()); | ||||
|     for(GameRunner r : m_runners) { | ||||
|       r.removePlayer(evt.getPlayer()); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @EventHandler | ||||
| @@ -35,11 +50,15 @@ public class GameEventHandler implements Listener { | ||||
|  | ||||
|   @EventHandler | ||||
|   public void onEntityTarget(EntityTargetEvent evt) { | ||||
|     m_runner.handleEntityRetargeting(evt); | ||||
|     for(GameRunner r : m_runners) { | ||||
|       r.handleEntityRetargeting(evt); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @EventHandler | ||||
|   public void onEntityDamage(EntityDamageEvent evt) { | ||||
|     m_runner.handleEntityDamage(evt); | ||||
|     for(GameRunner r : m_runners) { | ||||
|       r.handleEntityDamage(evt); | ||||
|     } | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -6,7 +6,6 @@ import org.bukkit.Location; | ||||
| import org.bukkit.entity.Entity; | ||||
| import org.bukkit.entity.EntityType; | ||||
| import org.bukkit.entity.Player; | ||||
| import org.bukkit.configuration.ConfigurationSection; | ||||
| import org.bukkit.World; | ||||
| import org.bukkit.WorldCreator; | ||||
|  | ||||
| @@ -25,7 +24,6 @@ import java.util.Map; | ||||
| import java.util.Collection; | ||||
|  | ||||
| import gg.malloc.defense.model.Arena; | ||||
| import gg.malloc.defense.model.Waypoint; | ||||
| import gg.malloc.defense.model.Game; | ||||
|  | ||||
| import gg.malloc.defense.games.LinearGame; | ||||
| @@ -33,46 +31,27 @@ import gg.malloc.defense.games.ScaledWaves; | ||||
|  | ||||
| import gg.malloc.defense.engine.GameRunner; | ||||
|  | ||||
| import gg.malloc.defense.config.Configuration; | ||||
|  | ||||
| import gg.malloc.defense.commands.AddPlayerCommand; | ||||
| import gg.malloc.defense.commands.SetStageCommand; | ||||
| import gg.malloc.defense.commands.PlayerReadyCommand; | ||||
| import gg.malloc.defense.commands.ListGamesCommand; | ||||
| import gg.malloc.defense.commands.JoinGameCommand; | ||||
| import gg.malloc.defense.commands.LeaveGameCommand; | ||||
| import gg.malloc.defense.commands.ReloadCommand; | ||||
|  | ||||
| public class Plugin extends JavaPlugin { | ||||
|   HashMap<String, Arena> m_arenas = new HashMap<>(); | ||||
|   ArrayList<Game> m_games = new ArrayList<>(); | ||||
|   HashMap<String, GameRunner> m_runningGames = new HashMap<>(); | ||||
|   HashMap<Player, GameRunner> m_playerGames = new HashMap<>(); | ||||
|   GameEventHandler m_handler = new GameEventHandler(); | ||||
|  | ||||
|   public Collection<String> arenaNames() { | ||||
|     return m_arenas.keySet(); | ||||
|   } | ||||
|  | ||||
|   private class TestSpawn implements Waypoint { | ||||
|     double m_x; | ||||
|     double m_y; | ||||
|     double m_z; | ||||
|     String m_name; | ||||
|  | ||||
|     public TestSpawn(double x, double y, double z) { | ||||
|       m_x = x; | ||||
|       m_y = y; | ||||
|       m_z = z; | ||||
|       m_name = "(" + x + "," + y + "," + z + ")"; | ||||
|     } | ||||
|  | ||||
|     public double getX() { return m_x; } | ||||
|     public double getY() { return m_y; } | ||||
|     public double getZ() { return m_z; } | ||||
|  | ||||
|     @Override | ||||
|     public String getName() { | ||||
|       return m_name; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   public void debuginfo() { | ||||
|     getLogger().info("Debug Info:"); | ||||
|   } | ||||
| @@ -81,9 +60,8 @@ public class Plugin extends JavaPlugin { | ||||
|   public void onEnable() { | ||||
|     getLogger().info("Malloc Defense registered"); | ||||
|     getLogger().setLevel(Level.FINEST); | ||||
|     //setupDemoGame(); | ||||
|     m_games.add(new ScaledWaves()); | ||||
|     loadArenas(); | ||||
|     reloadArenas(); | ||||
|     getCommand("join").setExecutor(new JoinGameCommand(this)); | ||||
|     getCommand("leave").setExecutor(new LeaveGameCommand(this)); | ||||
|     getCommand("ready").setExecutor(new PlayerReadyCommand(this)); | ||||
| @@ -92,54 +70,33 @@ public class Plugin extends JavaPlugin { | ||||
|     getCommand("addplayer").setExecutor(new AddPlayerCommand(this)); | ||||
|     getCommand("setstage").setExecutor(new SetStageCommand(this)); | ||||
|     getCommand("debuginfo").setExecutor(new DebuginfoCommand(this)); | ||||
|     getCommand("reload").setExecutor(new ReloadCommand(this)); | ||||
|  | ||||
|     getServer().getPluginManager().registerEvents(new PlayerQuitHandler(), this); | ||||
|     getServer().getPluginManager().registerEvents(m_handler, this); | ||||
|   } | ||||
|  | ||||
|   void loadArenas() { | ||||
|   public void reloadArenas() { | ||||
|     getLogger().info("Loading arenas..."); | ||||
|     saveDefaultConfig(); | ||||
|     ConfigurationSection pluginConfig = getConfig(); | ||||
|     ConfigurationSection mapList = pluginConfig.getConfigurationSection("maps"); | ||||
|     reloadConfig(); | ||||
|     Configuration config = new Configuration(getConfig()); | ||||
|  | ||||
|     for(String mapName : mapList.getKeys(false)) { | ||||
|       getLogger().info("Loading arena: " + mapName); | ||||
|       ConfigurationSection mapConfig = mapList.getConfigurationSection(mapName); | ||||
|       List<Map<?, ?>> spawnpointList = mapConfig.getMapList("spawnpoints"); | ||||
|       ArrayList<Waypoint> spawnpoints = new ArrayList<>(); | ||||
|  | ||||
|       for(Map<?, ?> spawnerObj : spawnpointList) { | ||||
|         Map<String, Double> thisSpawner = (Map<String, Double>)spawnerObj; | ||||
|         double x = thisSpawner.get("x"); | ||||
|         double y = thisSpawner.get("y"); | ||||
|         double z = thisSpawner.get("z"); | ||||
|         spawnpoints.add(new TestSpawn(x, y, z)); | ||||
|     for(GameRunner runner : m_runningGames.values()) { | ||||
|       runner.requestTransition(GameRunner.Stage.Idle); | ||||
|     } | ||||
|  | ||||
|       ConfigurationSection targetConfig = mapConfig.getConfigurationSection("target"); | ||||
|       double x = targetConfig.getDouble("x"); | ||||
|       double y = targetConfig.getDouble("y"); | ||||
|       double z = targetConfig.getDouble("z"); | ||||
|       Waypoint bombTarget = new TestSpawn(x, y, z); | ||||
|       Waypoint[] spawnArray = new Waypoint[spawnpoints.size()]; | ||||
|       spawnpoints.toArray(spawnArray); | ||||
|       Arena arena = new MemoryArena(spawnArray, bombTarget); | ||||
|       m_arenas.put(mapName, arena); | ||||
|     for(Player player : m_playerGames.keySet()) { | ||||
|       m_playerGames.get(player).removePlayer(player); | ||||
|     } | ||||
|     m_runningGames.clear(); | ||||
|     m_playerGames.clear(); | ||||
|     m_handler.clear(); | ||||
|     m_arenas.clear(); | ||||
|     for(String mapName : config.getMapNames()) { | ||||
|       m_arenas.put(mapName, config.getArenaForMap(mapName)); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   /*GameRunner getRunnerForWorld(World world) { | ||||
|     GameRunner ret; | ||||
|     if (m_runningGames.containsKey(world)) { | ||||
|       ret = m_runningGames.get(world); | ||||
|     } else if (m_arenas.containsKey(world)) { | ||||
|       ret = new GameRunner(this, m_games.get(0), m_arenas.get(world.getName()), world); | ||||
|       m_runningGames.put(world, ret); | ||||
|       getServer().getPluginManager().registerEvents(new GameEventHandler(ret), this); | ||||
|     } | ||||
|     return ret; | ||||
|   }*/ | ||||
|  | ||||
|   public boolean hasRunnerForArenaName(String arenaName) { | ||||
|     return m_runningGames.containsKey(arenaName); | ||||
|   } | ||||
| @@ -169,7 +126,7 @@ public class Plugin extends JavaPlugin { | ||||
|         gameWorld = new WorldCreator(arenaName).generateStructures(false).createWorld(); | ||||
|       } | ||||
|       ret = new GameRunner(this, m_games.get(0), m_arenas.get(arenaName), gameWorld); | ||||
|       getServer().getPluginManager().registerEvents(new GameEventHandler(ret), this); | ||||
|       m_handler.addRunner(ret); | ||||
|       m_runningGames.put(arenaName, ret); | ||||
|       getLogger().info("Game ready for " + arenaName); | ||||
|     } | ||||
| @@ -183,18 +140,4 @@ public class Plugin extends JavaPlugin { | ||||
|     } | ||||
|     return ret; | ||||
|   } | ||||
|  | ||||
|   void setupDemoGame() { | ||||
|     getLogger().info("Setting up demo data'"); | ||||
|     World testWorld = getServer().getWorld("quarry"); | ||||
|     if (testWorld == null) { | ||||
|       testWorld = new WorldCreator("quarry").generateStructures(false).createWorld(); | ||||
|     } | ||||
|     Waypoint[] spawnpoints = new Waypoint[3]; | ||||
|     spawnpoints[0] = new TestSpawn(-15, 80, -46); | ||||
|     spawnpoints[1] = new TestSpawn(-1, 80, -45); | ||||
|     spawnpoints[2] = new TestSpawn(12, 81, -42); | ||||
|     Waypoint bombTarget = new TestSpawn(-20, 80, 31); | ||||
|     m_arenas.put("quarry", new MemoryArena(spawnpoints, bombTarget)); | ||||
|   } | ||||
| } | ||||
|   | ||||
							
								
								
									
										22
									
								
								src/main/java/gg/malloc/defense/commands/ReloadCommand.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								src/main/java/gg/malloc/defense/commands/ReloadCommand.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | ||||
| package gg.malloc.defense.commands; | ||||
|  | ||||
| import gg.malloc.defense.Plugin; | ||||
|  | ||||
| import org.bukkit.command.Command; | ||||
| import org.bukkit.command.CommandExecutor; | ||||
| import org.bukkit.command.CommandSender; | ||||
|  | ||||
| public class ReloadCommand implements CommandExecutor { | ||||
|   Plugin m_plugin; | ||||
|  | ||||
|   public ReloadCommand(Plugin plugin) { | ||||
|     m_plugin = plugin; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public boolean onCommand(CommandSender sender, Command command, String s, String[] args) { | ||||
|     sender.sendMessage("Reloading arenas..."); | ||||
|     m_plugin.reloadArenas(); | ||||
|     return true; | ||||
|   } | ||||
| } | ||||
							
								
								
									
										52
									
								
								src/main/java/gg/malloc/defense/config/Configuration.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								src/main/java/gg/malloc/defense/config/Configuration.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,52 @@ | ||||
| package gg.malloc.defense.config; | ||||
|  | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Collection; | ||||
|  | ||||
| import gg.malloc.defense.model.Arena; | ||||
| import gg.malloc.defense.model.Waypoint; | ||||
|  | ||||
| import org.bukkit.configuration.ConfigurationSection; | ||||
|  | ||||
| public class Configuration { | ||||
|  | ||||
|   ConfigurationSection m_root; | ||||
|  | ||||
|   public Configuration(ConfigurationSection rootConfig) { | ||||
|     m_root = rootConfig; | ||||
|   } | ||||
|  | ||||
|   public Collection<String> getMapNames() { | ||||
|     ConfigurationSection mapList = m_root.getConfigurationSection("maps"); | ||||
|     return mapList.getKeys(false); | ||||
|   } | ||||
|  | ||||
|   public Arena getArenaForMap(String name) { | ||||
|     ConfigurationSection mapList = m_root.getConfigurationSection("maps"); | ||||
|     return makeArena(mapList.getConfigurationSection(name)); | ||||
|   } | ||||
|  | ||||
|   MemoryArena makeArena(ConfigurationSection mapConfig) { | ||||
|     List<Map<?, ?>> spawnpointList = mapConfig.getMapList("spawnpoints"); | ||||
|     ArrayList<Waypoint> spawnpoints = new ArrayList<>(); | ||||
|  | ||||
|     for(Map<?, ?> spawnerObj : spawnpointList) { | ||||
|       Map<String, Double> thisSpawner = (Map<String, Double>)spawnerObj; | ||||
|       double x = thisSpawner.get("x"); | ||||
|       double y = thisSpawner.get("y"); | ||||
|       double z = thisSpawner.get("z"); | ||||
|       spawnpoints.add(new Waypoint(x, y, z)); | ||||
|     } | ||||
|  | ||||
|     ConfigurationSection targetConfig = mapConfig.getConfigurationSection("target"); | ||||
|     double x = targetConfig.getDouble("x"); | ||||
|     double y = targetConfig.getDouble("y"); | ||||
|     double z = targetConfig.getDouble("z"); | ||||
|     Waypoint bombTarget = new Waypoint(x, y, z); | ||||
|     Waypoint[] spawnArray = new Waypoint[spawnpoints.size()]; | ||||
|     spawnpoints.toArray(spawnArray); | ||||
|     return new MemoryArena(spawnArray, bombTarget); | ||||
|   } | ||||
| } | ||||
| @@ -1,9 +1,9 @@ | ||||
| package gg.malloc.defense; | ||||
| package gg.malloc.defense.config; | ||||
| 
 | ||||
| import gg.malloc.defense.model.Arena; | ||||
| import gg.malloc.defense.model.Waypoint; | ||||
| 
 | ||||
| import org.bukkit.World; | ||||
| import org.bukkit.configuration.ConfigurationSection; | ||||
| 
 | ||||
| public class MemoryArena implements Arena { | ||||
| 
 | ||||
| @@ -1,8 +1,18 @@ | ||||
| package gg.malloc.defense.model; | ||||
|  | ||||
| public interface Waypoint { | ||||
|   double getX(); | ||||
|   double getY(); | ||||
|   double getZ(); | ||||
|   String getName(); | ||||
| public class Waypoint { | ||||
|   double m_x; | ||||
|   double m_y; | ||||
|   double m_z; | ||||
|   String m_name; | ||||
|  | ||||
|   public Waypoint(double x, double y, double z) { | ||||
|     m_x = x; | ||||
|     m_y = y; | ||||
|     m_z = z; | ||||
|   } | ||||
|  | ||||
|   public double getX() { return m_x; } | ||||
|   public double getY() { return m_y; } | ||||
|   public double getZ() { return m_z; } | ||||
| } | ||||
|   | ||||
| @@ -12,6 +12,8 @@ commands: | ||||
|     description: Join a game | ||||
|   leave: | ||||
|     description: Leave a game | ||||
|   reload: | ||||
|     description: Reload the configuration | ||||
|   setstage: | ||||
|     description: Sets a game stage | ||||
|   addplayer: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user