From 0f923f65643d04a7cc7eb9ed40b897b6a7572ba6 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 18 Jan 2020 13:39:15 +0100 Subject: [PATCH] Rework arena and map selection in fight, challenge and testbau command --- src/de/steamwar/bungeecore/ArenaMode.java | 6 + .../bungeecore/commands/BauCommand.java | 17 +- .../bungeecore/commands/ChallengeCommand.java | 71 ++++----- .../bungeecore/commands/FightCommand.java | 148 +++++++++++++----- 4 files changed, 145 insertions(+), 97 deletions(-) diff --git a/src/de/steamwar/bungeecore/ArenaMode.java b/src/de/steamwar/bungeecore/ArenaMode.java index f2df5bcb..b198d808 100644 --- a/src/de/steamwar/bungeecore/ArenaMode.java +++ b/src/de/steamwar/bungeecore/ArenaMode.java @@ -8,6 +8,7 @@ public class ArenaMode { private static Map byChat = new HashMap<>(); private static Map byInternal = new HashMap<>(); + private static List allModes = new LinkedList<>(); private static Random random = new Random(); static void init(Configuration config){ @@ -32,6 +33,10 @@ public class ArenaMode { return chatNames; } + public static List getAllModes(){ + return allModes; + } + private final String internalName; private final String displayName; private final List chatNames; @@ -45,6 +50,7 @@ public class ArenaMode { this.chatNames = config.getStringList("chatNames"); this.maps = config.getStringList("maps"); + allModes.add(this); byInternal.put(internalName, this); for(String name : chatNames){ byChat.put(name.toLowerCase(), this); diff --git a/src/de/steamwar/bungeecore/commands/BauCommand.java b/src/de/steamwar/bungeecore/commands/BauCommand.java index ec4fa8ca..3cee43b2 100644 --- a/src/de/steamwar/bungeecore/commands/BauCommand.java +++ b/src/de/steamwar/bungeecore/commands/BauCommand.java @@ -177,26 +177,15 @@ public class BauCommand { } private static void testarena(ProxiedPlayer p, String[] command){ - if (command.length == 2) { - BungeeCore.send(p, BungeeCore.CHAT_PREFIX + "/bau testarena "); - return; - } - - ArenaMode mode = ArenaMode.getByChat(command[2]); - if(mode == null){ - BungeeCore.send(p, BungeeCore.CHAT_PREFIX + "§cUnbekannter Spielmodus: " + command[2]); - return; - } - - ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { + FightCommand.createArena(p, "/bau testarena ", command, 2, (player, mode, map) -> ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { for (Subserver subserver : Subserver.getServerList()) { if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(p.getUniqueId())) { subserver.stop(); break; } } - SubserverSystem.sendToTestServer(p, mode, FightCommand.getMap(p, mode, new String[0])); - }); + SubserverSystem.sendToTestServer(p, mode, map); + })); } private static BauweltMember member(ProxiedPlayer p, SteamwarUser member){ diff --git a/src/de/steamwar/bungeecore/commands/ChallengeCommand.java b/src/de/steamwar/bungeecore/commands/ChallengeCommand.java index a271435d..20d14a59 100644 --- a/src/de/steamwar/bungeecore/commands/ChallengeCommand.java +++ b/src/de/steamwar/bungeecore/commands/ChallengeCommand.java @@ -18,68 +18,51 @@ public class ChallengeCommand extends BasicCommand { @Override public void execute(CommandSender sender, String[] args) { - if(args.length != 2){ - BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "§8/§echallenge §8[§7Spieler§8] §8[§7Spielmodus§8]"); - return; - } - if(!(sender instanceof ProxiedPlayer)){ - return; - } - - ProxiedPlayer player = (ProxiedPlayer) sender; - - Subserver subserver = Subserver.getSubserver(player); - if(subserver != null && subserver.getType() == Servertype.ARENA){ - BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§cDu befindest dich bereits in einer Arena."); + if(args.length < 1){ + BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "§8/§echallenge §8[§7Spieler§8]"); return; } ProxiedPlayer target = ProxyServer.getInstance().getPlayer(args[0]); if(target == null){ - BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§cDer gewünschte Spieler ist nicht online."); + BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "§cDer gewünschte Spieler ist nicht online."); return; } - subserver = Subserver.getSubserver(target); + Subserver subserver = Subserver.getSubserver(target); if(subserver != null && subserver.getType() == Servertype.ARENA){ - BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§cDer Herausgeforderte ist bereits in einer Arena."); + BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "§cDer Herausgeforderte ist bereits in einer Arena."); return; } - ArenaMode mode = ArenaMode.getByChat(args[1]); - if(mode == null){ - BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§cUnbekannter Spielmodus: " + args[1]); - return; - } + FightCommand.createArena(sender, "/challenge " + target.getName() + " ", args, 1, (player, mode, map) -> { + if(challenges.containsKey(target) && challenges.get(target).contains(player)){ + challenges.remove(target); + challenges.remove(player); - String map = FightCommand.getMap(sender, mode, new String[0]); + Subserver arena = SubserverSystem.startArena(mode, map, 0, null, null, player.getUniqueId(), target.getUniqueId()); - if(challenges.containsKey(target) && challenges.get(target).contains(player)){ - challenges.remove(target); - challenges.remove(player); + arena.sendPlayer(player); + arena.sendPlayer(target); - Subserver arena = SubserverSystem.startArena(mode, map, 0, null, null, player.getUniqueId(), target.getUniqueId()); + BungeeCore.broadcast(BungeeCore.CHAT_PREFIX + "§e" + mode.getDisplayName() + "§7-§eDuell§7: " + player.getName() + " vs " + target.getName(), + "§aZuschauen", + new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/join " + player.getName())); + }else{ + if(!challenges.containsKey(player)){ + challenges.put(player, new LinkedList<>()); + } - arena.sendPlayer(player); - arena.sendPlayer(target); + challenges.get(player).add(target); - BungeeCore.broadcast(BungeeCore.CHAT_PREFIX + "§e" + mode.getDisplayName() + "§7-§eDuell§7: " + player.getName() + " vs " + target.getName(), - "§aZuschauen", - new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/join " + player.getName())); - }else{ - if(!challenges.containsKey(player)){ - challenges.put(player, new LinkedList<>()); + BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§7Du hast §e" + target.getName() + " §7zu einem §e" + mode.getDisplayName() + "-Kampf §7herausgefordert!"); + BungeeCore.send(target, BungeeCore.CHAT_PREFIX + "§e" + player.getName() + " §7 hat dich zu einem §e" + mode.getDisplayName() + "-Kampf §7herausgefordert!"); + + BungeeCore.send(target, BungeeCore.CHAT_PREFIX + "§7Klicke §ehier§7, um die Herausforderung anzunehmen", + "§aHerausforderung annehmen", + new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/challenge " + player.getName() + " " + mode.getChatName())); } - - challenges.get(player).add(target); - - BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§7Du hast §e" + target.getName() + " §7zu einem §e" + mode.getDisplayName() + "-Kampf §7herausgefordert!"); - BungeeCore.send(target, BungeeCore.CHAT_PREFIX + "§e" + player.getName() + " §7 hat dich zu einem §e" + mode.getDisplayName() + "-Kampf §7herausgefordert!"); - - BungeeCore.send(target, BungeeCore.CHAT_PREFIX + "§7Klicke §ehier§7, um die Herausforderung anzunehmen", - "§aHerausforderung annehmen", - new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/challenge " + player.getName() + " " + mode.getChatName())); - } + }); } public static void remove(ProxiedPlayer player){ diff --git a/src/de/steamwar/bungeecore/commands/FightCommand.java b/src/de/steamwar/bungeecore/commands/FightCommand.java index 131ec44c..d74c2a4a 100644 --- a/src/de/steamwar/bungeecore/commands/FightCommand.java +++ b/src/de/steamwar/bungeecore/commands/FightCommand.java @@ -1,6 +1,7 @@ package de.steamwar.bungeecore.commands; import de.steamwar.bungeecore.*; +import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.chat.ClickEvent; @@ -11,63 +12,125 @@ import net.md_5.bungee.api.connection.ProxiedPlayer; import java.util.LinkedList; +/** + * Jeder Fightcommand (auch bau testarena und challenge) haben folgende Optionskette: + * + * [ArenaMode] [Map] + * + * Sollte der ArenaMode fehlen, kann er mit getMode() bestimmt werden. + * Sollte die Map fehlen, kann sie mit getMap() bestimmt werden. + */ public class FightCommand extends BasicCommand { public FightCommand() { super("fight", "", "f"); } - static String getMap(CommandSender sender, ArenaMode mode, String[] args){ - if(args.length == 2){ - String realMap = mode.hasMap(args[1].toLowerCase()); - if(realMap != null) - return realMap; - BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "§cDie gewünschte Arena gibt es nicht."); - return null; - }else{ + private static ArenaMode getMode(CommandSender sender, String arg){ + ArenaMode mode = ArenaMode.getByChat(arg); + if(mode != null) + return mode; + + BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "§cUnbekannter Spielmodus: " + arg); + return null; + } + + private static String getMap(CommandSender sender, ArenaMode mode, String arg){ + String realMap = mode.hasMap(arg.toLowerCase()); + if(realMap != null) + return realMap; + if(arg.equals("Random")) return mode.getRandomMap(); + + BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "§cDie gewünschte Arena gibt es nicht."); + return null; + } + + private static void getModes(CommandSender sender, String precommand){ + TextComponent start = new TextComponent(); + TextComponent current = start; + for(ArenaMode mode : ArenaMode.getAllModes()){ + String command = precommand + mode.getChatName(); + current.setBold(true); + current.setColor(ChatColor.GRAY); + current.setText(mode.getChatName()); + current.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§e" + command).create())); + current.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command)); + if(current != start) + start.addExtra(current); + current = new TextComponent(); } + + sender.sendMessage(start); + } + + private static void getMaps(CommandSender sender, String precommand, ArenaMode mode){ + TextComponent start = new TextComponent(); + TextComponent current = start; + for(String map : mode.getMaps()){ + String command = precommand + mode.getChatName() + " " + map; + current.setBold(true); + current.setColor(ChatColor.GRAY); + current.setText(mode.getChatName()); + current.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§e" + command).create())); + current.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command)); + if(current != start) + start.addExtra(current); + current = new TextComponent(); + } + + sender.sendMessage(start); + } + + private static boolean alreadyInArena(ProxiedPlayer player){ + Subserver subserver = Subserver.getSubserver(player); + if(subserver != null && subserver.getType() == Servertype.ARENA){ + BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§cDu befindest dich bereits in einer Arena."); + return true; + } + + return false; + } + + static void createArena(CommandSender sender, String precommand, String[] args, int startArg, FightCallback callback){ + if(!(sender instanceof ProxiedPlayer)) + return; + + ProxiedPlayer player = (ProxiedPlayer) sender; + if(alreadyInArena(player)) + return; + + if(args.length < startArg+1){ + getModes(sender, precommand); + return; + } + + ArenaMode mode = getMode(sender, args[startArg]); + if(mode == null) + return; + + if(args.length < startArg+2){ + getMaps(sender, precommand, mode); + return; + } + + String map = getMap(sender, mode, args[startArg+1]); + if(map == null) + return; + + callback.run(player, mode, map); } @Override public void execute(CommandSender sender, String[] args) { - if(args.length != 1 && args.length != 2){ - BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "§7Mit §e/fight §7kannst du einen neuen Kampf starten!"); - BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "§8/§efight §8[§7Spielmodus§8] <§7Arena§8>"); - BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "§8/§efight AirShip §8- §7Starte einen §eAirShip§8-§7Kampf§8!"); - BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "§8/§efight WarShip §8- §7Starte einen §eWarShip§8-§7Kampf§8!"); - BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "§8/§efight WarGear §8- §7Starte einen §eWarGear§8-§7Kampf§8!"); - BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "§8/§efight MiniWarGear §8- §7Starte einen §eMiniWarGear§8-§7Kampf"); - return; - } - - if(sender instanceof ProxiedPlayer){ - ProxiedPlayer player = (ProxiedPlayer) sender; - - Subserver subserver = Subserver.getSubserver(player); - if(subserver != null && subserver.getType() == Servertype.ARENA){ - BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "§cDu befindest dich bereits in einer Arena."); - return; - } - - ArenaMode mode = ArenaMode.getByChat(args[0]); - - if(mode == null){ - BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "§cUnbekannter Spielmodus: " + args[0]); - return; - } - - String map = getMap(sender, mode, args); - if(map == null) - return; - + createArena(sender, "/fight ", args, 0, (player, mode, map) -> { Subserver arena = SubserverSystem.startArena(mode, map, 0, null, null, player.getUniqueId(), null); arena.sendPlayer(player); TextComponent tc = new TextComponent(BungeeCore.CHAT_PREFIX + "§7Klicke §ehier§7, um §e" + mode.getDisplayName() + " §7gegen §e" + player.getName() + " §7zu §7kämpfen!"); tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§aGegen §7" + player.getName() + " §ekämpfen").create())); tc.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/join " + player.getName())); ProxyServer.getInstance().broadcast(tc); - } + }); } @Override @@ -82,4 +145,11 @@ public class FightCommand extends BasicCommand { } return new LinkedList<>(); } + + /** + * Is called when arena parameters are clear. + */ + interface FightCallback { + void run(ProxiedPlayer player, ArenaMode mode, String map); + } }