Initial commit

This commit is contained in:
Torrie Fischer 2022-05-08 01:52:25 +02:00
commit d2c12b4792
16 changed files with 686 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
test-server
*.swp
target

42
pom.xml Normal file
View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>gg.malloc.defense</groupId>
<artifactId>malloc-defense</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>codemc-repo</id>
<url>https://repo.codemc.org/repository/maven-public/</url>
<layout>default</layout>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.18.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,19 @@
package gg.malloc.defense;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class DebuginfoCommand implements CommandExecutor {
Plugin m_plugin;
public DebuginfoCommand(Plugin plugin) {
m_plugin = plugin;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
m_plugin.debuginfo();
return true;
}
}

View File

@ -0,0 +1,46 @@
package gg.malloc.defense;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.PlayerQuitEvent;
import org.bukkit.event.entity.EntityCombustEvent;
import org.bukkit.entity.Player;
public class GameEventHandler implements Listener {
Plugin m_plugin;
public GameEventHandler(Plugin plugin) {
m_plugin = plugin;
}
@EventHandler
public void onEntityDeath(EntityDeathEvent evt) {
GameRunner runner = m_plugin.getRunnerForWorld(evt.getEntity().getLocation().getWorld());
runner.handleEntityDeath(evt.getEntity());
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent evt) {
GameRunner runner = m_plugin.getRunnerForWorld(evt.getPlayer().getLocation().getWorld());
runner.removePlayer(evt.getEntity());
}
@EventHandler
public void onEntityCombust(EntityCombustEvent evt) {
evt.setCancelled(true);
}
@EventHandler
public void onEntityDamage(EntityDamageEvent evt) {
if (evt.getEntity() instanceof Player) {
Player player = (Player)evt.getEntity();
if (player.getHealth() - evt.getFinalDamage() <= 0) {
GameRunner runner = m_plugin.getRunnerForWorld(evt.getEntity().getLocation().getWorld());
evt.setCancelled(true);
runner.handlePlayerDeath(player);
}
}
}
}

View File

@ -0,0 +1,294 @@
package gg.malloc.defense;
import gg.malloc.defense.model.Game;
import gg.malloc.defense.model.Arena;
import gg.malloc.defense.model.Spawnpoint;
import gg.malloc.defense.model.Spawner;
import gg.malloc.defense.model.Wave;
import java.util.ArrayList;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.GameMode;
import org.bukkit.attribute.Attribute;
public class GameRunner {
ArrayList<Entity> m_spawnedMobs = new ArrayList<Entity>();
int m_killedMobs = 0;
Arena m_arena;
Game m_game;
State m_state;
ArrayList<Player> m_players = new ArrayList<>();
ArrayList<Player> m_livingPlayers = new ArrayList<>();
Plugin m_plugin;
BossBar m_gameBar = Bukkit.createBossBar("Malloc Defense", BarColor.PURPLE, BarStyle.SOLID);
BukkitTask m_countdownTask;
int m_currentWaveNum = 0;
int m_currentBatch = 0;
Wave m_currentWave = null;
enum State {
Idle,
Warmup,
Playing,
GameOver
}
public GameRunner(Plugin plugin, Game game, Arena arena) {
m_plugin = plugin;
m_game = game;
m_arena = arena;
m_state = State.Idle;
m_gameBar.setVisible(true);
}
int m_warmupCountdown = 0;
private void countdownTick() {
if (m_warmupCountdown == 0) {
requestTransition(State.Playing);
} else {
m_gameBar.setProgress((double)m_warmupCountdown / (double)30);
broadcastMessage("Starting game in " + m_warmupCountdown);
m_warmupCountdown--;
m_countdownTask = m_plugin.getServer().getScheduler().runTaskLater(m_plugin, () -> {
countdownTick();
}, 20);
}
}
private void clearMobs() {
for(Entity e : m_spawnedMobs) {
e.remove();
}
m_spawnedMobs.clear();
}
private boolean enterIdle() {
broadcastMessage("Game state: Idle");
m_currentWaveNum = 0;
m_gameBar.setColor(BarColor.PURPLE);
m_gameBar.setProgress(1.0);
m_gameBar.setTitle("Idle");
if (m_countdownTask != null) {
m_countdownTask.cancel();
m_countdownTask = null;
}
clearMobs();
return true;
}
private boolean enterWarmup() {
broadcastMessage("Game state: Warmup");
m_currentWaveNum += 1;
m_currentWave = m_game.getWave(m_currentWaveNum);
m_gameBar.setColor(BarColor.YELLOW);
m_gameBar.setProgress(1.0);
m_gameBar.setTitle("Warmup");
m_warmupCountdown = 30;
for(Player p : m_players) {
m_livingPlayers.add(p);
}
clearMobs();
countdownTick();
return true;
}
private boolean enterPlaying() {
broadcastMessage("Game state: Playing");
broadcastMessage("Starting wave " + m_currentWaveNum);
m_currentBatch = 1;
m_gameBar.setColor(BarColor.GREEN);
m_gameBar.setProgress(0.0);
m_gameBar.setTitle("Playing");
spawnNextBatch();
return true;
}
private boolean enterGameOver() {
broadcastMessage("Game state: Game Over!");
m_gameBar.setColor(BarColor.RED);
m_gameBar.setProgress(1.0);
m_gameBar.setTitle("Game Over!");
if (m_countdownTask != null) {
m_countdownTask.cancel();
m_countdownTask = null;
}
for(Player p : m_players) {
p.setGameMode(GameMode.ADVENTURE);
}
clearMobs();
return true;
}
private void updateMobBar() {
m_gameBar.setTitle("Mobs remaining: " + (m_currentWave.totalMobCount() - m_killedMobs));
m_gameBar.setProgress(m_killedMobs / m_currentWave.totalMobCount());
}
private void spawnNextBatch() {
broadcastMessage("Spawning batch " + m_currentBatch);
Spawner spawner = new GameSpawner(m_arena.spawnpoints()[0]);
m_currentWave.spawnBatch(spawner, m_currentBatch);
updateMobBar();
}
public void handlePlayerDeath(Player player) {
if (m_livingPlayers.contains(player)) {
m_livingPlayers.remove(player);
player.setGameMode(GameMode.SPECTATOR);
if (m_livingPlayers.size() == 0) {
broadcastMessage("Everyone is dead :(");
requestTransition(State.GameOver);
}
}
}
public void handleEntityDeath(Entity entity) {
if (m_spawnedMobs.contains(entity)) {
broadcastMessage("Killed game entity " + entity);
m_spawnedMobs.remove(entity);
m_killedMobs += 1;
updateMobBar();
if (m_spawnedMobs.size() == 0) {
broadcastMessage("Batch complete!");
if (m_currentBatch >= m_currentWave.batchCount()) {
if (m_currentWaveNum >= m_game.getWaveCount()) {
requestTransition(State.GameOver);
} else {
requestTransition(State.Warmup);
}
} else {
m_currentBatch += 1;
spawnNextBatch();
}
} else {
broadcastMessage("Entities remaining: " + m_spawnedMobs.size());
}
}
}
private boolean syncPlayer(Player player) {
World playerWorld = player.getLocation().getWorld();
World gameWorld = m_arena.getWorld();
m_gameBar.addPlayer(player);
if (m_livingPlayers.contains(player)) {
player.setGameMode(GameMode.ADVENTURE);
player.setHealth(player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue());
} else {
player.setGameMode(GameMode.SPECTATOR);
}
if (playerWorld != gameWorld) {
return player.teleport(gameWorld.getSpawnLocation(), PlayerTeleportEvent.TeleportCause.PLUGIN);
} else {
return false;
}
}
private boolean attemptTransition(State state) {
if (m_state == state) {
return false;
}
switch(state) {
case Idle:
return enterIdle();
case Warmup:
return enterWarmup();
case Playing:
return enterPlaying();
case GameOver:
return enterGameOver();
}
return false;
}
private boolean validateTransition(State from, State to) {
switch(from) {
case Idle:
return to == State.Warmup;
case Warmup:
return to == State.Playing || to == State.Idle || to == State.GameOver;
case Playing:
return to == State.Warmup || to == State.GameOver || to == State.Idle;
case GameOver:
return to == State.Idle;
}
return false;
}
public boolean requestTransition(State state) {
if (!validateTransition(m_state, state)) {
return false;
}
if (attemptTransition(state)) {
m_state = state;
for(Player p : m_players) {
syncPlayer(p);
}
return true;
}
return false;
}
public boolean addPlayer(Player p) {
if (m_state == State.Idle || m_state == State.Warmup) {
m_players.add(p);
broadcastMessage("Added player " + p + " to game");
syncPlayer(p);
if (m_state == State.Idle) {
requestTransition(State.Warmup);
}
return true;
} else {
return false;
}
}
public void removePlayer(Player p) {
m_gameBar.removePlayer(p);
m_players.remove(p);
m_livingPlayers.remove(p);
if (m_players.size() == 0) {
requestTransition(State.Idle);
}
}
void broadcastMessage(String string) {
World world = m_arena.getWorld();
for(Player p : world.getPlayers()) {
p.sendMessage(string);
}
}
void registerSpawnedMob(Entity entity) {
m_spawnedMobs.add(entity);
}
private class GameSpawner implements Spawner {
Spawnpoint m_spawnpoint;
public GameSpawner(Spawnpoint spawnpoint) {
m_spawnpoint = spawnpoint;
}
@Override
public Entity spawnMob(EntityType type) {
Entity newMob = m_arena.getWorld().spawnEntity(m_spawnpoint.getLocation(), type);
LivingEntity livingMob = (LivingEntity)newMob;
livingMob.setRemoveWhenFarAway(false);
registerSpawnedMob(newMob);
return newMob;
}
}
}

View File

@ -0,0 +1,23 @@
package gg.malloc.defense;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class InitGameCommand implements CommandExecutor {
Plugin m_plugin;
public InitGameCommand(Plugin plugin) {
m_plugin = plugin;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
String worldName = args[0];
GameRunner runner = m_plugin.getRunnerForWorld(m_plugin.getServer().getWorld(worldName));
Player player= m_plugin.getServer().getPlayer(args[1]);
runner.addPlayer(player);
return true;
}
}

View File

@ -0,0 +1,34 @@
package gg.malloc.defense;
import gg.malloc.defense.model.Arena;
import gg.malloc.defense.model.Spawnpoint;
import org.bukkit.World;
public class MemoryArena implements Arena {
Spawnpoint[] m_spawnpoints;
World m_world;
String m_name;
public MemoryArena(String name, World world, Spawnpoint[] spawnpoints) {
m_world = world;
m_spawnpoints = spawnpoints;
m_name = name;
}
@Override
public String name() {
return m_name;
}
@Override
public Spawnpoint[] spawnpoints() {
return m_spawnpoints;
}
@Override
public World getWorld() {
return m_world;
}
}

View File

@ -0,0 +1,82 @@
package gg.malloc.defense;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.World;
import org.bukkit.plugin.PluginLogger;
import java.util.ArrayList;
import java.util.HashMap;
import gg.malloc.defense.model.Arena;
import gg.malloc.defense.model.Spawnpoint;
import gg.malloc.defense.model.Game;
import gg.malloc.defense.games.LinearGame;
public class Plugin extends JavaPlugin {
ArrayList<Arena> m_arenas = new ArrayList<>();
ArrayList<Game> m_games = new ArrayList<>();
HashMap<World, GameRunner> m_runningGames = new HashMap<>();
private class TestSpawn implements Spawnpoint {
Location m_location;
public TestSpawn(Location location) {
m_location = location;
}
@Override
public Location getLocation() {
return m_location;
}
@Override
public String getName() {
return "Mob Spawner";
}
@Override
public String getID() {
return "mob-spawner";
}
}
PluginLogger m_log = new PluginLogger(this);
public void debuginfo() {
m_log.info("Debug Info:");
}
@Override
public void onEnable() {
getLogger().info("Malloc Defense registered");
setupTestGame();
getCommand("setstage").setExecutor(new SetStageCommand(this));
getCommand("initgame").setExecutor(new InitGameCommand(this));
getCommand("debuginfo").setExecutor(new DebuginfoCommand(this));
getServer().getPluginManager().registerEvents(new GameEventHandler(this), this);
}
public GameRunner getRunnerForWorld(World world) {
GameRunner ret;
if (m_runningGames.containsKey(world)) {
ret = m_runningGames.get(world);
} else {
ret = new GameRunner(this, m_games.get(0), m_arenas.get(0));
m_runningGames.put(world, ret);
}
return ret;
}
void setupTestGame() {
World testWorld = getServer().getWorld("world");
Spawnpoint[] spawnpoints = new Spawnpoint[1];
spawnpoints[0] = new TestSpawn(testWorld.getSpawnLocation());
m_arenas.add(new MemoryArena("Test Arena", testWorld, spawnpoints));
m_games.add(new LinearGame());
}
}

View File

@ -0,0 +1,38 @@
package gg.malloc.defense;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.World;
public class SetStageCommand implements CommandExecutor {
Plugin m_plugin;
public SetStageCommand(Plugin plugin) {
m_plugin = plugin;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
World world = m_plugin.getServer().getWorld(args[1]);
GameRunner runner = m_plugin.getRunnerForWorld(world);
String stateName = args[0].toLowerCase();
boolean ret = false;
if (stateName.equals("idle")) {
ret = runner.requestTransition(GameRunner.State.Idle);
} else if (stateName.equals("warmup")) {
ret = runner.requestTransition(GameRunner.State.Warmup);
} else if (stateName.equals("playing")) {
ret = runner.requestTransition(GameRunner.State.Playing);
} else if (stateName.equals("gameover")) {
ret = runner.requestTransition(GameRunner.State.GameOver);
} else {
sender.sendMessage("Unknown state " + stateName);
return false;
}
if (!ret) {
sender.sendMessage("Could not set state to " + stateName);
}
return ret;
}
}

View File

@ -0,0 +1,54 @@
package gg.malloc.defense.games;
import gg.malloc.defense.model.Wave;
import gg.malloc.defense.model.Game;
import gg.malloc.defense.model.Spawner;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
public class LinearGame implements Game {
private class ZombieWave implements Wave {
int m_batches;
int m_zombiesPerBatch;
public ZombieWave(int zombiesPerBatch, int batches) {
m_batches = batches;
m_zombiesPerBatch = zombiesPerBatch;
}
@Override
public int totalMobCount() {
int ret = 0;
for(int i = 1; i <= m_batches; i++) {
ret += i * m_zombiesPerBatch;
}
return ret;
}
@Override
public int batchCount() {
return m_batches;
}
@Override
public void spawnBatch(Spawner spawner, int batch) {
int zombiesToSpawn = batch * m_zombiesPerBatch;
for(int i = 0; i < zombiesToSpawn; i++) {
Entity newMob = spawner.spawnMob(EntityType.ZOMBIE);
newMob.setCustomName("Zombie " + i + "/" + zombiesToSpawn);
}
}
}
@Override
public int getWaveCount() {
return 10;
}
@Override
public Wave getWave(int waveNumber) {
return new ZombieWave(waveNumber, 2);
}
}

View File

@ -0,0 +1,9 @@
package gg.malloc.defense.model;
import org.bukkit.World;
public interface Arena {
World getWorld();
String name();
Spawnpoint[] spawnpoints();
}

View File

@ -0,0 +1,6 @@
package gg.malloc.defense.model;
public interface Game {
int getWaveCount();
Wave getWave(int waveNumber);
}

View File

@ -0,0 +1,8 @@
package gg.malloc.defense.model;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Entity;
public interface Spawner {
Entity spawnMob(EntityType type);
}

View File

@ -0,0 +1,9 @@
package gg.malloc.defense.model;
import org.bukkit.Location;
public interface Spawnpoint {
Location getLocation();
String getName();
String getID();
}

View File

@ -0,0 +1,7 @@
package gg.malloc.defense.model;
public interface Wave {
int batchCount();
int totalMobCount();
void spawnBatch(Spawner spawner, int batch);
}

View File

@ -0,0 +1,12 @@
name: Malloc-Defense
version: 1.0
api-version: 1.18
main: gg.malloc.defense.Plugin
depend: []
commands:
setstage:
description: Sets a game stage
initgame:
description: Adds a player to a game
debuginfo:
description: Unknowable powers