implement config re-loading

This commit is contained in:
Torrie Fischer 2022-05-11 20:14:10 +02:00
parent a4b110773e
commit 793b25251c
9 changed files with 145 additions and 96 deletions

View File

@ -9,13 +9,14 @@
# Malloc Beta # Malloc Beta
[X] One arena config [X] One arena config
[ ] Config reload [X] Config reload
[X] Leave games [X] Leave games
[X] Join games [X] Join games
[ ] Lobby with instructions [ ] Lobby with instructions
[ ] Drop back to lobby on game over [ ] Drop back to lobby on game over
[ ] Grist drops [ ] Grist drops
[ ] Item shoppes [ ] Item shoppes
[ ] Command tab completion
# Scaled waves # Scaled waves

View File

@ -2,6 +2,9 @@ package gg.malloc.defense;
import gg.malloc.defense.engine.GameRunner; import gg.malloc.defense.engine.GameRunner;
import java.util.Collection;
import java.util.HashSet;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.event.entity.EntityDeathEvent;
@ -12,20 +15,32 @@ import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class GameEventHandler implements Listener { public class GameEventHandler implements Listener {
GameRunner m_runner; Collection<GameRunner> m_runners;
public GameEventHandler(GameRunner runner) { public GameEventHandler() {
m_runner = runner; m_runners = new HashSet<GameRunner>();
}
public void addRunner(GameRunner runner) {
m_runners.add(runner);
}
public void clear() {
m_runners.clear();
} }
@EventHandler @EventHandler
public void onEntityDeath(EntityDeathEvent evt) { public void onEntityDeath(EntityDeathEvent evt) {
m_runner.handleEntityDeath(evt.getEntity()); for(GameRunner r : m_runners) {
r.handleEntityDeath(evt.getEntity());
}
} }
@EventHandler @EventHandler
public void onPlayerQuit(PlayerQuitEvent evt) { public void onPlayerQuit(PlayerQuitEvent evt) {
m_runner.removePlayer(evt.getPlayer()); for(GameRunner r : m_runners) {
r.removePlayer(evt.getPlayer());
}
} }
@EventHandler @EventHandler
@ -35,11 +50,15 @@ public class GameEventHandler implements Listener {
@EventHandler @EventHandler
public void onEntityTarget(EntityTargetEvent evt) { public void onEntityTarget(EntityTargetEvent evt) {
m_runner.handleEntityRetargeting(evt); for(GameRunner r : m_runners) {
r.handleEntityRetargeting(evt);
}
} }
@EventHandler @EventHandler
public void onEntityDamage(EntityDamageEvent evt) { public void onEntityDamage(EntityDamageEvent evt) {
m_runner.handleEntityDamage(evt); for(GameRunner r : m_runners) {
r.handleEntityDamage(evt);
}
} }
} }

View File

@ -6,7 +6,6 @@ import org.bukkit.Location;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.WorldCreator; import org.bukkit.WorldCreator;
@ -25,7 +24,6 @@ import java.util.Map;
import java.util.Collection; import java.util.Collection;
import gg.malloc.defense.model.Arena; import gg.malloc.defense.model.Arena;
import gg.malloc.defense.model.Waypoint;
import gg.malloc.defense.model.Game; import gg.malloc.defense.model.Game;
import gg.malloc.defense.games.LinearGame; 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.engine.GameRunner;
import gg.malloc.defense.config.Configuration;
import gg.malloc.defense.commands.AddPlayerCommand; import gg.malloc.defense.commands.AddPlayerCommand;
import gg.malloc.defense.commands.SetStageCommand; import gg.malloc.defense.commands.SetStageCommand;
import gg.malloc.defense.commands.PlayerReadyCommand; import gg.malloc.defense.commands.PlayerReadyCommand;
import gg.malloc.defense.commands.ListGamesCommand; import gg.malloc.defense.commands.ListGamesCommand;
import gg.malloc.defense.commands.JoinGameCommand; import gg.malloc.defense.commands.JoinGameCommand;
import gg.malloc.defense.commands.LeaveGameCommand; import gg.malloc.defense.commands.LeaveGameCommand;
import gg.malloc.defense.commands.ReloadCommand;
public class Plugin extends JavaPlugin { public class Plugin extends JavaPlugin {
HashMap<String, Arena> m_arenas = new HashMap<>(); HashMap<String, Arena> m_arenas = new HashMap<>();
ArrayList<Game> m_games = new ArrayList<>(); ArrayList<Game> m_games = new ArrayList<>();
HashMap<String, GameRunner> m_runningGames = new HashMap<>(); HashMap<String, GameRunner> m_runningGames = new HashMap<>();
HashMap<Player, GameRunner> m_playerGames = new HashMap<>(); HashMap<Player, GameRunner> m_playerGames = new HashMap<>();
GameEventHandler m_handler = new GameEventHandler();
public Collection<String> arenaNames() { public Collection<String> arenaNames() {
return m_arenas.keySet(); 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() { public void debuginfo() {
getLogger().info("Debug Info:"); getLogger().info("Debug Info:");
} }
@ -81,9 +60,8 @@ public class Plugin extends JavaPlugin {
public void onEnable() { public void onEnable() {
getLogger().info("Malloc Defense registered"); getLogger().info("Malloc Defense registered");
getLogger().setLevel(Level.FINEST); getLogger().setLevel(Level.FINEST);
//setupDemoGame();
m_games.add(new ScaledWaves()); m_games.add(new ScaledWaves());
loadArenas(); reloadArenas();
getCommand("join").setExecutor(new JoinGameCommand(this)); getCommand("join").setExecutor(new JoinGameCommand(this));
getCommand("leave").setExecutor(new LeaveGameCommand(this)); getCommand("leave").setExecutor(new LeaveGameCommand(this));
getCommand("ready").setExecutor(new PlayerReadyCommand(this)); getCommand("ready").setExecutor(new PlayerReadyCommand(this));
@ -92,54 +70,33 @@ public class Plugin extends JavaPlugin {
getCommand("addplayer").setExecutor(new AddPlayerCommand(this)); getCommand("addplayer").setExecutor(new AddPlayerCommand(this));
getCommand("setstage").setExecutor(new SetStageCommand(this)); getCommand("setstage").setExecutor(new SetStageCommand(this));
getCommand("debuginfo").setExecutor(new DebuginfoCommand(this)); getCommand("debuginfo").setExecutor(new DebuginfoCommand(this));
getCommand("reload").setExecutor(new ReloadCommand(this));
getServer().getPluginManager().registerEvents(new PlayerQuitHandler(), this); getServer().getPluginManager().registerEvents(new PlayerQuitHandler(), this);
getServer().getPluginManager().registerEvents(m_handler, this);
} }
void loadArenas() { public void reloadArenas() {
getLogger().info("Loading arenas..."); getLogger().info("Loading arenas...");
saveDefaultConfig(); saveDefaultConfig();
ConfigurationSection pluginConfig = getConfig(); reloadConfig();
ConfigurationSection mapList = pluginConfig.getConfigurationSection("maps"); Configuration config = new Configuration(getConfig());
for(String mapName : mapList.getKeys(false)) { for(GameRunner runner : m_runningGames.values()) {
getLogger().info("Loading arena: " + mapName); runner.requestTransition(GameRunner.Stage.Idle);
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(Player player : m_playerGames.keySet()) {
ConfigurationSection targetConfig = mapConfig.getConfigurationSection("target"); m_playerGames.get(player).removePlayer(player);
double x = targetConfig.getDouble("x"); }
double y = targetConfig.getDouble("y"); m_runningGames.clear();
double z = targetConfig.getDouble("z"); m_playerGames.clear();
Waypoint bombTarget = new TestSpawn(x, y, z); m_handler.clear();
Waypoint[] spawnArray = new Waypoint[spawnpoints.size()]; m_arenas.clear();
spawnpoints.toArray(spawnArray); for(String mapName : config.getMapNames()) {
Arena arena = new MemoryArena(spawnArray, bombTarget); m_arenas.put(mapName, config.getArenaForMap(mapName));
m_arenas.put(mapName, arena);
} }
} }
/*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) { public boolean hasRunnerForArenaName(String arenaName) {
return m_runningGames.containsKey(arenaName); return m_runningGames.containsKey(arenaName);
} }
@ -169,7 +126,7 @@ public class Plugin extends JavaPlugin {
gameWorld = new WorldCreator(arenaName).generateStructures(false).createWorld(); gameWorld = new WorldCreator(arenaName).generateStructures(false).createWorld();
} }
ret = new GameRunner(this, m_games.get(0), m_arenas.get(arenaName), gameWorld); 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); m_runningGames.put(arenaName, ret);
getLogger().info("Game ready for " + arenaName); getLogger().info("Game ready for " + arenaName);
} }
@ -183,18 +140,4 @@ public class Plugin extends JavaPlugin {
} }
return ret; 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));
}
} }

View 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;
}
}

View 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);
}
}

View File

@ -1,9 +1,9 @@
package gg.malloc.defense; package gg.malloc.defense.config;
import gg.malloc.defense.model.Arena; import gg.malloc.defense.model.Arena;
import gg.malloc.defense.model.Waypoint; import gg.malloc.defense.model.Waypoint;
import org.bukkit.World; import org.bukkit.configuration.ConfigurationSection;
public class MemoryArena implements Arena { public class MemoryArena implements Arena {

View File

@ -1,8 +1,18 @@
package gg.malloc.defense.model; package gg.malloc.defense.model;
public interface Waypoint { public class Waypoint {
double getX(); double m_x;
double getY(); double m_y;
double getZ(); double m_z;
String getName(); 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; }
} }

View File

@ -12,6 +12,8 @@ commands:
description: Join a game description: Join a game
leave: leave:
description: Leave a game description: Leave a game
reload:
description: Reload the configuration
setstage: setstage:
description: Sets a game stage description: Sets a game stage
addplayer: addplayer: