implemented major logic parts and introduced spawn management

This commit is contained in:
Steev 2022-01-06 01:21:41 +01:00
parent 1f0ff2d5c4
commit 4d2f0d24d1
14 changed files with 314 additions and 28 deletions

View File

@ -14,3 +14,24 @@ You are free to use this plugin on your server but, you have to mention me as th
## Current Roadmap
Because this plugin currently is in its early development phase I will update this to more interesting features down the road
my main goal, for now, is to get a stable plugin to host on servers without issues.
- [X] Getting the Plugin to start
- [X] Player Amount detection works
- [X] Getting the Game to start
- [X] Countdown works
- [X] All players get a trident assigned
- [X] Trident gets properly reset to Thrower (when not dead)
- [X] Error occurs when trident hits ground
- [ ] does not detect if player somehow remains with tridents
- was worked arround by moving players to spectator
- [X] Playerdeath Detection works
- [X] Alive Counter works
- Appears not to track at all
- [X] Game winer routine works
- death detection is nonfunctional
- [X] config gets created
- [X] config gets loaded
- [ ] minimal players get loaded from config
- [X] Writing Spawnpoints into the config works
- [ ] Reading Locations from config works
- [ ] Players will be teleported to the location wanted
- [ ] Player items do not drop on death and get removed

7
config.yml Normal file
View File

@ -0,0 +1,7 @@
# general
prefix: null
minplayers: 2
# spawn points
lobby: null
arena: null

View File

@ -5,10 +5,12 @@ import de.steev.Tridentwar.listeners.PlayerDeathListener;
import de.steev.Tridentwar.listeners.ProjectileHitListener;
import de.steev.Tridentwar.listeners.ProjectileLaunchListener;
import de.steev.Tridentwar.manager.GameManager;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
public class Tridentwar extends JavaPlugin {
private GameManager gameManager;
public FileConfiguration config = this.getConfig();
@Override
public void onEnable(){
@ -18,11 +20,13 @@ public class Tridentwar extends JavaPlugin {
getServer().getPluginManager().registerEvents(new ProjectileHitListener(this.gameManager), this);
getServer().getPluginManager().registerEvents(new PlayerDeathListener(this.gameManager), this);
getCommand("tw").setExecutor(new TridentwarCommand(this.gameManager));
this.saveDefaultConfig();
}
@Override
public void onDisable(){
super.onDisable();
this.saveDefaultConfig();
gameManager.cleanup();
}
}

View File

@ -5,6 +5,7 @@ import de.steev.Tridentwar.manager.GameState;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class TridentwarCommand implements CommandExecutor {
@ -22,9 +23,44 @@ public class TridentwarCommand implements CommandExecutor {
commandSender.sendMessage("use args: start");
break;
case "start":
System.out.println(args[0]);
System.out.println("start thing: " + args[0]);
gameManager.setGameState(GameState.STARTING);
break;
/**
* dev command for returning running gamestate
* @deprecated this command will be removed once state management works properly
*/
case "getstate":
commandSender.sendMessage("Current State: " + gameManager.gameState);
break;
case "setlocation":
System.out.println("loc thing" + args[1]);
if(args.length <= 2){
switch (args[1].toLowerCase()){
default:
commandSender.sendMessage("wrong or undefined location type");
break;
case "lobby":
try {
this.gameManager.setLocation("lobby", ((Player)commandSender).getLocation());
commandSender.sendMessage("lobby set");
}catch (Exception ex) {
commandSender.sendMessage("Error while setting location");
}
break;
case "arena":
try {
this.gameManager.setLocation("arena", ((Player)commandSender).getLocation());
commandSender.sendMessage("lobby set");
}catch (Exception ex) {
commandSender.sendMessage("Error while setting location");
}
break;
}
} else {
commandSender.sendMessage("Empty locations cannot be processed");
}
break;
}
} else {
commandSender.sendMessage("Empty Commands are not allowed!");

View File

@ -1,10 +1,13 @@
package de.steev.Tridentwar.listeners;
import de.steev.Tridentwar.manager.GameManager;
import de.steev.Tridentwar.manager.GameState;
import org.bukkit.GameMode;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
public class PlayerDeathListener implements Listener {
private GameManager gameManager;
@ -12,9 +15,20 @@ public class PlayerDeathListener implements Listener {
public PlayerDeathListener(GameManager gameManager) { this.gameManager = gameManager; }
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event) {
this.gameManager.getPlayerManager().playerDeath();
event.getEntity().setGameMode(GameMode.SPECTATOR);
event.getEntity().sendMessage("Du bist gestorben");
public void onPlayerDamage(EntityDamageByEntityEvent event) {
if(this.gameManager.gameState == GameState.ACTIVE) {
this.gameManager.getPlayerManager().playerDeath();
if(event.getEntity() instanceof Player) {
Player dead = ((Player)event.getEntity());
if(dead.getHealth() < 1) {
dead.getInventory().clear();
dead.setGameMode(GameMode.SPECTATOR);
// proper messaging
dead.sendTitle("Dead", "you Died", 1, 100, 1);
dead.playSound(dead.getLocation(), Sound.ENTITY_ENDER_DRAGON_DEATH, 5,1);
dead.sendMessage("Du bist gestorben");
}
}
}
}
}

View File

@ -1,6 +1,7 @@
package de.steev.Tridentwar.listeners;
import de.steev.Tridentwar.manager.GameManager;
import de.steev.Tridentwar.manager.GameState;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
@ -18,14 +19,16 @@ public class ProjectileHitListener implements Listener {
@EventHandler
public void onTridentHit(ProjectileHitEvent event){
if(this.gameManager.getTridentManager().getTask((Player)event.getEntity().getShooter()) != null) {
this.gameManager.getTridentManager().getTask((Player) event.getEntity().getShooter()).cancel();
this.gameManager.getTridentManager().removeTridentTast((Player) event.getEntity().getShooter());
if (this.gameManager.gameState == GameState.ACTIVE) {
if(this.gameManager.getTridentManager().getTask((Player)event.getEntity().getShooter()) != null) {
this.gameManager.getTridentManager().getTask((Player) event.getEntity().getShooter()).cancel();
this.gameManager.getTridentManager().removeTridentTast((Player) event.getEntity().getShooter());
}
if(event.getHitEntity() instanceof Player) ((Player) event.getEntity().getShooter()).playSound(((Player) event.getEntity().getShooter()).getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1f, 0.5f);
event.getEntity().remove();
((Player) event.getEntity().getShooter()).getInventory().addItem(new ItemStack(Material.TRIDENT));
}
if(event.getHitEntity() instanceof Player) ((Player) event.getEntity().getShooter()).playSound(((Player) event.getEntity().getShooter()).getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1f, 0.5f);
event.getEntity().remove();
((Player) event.getEntity().getShooter()).getInventory().addItem(new ItemStack(Material.TRIDENT));
}
}

View File

@ -2,6 +2,7 @@ package de.steev.Tridentwar.listeners;
import de.steev.Tridentwar.Tridentwar;
import de.steev.Tridentwar.manager.GameManager;
import de.steev.Tridentwar.manager.GameState;
import de.steev.Tridentwar.tasks.TridentResetTask;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -21,8 +22,10 @@ public class ProjectileLaunchListener implements Listener {
@EventHandler
public void OnProjectileLaunch(ProjectileLaunchEvent event){
this.tridentResetTask = new TridentResetTask(this.gameManager, (Player)event.getEntity().getShooter(), event.getEntity());
this.tridentResetTask.runTaskLater(plugin, 1000);
gameManager.getTridentManager().setTasks((Player)event.getEntity().getShooter(), this.tridentResetTask);
if(this.gameManager.gameState == GameState.ACTIVE){
this.tridentResetTask = new TridentResetTask(this.gameManager, (Player)event.getEntity().getShooter(), event.getEntity());
this.tridentResetTask.runTaskLater(plugin, 1000);
gameManager.getTridentManager().setTasks((Player)event.getEntity().getShooter(), this.tridentResetTask);
}
}
}

View File

@ -3,6 +3,10 @@ package de.steev.Tridentwar.manager;
import de.steev.Tridentwar.Tridentwar;
import de.steev.Tridentwar.tasks.GameStartCountdownTask;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
public class GameManager {
private final Tridentwar plugin;
@ -10,14 +14,24 @@ public class GameManager {
private GameStartCountdownTask gameStartCountdownTask;
public GameState gameState = GameState.LOBBY;
private PlayerManager playerManager;
private MessageManager messageManager;
/**
* Handles the entire Game
* @param plugin the local plugin insance
*/
public GameManager(Tridentwar plugin) {
this.plugin = plugin;
this.tridentManager = new TridentManager(this);
this.playerManager = new PlayerManager(this);
this.messageManager = new MessageManager(this);
}
/**
* Sets the Gamestate and decied next gamestep
* @param gameState Gamestate of the current game
*/
public void setGameState(GameState gameState){
if(this.gameState == GameState.ACTIVE && gameState == GameState.STARTING) return;
this.gameState = gameState;
@ -29,7 +43,7 @@ public class GameManager {
this.playerManager.giveKits();
break;
case STARTING:
if(Bukkit.getOnlinePlayers().size() < 2) {
if(Bukkit.getOnlinePlayers().size() < this.plugin.config.getInt("minplayers")) {
// Message about minimal player count not beeing reached
Bukkit.broadcastMessage("Game cannot be started with a single player");
return;
@ -38,22 +52,72 @@ public class GameManager {
this.gameStartCountdownTask = new GameStartCountdownTask(this);
this.gameStartCountdownTask.runTaskTimer(plugin, 0 , 20);
// teleport players
playerManager.teleportPlayers(this.plugin.config.getLocation("arena"));
break;
case LOBBY:
playerManager.teleportPlayers(this.plugin.config.getLocation("lobby"));
break;
case WON:
Bukkit.broadcastMessage("WON");
Bukkit.broadcastMessage("Game has been won");
for (Player p : Bukkit.getOnlinePlayers()) {
if(p.getGameMode() == GameMode.SURVIVAL) {
this.messageManager.broadCastTitle("Game ends", p.getDisplayName() + " has won the game");
}
}
playerManager.teleportPlayers(this.plugin.config.getLocation("lobby"));
setGameState(GameState.STOPPING);
break;
case STOPPING:
this.messageManager.broadCastTitle("Round stops", "You will be moved back to hub");
this.playerManager.removeKits();
// move players back to hub
Bukkit.broadcastMessage("Stopping Game");
setGameState(GameState.LOBBY);
break;
case ABORTING:
Bukkit.broadcastMessage("No Player Alive game aborts");
this.messageManager.broadCastTitle("Round aborting", "You will be moved back to hub");
this.playerManager.removeKits();
// move players back to hub
Bukkit.broadcastMessage("No Player Alive game aborts");
setGameState(GameState.LOBBY);
break;
}
}
public void cleanup(){}
/**
* local trident manager
* @retur tridentManager instance
*/
public TridentManager getTridentManager() { return tridentManager; }
/**
* Local playermanager
* @return playermanager instance
*/
public PlayerManager getPlayerManager() { return playerManager; }
/**
* current gamestate
* @return the current gamestate
*/
public GameState getGameState() { return gameState; }
/**
* Returns plugin instance do not use if not needed!
* @return the plugin
*/
public Tridentwar getPlugin() { return plugin; }
/**
* Sets a specified location
* @param path the path inside the config
* @param loc the location to set
*/
public void setLocation(String path, Location loc) {
System.out.println("Location: " + loc);
plugin.config.set(path, loc);
plugin.saveConfig();
}
}

View File

@ -1,5 +1,5 @@
package de.steev.Tridentwar.manager;
public enum GameState {
LOBBY, STARTING, ACTIVE, WON, RESTARTING, STOPPING, ABORTING;
LOBBY, WAITING, STARTING, ACTIVE, WON, RESTARTING, STOPPING, ABORTING;
}

View File

@ -0,0 +1,27 @@
package de.steev.Tridentwar.manager;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
public class MessageManager {
private GameManager gameManager;
/**
* Handles all message related functions
* @param gameManager the local Gamemanager instance
*/
public MessageManager(GameManager gameManager){
this.gameManager = gameManager;
}
/**
* Broadcasts a title message to all players online
* @param header the Big header of the title
* @param bottom The lower small message of the title
*/
public void broadCastTitle(String header, String bottom) {
for(Player p : Bukkit.getOnlinePlayers()) {
p.sendTitle(header, bottom, 1, 100, 1);
}
}
}

View File

@ -2,32 +2,81 @@ package de.steev.Tridentwar.manager;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public class PlayerManager {
private GameManager gameManager;
/** The amount of players yet alife */
private int alive = 0;
/**
* Handles all player related functions
* @param gameManager The Local Gamemanager instance
*/
public PlayerManager(GameManager gameManager) {
this.gameManager = gameManager;
}
/** Give a kit to all Players in Survivalmode */
public void giveKits() { Bukkit.getOnlinePlayers().stream().filter(player -> player.getGameMode() == GameMode.SURVIVAL).forEach(this::giveKit); }
public void giveKit(Player player){
player.getInventory().addItem(new ItemStack(Material.TRIDENT));
/** Removes a kit from all Players in Survivalmode */
public void removeKits() { Bukkit.getOnlinePlayers().stream().filter(player -> player.getGameMode() == GameMode.SURVIVAL).forEach(this::giveKit); }
/** Gives a specified kit in this case hardcoded to specified players */
public void giveKit(Player player){ player.getInventory().addItem(new ItemStack(Material.TRIDENT)); }
/** Revmoes a specified kit in this case hardcoded from a specified player */
public void removeKit(Player player){ player.getInventory().removeItem(new ItemStack(Material.TRIDENT)); }
/**
* sets all players online to a given gamemode
* @param gamemode the gamemode all players will be set on
*/
public void setGameModes(GameMode gamemode){
for (Player p : Bukkit.getOnlinePlayers()) {
setGameMode(p, gamemode);
}
}
/**
* sets a specified player on a given gamemode
* @param player the player to change gamemode of
* @param gamemode the gamemode wanted to be changed to
*/
public void setGameMode(Player player, GameMode gamemode) { player.setGameMode(gamemode); }
/**
* teleports a player to a location
* @param player the wanted player
* @param loc the wanted location
*/
public void teleportPlayer(Player player, Location loc) { player.teleport(loc); }
/**
* teleports all player to a given location
* @param loc
*/
public void teleportPlayers(Location loc) {
for (Player p : Bukkit.getOnlinePlayers()) {
teleportPlayer(p, loc);
}
}
/** Handles Player death */
public void playerDeath(){
System.out.println("Before death: " + this.alive);
this.alive--;
System.out.println("After death: " + this.alive);
if(this.alive == 1) {
System.out.println("game ends with winner");
this.gameManager.setGameState(GameState.WON);
} else if(this.alive == 0) {
System.out.println("game gets aborted");
this.gameManager.setGameState(GameState.ABORTING);
}
}
/**
* Gets the amout of alive players
* @return Integert amout of alive players
*/
public int getAlive() { return alive; }
/**
* sets the yet alive players
* @param alive amount of yet alive players
*/
public void setAlive(int alive) { this.alive = alive; }
}

View File

@ -0,0 +1,34 @@
package de.steev.Tridentwar.manager;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
public class SoundManager {
private GameManager gameManager;
/**
* Manages all Sound related functions
* @param gameManager the local Gamemanager instance
*/
public SoundManager(GameManager gameManager) {
this.gameManager = gameManager;
}
/**
* Refactored version of Player#playsound
* @param player The targeted player to play the sound
* @param sound The Sound all Players get played
* @param volume How loud the Sound will be
* @param pitch How pitched the sound is
*/
public void playSound (Player player, Sound sound, float volume, float pitch){ player.playSound(player.getLocation(), sound, volume, pitch); }
/**
* Plays defined sounds to all players online
* @param sound The Sound all Players get played
* @param volume How loud the Sound will be
* @param pitch How pitched the sound is
*/
public void broadCastSound (Sound sound, float volume, float pitch) { Bukkit.getOnlinePlayers().forEach(player -> playSound(player,sound, volume, pitch)); }
}

View File

@ -6,11 +6,33 @@ import org.bukkit.entity.Player;
import java.util.HashMap;
public class TridentManager {
private HashMap<Player, TridentResetTask> tasks;
/** collects all TridentResetTask bound to a player */
private HashMap<Player, TridentResetTask> tasks = new HashMap<Player, TridentResetTask>();
private GameManager gameManager;
/**
* Handles all trident related functions
* @param gameManager the local Gamemanager instance
*/
public TridentManager(GameManager gameManager) { this.gameManager = gameManager; }
/**
* Sets the trident reset task
* @param thrower the throwing player
* @param task the tridentreset tast to create
*/
public void setTasks(Player thrower, TridentResetTask task) { this.tasks.put(thrower, task); }
/**
* the trident reset task of a given player
* @param thrower the throwing player to get the task from
* @return the TridentResetTask of given player
*/
public TridentResetTask getTask (Player thrower) { return tasks.get(thrower); }
/**
* Removes a TridentResetTask of a given player
* @param thrower the thrower of which the TridentResetTask should be removed from
*/
public void removeTridentTast(Player thrower) { tasks.remove(thrower); }
}

View File

@ -17,11 +17,13 @@ public class TridentResetTask extends BukkitRunnable {
this.gameManager = gameManager;
this.thrower = thrower;
this.projektile = projektile;
System.out.println("Trident Reset task started");
}
@Override
public void run() {
projektile.remove();
thrower.getInventory().addItem(new ItemStack(Material.TRIDENT));
System.out.println("Trident Reset task executed");
}
}