diff --git a/TODO.md b/TODO.md index fd712ab..ffb1edd 100644 --- a/TODO.md +++ b/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 diff --git a/src/main/java/gg/malloc/defense/GameEventHandler.java b/src/main/java/gg/malloc/defense/GameEventHandler.java index bb817d2..a20be7e 100644 --- a/src/main/java/gg/malloc/defense/GameEventHandler.java +++ b/src/main/java/gg/malloc/defense/GameEventHandler.java @@ -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 m_runners; - public GameEventHandler(GameRunner runner) { - m_runner = runner; + public GameEventHandler() { + m_runners = new HashSet(); + } + + 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); + } } } diff --git a/src/main/java/gg/malloc/defense/Plugin.java b/src/main/java/gg/malloc/defense/Plugin.java index 6ba9102..20b4657 100644 --- a/src/main/java/gg/malloc/defense/Plugin.java +++ b/src/main/java/gg/malloc/defense/Plugin.java @@ -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 m_arenas = new HashMap<>(); ArrayList m_games = new ArrayList<>(); HashMap m_runningGames = new HashMap<>(); HashMap m_playerGames = new HashMap<>(); + GameEventHandler m_handler = new GameEventHandler(); public Collection 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> spawnpointList = mapConfig.getMapList("spawnpoints"); - ArrayList spawnpoints = new ArrayList<>(); - - for(Map spawnerObj : spawnpointList) { - Map thisSpawner = (Map)spawnerObj; - double x = thisSpawner.get("x"); - double y = thisSpawner.get("y"); - double z = thisSpawner.get("z"); - spawnpoints.add(new TestSpawn(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 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(GameRunner runner : m_runningGames.values()) { + runner.requestTransition(GameRunner.Stage.Idle); + } + 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)); - } } diff --git a/src/main/java/gg/malloc/defense/commands/ReloadCommand.java b/src/main/java/gg/malloc/defense/commands/ReloadCommand.java new file mode 100644 index 0000000..ff82cb5 --- /dev/null +++ b/src/main/java/gg/malloc/defense/commands/ReloadCommand.java @@ -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; + } +} diff --git a/src/main/java/gg/malloc/defense/config/Configuration.java b/src/main/java/gg/malloc/defense/config/Configuration.java new file mode 100644 index 0000000..50adbed --- /dev/null +++ b/src/main/java/gg/malloc/defense/config/Configuration.java @@ -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 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> spawnpointList = mapConfig.getMapList("spawnpoints"); + ArrayList spawnpoints = new ArrayList<>(); + + for(Map spawnerObj : spawnpointList) { + Map thisSpawner = (Map)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); + } +} diff --git a/src/main/java/gg/malloc/defense/MemoryArena.java b/src/main/java/gg/malloc/defense/config/MemoryArena.java similarity index 84% rename from src/main/java/gg/malloc/defense/MemoryArena.java rename to src/main/java/gg/malloc/defense/config/MemoryArena.java index 1269ebc..768fc0f 100644 --- a/src/main/java/gg/malloc/defense/MemoryArena.java +++ b/src/main/java/gg/malloc/defense/config/MemoryArena.java @@ -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 { diff --git a/src/main/java/gg/malloc/defense/engine/GameRunner.java b/src/main/java/gg/malloc/defense/engine/GameRunner.java index 4d8fd74..0aac99a 100644 --- a/src/main/java/gg/malloc/defense/engine/GameRunner.java +++ b/src/main/java/gg/malloc/defense/engine/GameRunner.java @@ -283,7 +283,7 @@ public class GameRunner { case Idle: return !m_players.isEmpty() && to == Stage.Warmup; case Warmup: - return to == Stage.Playing || to == Stage.Idle || to == Stage.Countdown ; + return to == Stage.Playing || to == Stage.Idle || to == Stage.Countdown; case Countdown: return to == Stage.Playing || to == Stage.Idle || to == Stage.Warmup; case Playing: diff --git a/src/main/java/gg/malloc/defense/model/Waypoint.java b/src/main/java/gg/malloc/defense/model/Waypoint.java index eda052f..ae92f35 100644 --- a/src/main/java/gg/malloc/defense/model/Waypoint.java +++ b/src/main/java/gg/malloc/defense/model/Waypoint.java @@ -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; } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index a89a5bf..2e34a08 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -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: