From 64b7814dd5e9613f113762084a890fc3148d5837 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 1 Apr 2021 19:10:03 +0200 Subject: [PATCH] Update CommandTPSLimiter to new SWCommand system --- .../src/de/steamwar/bausystem/BauSystem.java | 3 +- .../bausystem/commands/CommandTPSLimiter.java | 98 +++++++++++-------- .../CommandTPSLimiterTabComplete.java | 51 ---------- BauSystem_Main/src/plugin.yml | 1 - 4 files changed, 59 insertions(+), 94 deletions(-) delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiterTabComplete.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index f9958f8..74b66d3 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -76,8 +76,7 @@ public class BauSystem extends JavaPlugin implements Listener { } new CommandTrace(); - getCommand("tpslimit").setExecutor(new CommandTPSLimiter()); - getCommand("tpslimit").setTabCompleter(new CommandTPSLimiterTabComplete()); + new CommandTPSLimiter(); getCommand("nightvision").setExecutor(new CommandNV()); getCommand("reset").setExecutor(new CommandReset()); getCommand("speed").setExecutor(new CommandSpeed()); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java index 58f79ea..1ebdc0e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java @@ -23,18 +23,22 @@ import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.Permission; import de.steamwar.bausystem.world.TPSUtils; import de.steamwar.bausystem.world.Welt; +import de.steamwar.command.SWCommand; +import de.steamwar.command.SWCommandUtils; +import de.steamwar.command.TypeMapper; import de.steamwar.core.VersionedRunnable; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.Bukkit; import org.bukkit.World; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitTask; -public class CommandTPSLimiter implements CommandExecutor { +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class CommandTPSLimiter extends SWCommand { private static final World WORLD = Bukkit.getWorlds().get(0); private static double currentTPSLimit = 20; @@ -48,6 +52,56 @@ public class CommandTPSLimiter implements CommandExecutor { private BukkitTask tpsLimiter = null; + private List arguments = new ArrayList<>(Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20")); + + public CommandTPSLimiter() { + super("tpslimit"); + if (TPSUtils.isWarpAllowed()) { + arguments.addAll(Arrays.asList("21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40")); + } + } + + @Register(help = true) + public void genericHelp(Player p, String... args) { + p.sendMessage(BauSystem.PREFIX + "Jetziges TPS limit: " + currentTPSLimit); + p.sendMessage(BauSystem.PREFIX + "Ändere das TPS limit mit: §8/§etpslimit §8[§7TPS§8|§edefault§8]"); + } + + @Register({"default"}) + public void defaultCommand(Player p) { + if (!permissionCheck(p)) return; + currentTPSLimit = 20; + sendNewTPSLimitMessage(); + tpsLimiter(); + } + + @Register + public void valueCommand(Player p, String tpsLimit) { + try { + double tpsLimitDouble = Double.parseDouble(tpsLimit.replace(',', '.')); + if (tpsLimitDouble < 0.5 || tpsLimitDouble > (TPSUtils.isWarpAllowed() ? 40 : 20)) { + sendInvalidArgumentMessage(p); + return; + } + currentTPSLimit = tpsLimitDouble; + sendNewTPSLimitMessage(); + tpsLimiter(); + } catch (NumberFormatException e) { + sendInvalidArgumentMessage(p); + } + } + + @ClassMapper(value = double.class, local = true) + public TypeMapper doubleTypeMapper() { + return SWCommandUtils.createMapper(s -> { + try { + return Double.parseDouble(s.replace(',', '.')); + } catch (NumberFormatException e) { + return null; + } + }, s -> arguments); + } + private boolean permissionCheck(Player player) { if (Welt.noPermission(player, Permission.world)) { player.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht den TPS-Limiter nutzen"); @@ -56,42 +110,6 @@ public class CommandTPSLimiter implements CommandExecutor { return true; } - @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if (!(sender instanceof Player)) { - return false; - } else if (args.length == 0) { - sender.sendMessage(BauSystem.PREFIX + "Jetziges TPS limit: " + currentTPSLimit); - sender.sendMessage(BauSystem.PREFIX + "Ändere das TPS limit mit: §8/§etpslimit §8[§7TPS§8|§edefault§8]"); - return false; - } - Player player = (Player) sender; - if (!permissionCheck(player)) return false; - - String tpsLimit = args[0]; - if (tpsLimit.equals("default")) { - currentTPSLimit = 20; - sendNewTPSLimitMessage(); - tpsLimiter(); - return false; - } - - try { - double tpsLimitDouble = Double.parseDouble(tpsLimit.replace(',', '.')); - if (tpsLimitDouble < 0.5 || tpsLimitDouble > (TPSUtils.isWarpAllowed() ? 40 : 20)) { - sendInvalidArgumentMessage(player); - return false; - } - currentTPSLimit = tpsLimitDouble; - sendNewTPSLimitMessage(); - tpsLimiter(); - } catch (NumberFormatException e) { - sendInvalidArgumentMessage(player); - } - - return false; - } - private void sendNewTPSLimitMessage() { Bukkit.getOnlinePlayers().forEach(p -> p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§eTPS limit auf " + currentTPSLimit + " gesetzt."))); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiterTabComplete.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiterTabComplete.java deleted file mode 100644 index cfd10a1..0000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiterTabComplete.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - 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.bausystem.commands; - -import de.steamwar.bausystem.SWUtils; -import de.steamwar.bausystem.world.TPSUtils; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.command.TabCompleter; -import org.bukkit.entity.Player; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class CommandTPSLimiterTabComplete implements TabCompleter { - - private List arguments = new ArrayList<>(Arrays.asList("default", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20")); - - public CommandTPSLimiterTabComplete() { - if (TPSUtils.isWarpAllowed()) - arguments.addAll(Arrays.asList("21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40")); - } - - @Override - public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { - if(!(sender instanceof Player)) return new ArrayList<>(); - if (args.length != 1) { - return new ArrayList<>(); - } - return SWUtils.manageList(arguments, args); - } - -} diff --git a/BauSystem_Main/src/plugin.yml b/BauSystem_Main/src/plugin.yml index b0ada63..ec636de 100644 --- a/BauSystem_Main/src/plugin.yml +++ b/BauSystem_Main/src/plugin.yml @@ -12,7 +12,6 @@ commands: debugstick: tnt: fire: - tpslimit: testblock: aliases: tb reset: