mirror of
https://github.com/zoriya/PluginSDG.git
synced 2026-06-08 15:54:46 +00:00
implementing command:Create game, list game, remove game
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.1</version>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package moe.sdg.PluginSDG.Commands;
|
||||
|
||||
import moe.sdg.PluginSDG.GameManager;
|
||||
import moe.sdg.PluginSDG.GameType;
|
||||
import moe.sdg.PluginSDG.MiniGame;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
public class SDGCommand implements CommandExecutor
|
||||
{
|
||||
GameManager gameManager;
|
||||
|
||||
public SDGCommand(GameManager gameManager){
|
||||
this.gameManager = gameManager;
|
||||
}
|
||||
|
||||
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
|
||||
|
||||
if(args.length == 0)
|
||||
{
|
||||
commandSender.sendMessage(ChatColor.BLUE + "usage: /sdg <action>");
|
||||
return true;
|
||||
}
|
||||
//parse sdg argument
|
||||
switch (args[0])
|
||||
{
|
||||
case "create":
|
||||
if(args.length != 4 && args.length != 3)
|
||||
{
|
||||
commandSender.sendMessage(ChatColor.BLUE + "Usage /sdg create <gameType> <mapName> <gameName>");
|
||||
break;
|
||||
}
|
||||
|
||||
//parse create argument
|
||||
if (args[1].equals("deathmatch")) {
|
||||
//TODO: check for map name
|
||||
//check if a gameName was given
|
||||
if (args.length == 3) {
|
||||
|
||||
gameManager.createGame(GameType.DeathMatch, args[2]);
|
||||
commandSender.sendMessage(ChatColor.BLUE + "Created deathmatch game with a default name since no name where precised");
|
||||
} else {
|
||||
|
||||
//check if game name is already used
|
||||
if (gameManager.getGamesByName(args[3]).size() != 0) {
|
||||
commandSender.sendMessage(ChatColor.BLUE + "Name is already used" + args[0]);
|
||||
}else {
|
||||
gameManager.createGame(GameType.DeathMatch, args[2], args[3]);
|
||||
commandSender.sendMessage(ChatColor.BLUE + "Created deathmatch game");
|
||||
}
|
||||
}
|
||||
}
|
||||
commandSender.sendMessage(ChatColor.BLUE + "Unknown game type " + args[1]);
|
||||
break;
|
||||
|
||||
|
||||
case "remove":
|
||||
//check if entered name exist
|
||||
if(gameManager.getGamesByName(args[1]).size() == 0)
|
||||
{
|
||||
commandSender.sendMessage(ChatColor.BLUE + "Invalid game name: " + args[1]);
|
||||
}else{
|
||||
gameManager.getGamesByName(args[1]).get(0).destroy();
|
||||
commandSender.sendMessage(ChatColor.BLUE + "removed game " + args[1]);
|
||||
}
|
||||
break;
|
||||
|
||||
case "list":
|
||||
|
||||
ArrayList<MiniGame> games = gameManager.getGames();
|
||||
|
||||
commandSender.sendMessage(ChatColor.BLUE + "====="+ ChatColor.DARK_RED + "Game list" + ChatColor.BLUE + "=====" );
|
||||
|
||||
//list game name
|
||||
if(games.size() == 0){
|
||||
commandSender.sendMessage("There is no game");
|
||||
}else {
|
||||
for (MiniGame game : games)
|
||||
{
|
||||
commandSender.sendMessage(game.getType() + " " + game.getCurrentPlayers() + "/" + game.getMaxPlayers() + " " + game.getName());
|
||||
}
|
||||
}
|
||||
commandSender.sendMessage(ChatColor.BLUE +"===================");
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package moe.sdg.PluginSDG;
|
||||
|
||||
import moe.sdg.PluginSDG.Commands.SDGCommand;
|
||||
import moe.sdg.PluginSDG.games.DeathMatch;
|
||||
import moe.sdg.PluginSDG.Commands.HubCommand;
|
||||
import moe.sdg.PluginSDG.Commands.SetHubCommand;
|
||||
@@ -27,6 +28,7 @@ public class GameManager extends JavaPlugin
|
||||
|
||||
getCommand("hub").setExecutor(new HubCommand(config));
|
||||
getCommand("sethub").setExecutor(new SetHubCommand(config));
|
||||
getCommand("sdg").setExecutor(new SDGCommand(this));
|
||||
_games = new ArrayList<>();
|
||||
getLogger().info("Game manager loaded.");
|
||||
}
|
||||
@@ -48,17 +50,36 @@ public class GameManager extends JavaPlugin
|
||||
//! @brief factory for creating new game
|
||||
//! @param type The type of game to create
|
||||
//! @param map the name of the map to use
|
||||
//! @param gameName the name of the new game
|
||||
//! @return
|
||||
public MiniGame createGame(GameType type, String map)
|
||||
public MiniGame createGame(GameType type, String map, String gameName)
|
||||
{
|
||||
switch (type) {
|
||||
case DeathMatch:
|
||||
DeathMatch match = new DeathMatch(this);
|
||||
DeathMatch match = gameName == null ? new DeathMatch(this, this.generateNewName()) :
|
||||
new DeathMatch(this, gameName);
|
||||
this._games.add(match);
|
||||
return match;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public MiniGame createGame(GameType type, String map)
|
||||
{
|
||||
return this.createGame(type,map,null);
|
||||
}
|
||||
|
||||
//! @brief generate a new possible game name of the form <unnamed: number>
|
||||
//! @return
|
||||
private String generateNewName(){
|
||||
String name = "unnamed ";
|
||||
int i = 0;
|
||||
while (this.getGamesByName(name).size() != 0)
|
||||
{
|
||||
name += String.valueOf(i);
|
||||
i++;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
//! @brief delete a game
|
||||
//! @param game the game to be deleted
|
||||
@@ -67,8 +88,16 @@ public class GameManager extends JavaPlugin
|
||||
this._games.remove(game);
|
||||
}
|
||||
|
||||
public ArrayList<MiniGame> getByType(final GameType type)
|
||||
public ArrayList<MiniGame> getGamesByType(final GameType type)
|
||||
{
|
||||
return this._games.stream().filter(e -> e.getType() == type).collect(Collectors.toCollection(ArrayList::new));
|
||||
}
|
||||
|
||||
public ArrayList<MiniGame> getGames(){ return this._games;}
|
||||
|
||||
public ArrayList<MiniGame> getGamesByName(String name)
|
||||
{
|
||||
return this._games.stream().filter(e -> e.getName().equals(name)).collect(Collectors.toCollection(ArrayList::new));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,21 +3,28 @@ package moe.sdg.PluginSDG;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import java.util.ArrayList;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class MiniGame
|
||||
{
|
||||
private final ArrayList<Player> _players;
|
||||
private final GameManager _manager;
|
||||
private final Location _lobbyLocation;
|
||||
|
||||
|
||||
public MiniGame(GameManager manager)
|
||||
private final String gameName;
|
||||
|
||||
public String getName() {
|
||||
return gameName;
|
||||
}
|
||||
|
||||
public MiniGame(GameManager manager, String gameName)
|
||||
{
|
||||
this._manager = manager;
|
||||
this._lobbyLocation = null;
|
||||
this._players = new ArrayList<Player>();
|
||||
this.gameName = gameName;
|
||||
}
|
||||
|
||||
|
||||
public abstract GameType getType();
|
||||
|
||||
//! @brief Return the current count of players in the game.
|
||||
@@ -43,14 +50,28 @@ public abstract class MiniGame
|
||||
|
||||
//! @brief Method called when a player wants to leave the game.
|
||||
//! @param player The player who will leave.
|
||||
public void leave(Player player)
|
||||
public void removePlayer(Player player)
|
||||
{
|
||||
this._players.remove(player);
|
||||
player.teleport(_manager.getHubLocation());
|
||||
if(this.getCurrentPlayers() == 0)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//! @brief Start the game.
|
||||
public abstract void start();
|
||||
|
||||
//! @brief End the game.
|
||||
public abstract void end();
|
||||
|
||||
public void destroy()
|
||||
{
|
||||
if(this.getCurrentPlayers() == 0)
|
||||
{
|
||||
_manager.deleteGame(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -38,9 +38,9 @@ public abstract class TeamMiniGame extends MiniGame
|
||||
private final ArrayList<Team> _teams;
|
||||
|
||||
|
||||
public TeamMiniGame(GameManager manager)
|
||||
public TeamMiniGame(GameManager manager, String gameName)
|
||||
{
|
||||
super(manager);
|
||||
super(manager, gameName);
|
||||
this._teams = new ArrayList<Team>();
|
||||
}
|
||||
|
||||
|
||||
@@ -7,11 +7,11 @@ import moe.sdg.PluginSDG.MiniGame;
|
||||
public class DeathMatch extends MiniGame {
|
||||
|
||||
private int _maxPlayer = 4;
|
||||
private boolean enforceMaxPlayer = false;
|
||||
private boolean enforceMaxPlayer = true;
|
||||
|
||||
public DeathMatch(GameManager gameManager)
|
||||
public DeathMatch(GameManager gameManager, String name)
|
||||
{
|
||||
super(gameManager);
|
||||
super(gameManager, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -23,7 +23,7 @@ public class DeathMatch extends MiniGame {
|
||||
public int getMaxPlayers() {
|
||||
if (enforceMaxPlayer)
|
||||
return _maxPlayer;
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
//! @brief Start the game.
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
name: PluginSDG
|
||||
version: 1.0
|
||||
main: moe.sdg.PluginSDG.GameManager
|
||||
api-version: 1.16
|
||||
commands:
|
||||
hub:
|
||||
description: Return to the hub
|
||||
usage: /<command>
|
||||
sethub:
|
||||
description: Set the hub location
|
||||
usage: /<command>
|
||||
sdg:
|
||||
description: main command for all sdg sub-command
|
||||
usage: /<command>
|
||||
Reference in New Issue
Block a user