From 7de6402a630d83bf6683f68367f94dba20233a83 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 1 Sep 2022 18:24:56 +0200 Subject: [PATCH] Update ChallengeCommand --- .../bungeecore/commands/ChallengeCommand.java | 62 ++++--------- .../bungeecore/commands/TypeMappers.java | 88 +++++++++++++++++++ src/de/steamwar/command/TypeUtils.java | 3 + 3 files changed, 110 insertions(+), 43 deletions(-) create mode 100644 src/de/steamwar/bungeecore/commands/TypeMappers.java diff --git a/src/de/steamwar/bungeecore/commands/ChallengeCommand.java b/src/de/steamwar/bungeecore/commands/ChallengeCommand.java index 143ae94..6f300f3 100644 --- a/src/de/steamwar/bungeecore/commands/ChallengeCommand.java +++ b/src/de/steamwar/bungeecore/commands/ChallengeCommand.java @@ -45,7 +45,7 @@ public class ChallengeCommand extends SWCommand { } @Register(description = "CHALLENGE_USAGE") - public void challenge(@Validator("fabric") ProxiedPlayer player, @Mapper("target") ProxiedPlayer target, String... args) { + public void challenge(@Validator("fabric") ProxiedPlayer player, @Validator("target") ProxiedPlayer target, @Mapper("nonHistoricArenaMode") @OptionalValue("") @AllowNull ArenaMode arenaMode, @Mapper("arenaMap") @OptionalValue("") @AllowNull String map) { FightCommand.createArena(player, "/challenge " + target.getName() + " ", false, args, 0, false, (p, mode, map) -> { if(challenges.containsKey(target) && challenges.get(target).contains(p)){ challenges.remove(target); @@ -69,55 +69,31 @@ public class ChallengeCommand extends SWCommand { }); } - @Validator("fabric") - public TypeValidator fabricChecker() { + @Validator("target") + public TypeValidator targetValidator() { return (sender, value, messageSender) -> { - if (ModLoaderBlocker.isFabric(value)) { - messageSender.send("MODLOADER_DENIED"); + if (value == null) { + messageSender.send("CHALLENGE_OFFLINE"); + return false; + } + if (sender == value) { + messageSender.send("CHALLENGE_SELF"); + return false; + } + if (IgnoreSystem.isIgnored(value, (ProxiedPlayer) sender)) { + messageSender.send("CHALLENGE_IGNORED"); + return false; + } + + Subserver subserver = Subserver.getSubserver(value); + if (subserver != null && subserver.getType() == Servertype.ARENA) { + messageSender.send("CHALLENGE_INARENA"); return false; } return true; }; } - @Mapper("target") - public TypeMapper targetMapper() { - return new TypeMapper() { - @Override - public ProxiedPlayer map(CommandSender commandSender, String[] previousArguments, String s) { - return BungeeCord.getInstance().getPlayer(s); - } - - @Override - public boolean validate(CommandSender sender, ProxiedPlayer value, MessageSender messageSender) { - if (value == null) { - messageSender.send("CHALLENGE_OFFLINE"); - return false; - } - if (sender == value) { - messageSender.send("CHALLENGE_SELF"); - return false; - } - if (IgnoreSystem.isIgnored(value, (ProxiedPlayer) sender)) { - messageSender.send("CHALLENGE_IGNORED"); - return false; - } - - Subserver subserver = Subserver.getSubserver(value); - if (subserver != null && subserver.getType() == Servertype.ARENA) { - messageSender.send("CHALLENGE_INARENA"); - return false; - } - return true; - } - - @Override - public Collection tabCompletes(CommandSender sender, String[] previousArguments, String s) { - return BungeeCord.getInstance().getPlayers().stream().map(ProxiedPlayer::getDisplayName).collect(Collectors.toList()); - } - }; - } - public static void remove(ProxiedPlayer player){ challenges.remove(player); } diff --git a/src/de/steamwar/bungeecore/commands/TypeMappers.java b/src/de/steamwar/bungeecore/commands/TypeMappers.java new file mode 100644 index 0000000..4f3d7bd --- /dev/null +++ b/src/de/steamwar/bungeecore/commands/TypeMappers.java @@ -0,0 +1,88 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bungeecore.commands; + +import de.steamwar.bungeecore.ArenaMode; +import de.steamwar.bungeecore.listeners.mods.ModLoaderBlocker; +import de.steamwar.command.SWCommandUtils; +import de.steamwar.command.TypeMapper; +import de.steamwar.command.TypeValidator; +import lombok.experimental.UtilityClass; +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +@UtilityClass +public class TypeMappers { + + public static void init() { + SWCommandUtils.addValidator("fabric", fabricChecker()); + SWCommandUtils.addMapper("nonHistoricArenaMode", arenaModeTypeMapper(false)); + SWCommandUtils.addMapper("historicArenaMode", arenaModeTypeMapper(true)); + SWCommandUtils.addMapper("arenaMap", arenaMapTypeMapper()); + } + + public static TypeValidator fabricChecker() { + return (sender, value, messageSender) -> { + if (ModLoaderBlocker.isFabric(value)) { + messageSender.send("MODLOADER_DENIED"); + return false; + } + return true; + }; + } + + public static TypeMapper arenaModeTypeMapper(boolean historic) { + return new TypeMapper() { + @Override + public ArenaMode map(CommandSender commandSender, String[] previousArguments, String s) { + return ArenaMode.getByChat(s); + } + + @Override + public Collection tabCompletes(CommandSender sender, String[] previousArguments, String s) { + return ArenaMode.getAllChatNames(historic); + } + }; + } + + public static TypeMapper arenaMapTypeMapper() { + return new TypeMapper() { + @Override + public String map(CommandSender commandSender, String[] previousArguments, String s) { + if (s.equalsIgnoreCase("random")) return "random"; + ArenaMode arenaMode = ArenaMode.getByChat(previousArguments[previousArguments.length - 1]); + if (arenaMode.getMaps().contains(s)) return s; + return null; + } + + @Override + public Collection tabCompletes(CommandSender sender, String[] previousArguments, String s) { + ArenaMode arenaMode = ArenaMode.getByChat(previousArguments[previousArguments.length - 1]); + List stringList = new ArrayList<>(arenaMode.getMaps()); + stringList.add("random"); + return stringList; + } + }; + } +} diff --git a/src/de/steamwar/command/TypeUtils.java b/src/de/steamwar/command/TypeUtils.java index df779d9..833cd4b 100644 --- a/src/de/steamwar/command/TypeUtils.java +++ b/src/de/steamwar/command/TypeUtils.java @@ -19,6 +19,7 @@ package de.steamwar.command; +import de.steamwar.bungeecore.commands.TypeMappers; import de.steamwar.bungeecore.sql.SteamwarUser; import lombok.experimental.UtilityClass; import net.md_5.bungee.BungeeCord; @@ -32,5 +33,7 @@ public class TypeUtils { static void init() { SWCommandUtils.addMapper(ProxiedPlayer.class, SWCommandUtils.createMapper(BungeeCord.getInstance()::getPlayer, (s) -> BungeeCord.getInstance().getPlayers().stream().map(ProxiedPlayer::getName).collect(Collectors.toList()))); SWCommandUtils.addMapper(SteamwarUser.class, SWCommandUtils.createMapper(SteamwarUser::get, s -> BungeeCord.getInstance().getPlayers().stream().map(ProxiedPlayer::getName).collect(Collectors.toList()))); + + TypeMappers.init(); } }