SteamWar/BauSystem
Archiviert
13
0

Update CommandTNT to new SWCommand system

Dieser Commit ist enthalten in:
yoyosource 2021-04-01 19:24:21 +02:00
Ursprung dda62edfaa
Commit 34a3534d50
4 geänderte Dateien mit 63 neuen und 101 gelöschten Zeilen

Datei anzeigen

@ -80,8 +80,7 @@ public class BauSystem extends JavaPlugin implements Listener {
getCommand("nightvision").setExecutor(new CommandNV());
getCommand("reset").setExecutor(new CommandReset());
getCommand("speed").setExecutor(new CommandSpeed());
getCommand("tnt").setExecutor(new CommandTNT());
getCommand("tnt").setTabCompleter(new CommandTNTTabComplete());
new CommandTNT();
getCommand("fire").setExecutor(new CommandFire());
getCommand("freeze").setExecutor(new CommandFreeze());
getCommand("testblock").setExecutor(new CommandTestblock());

Datei anzeigen

@ -23,16 +23,21 @@ import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.world.Region;
import de.steamwar.bausystem.world.Welt;
import de.steamwar.command.SWCommand;
import de.steamwar.command.SWCommandUtils;
import de.steamwar.command.TypeMapper;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
public class CommandTNT implements CommandExecutor, Listener {
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CommandTNT extends SWCommand implements Listener {
public enum TNTMode {
ON("§aan"),
@ -48,13 +53,65 @@ public class CommandTNT implements CommandExecutor, Listener {
public String getName() {
return name;
}
}
public CommandTNT() {
super("tnt");
Bukkit.getPluginManager().registerEvents(this, BauSystem.getPlugin());
}
@Register
public void toggleCommand(Player p) {
if (!permissionCheck(p)) return;
Region region = Region.getRegion(p.getLocation());
tntToggle(region, null, null);
}
@Register
public void setCommand(Player p, TNTMode tntMode) {
if (!permissionCheck(p)) return;
Region region = Region.getRegion(p.getLocation());
tntToggle(region, null, null);
String requestedMessage = null;
switch (tntMode) {
case ON:
requestedMessage = getEnableMessage();
break;
case OFF:
requestedMessage = getDisableMessage();
break;
case ONLY_TB:
requestedMessage = getTestblockEnableMessage();
break;
}
tntToggle(region, tntMode, requestedMessage);
}
private boolean permissionCheck(Player p) {
if (Welt.noPermission(p, Permission.world)) {
p.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht TNT-Schaden (de-)aktivieren");
return false;
}
return true;
}
@ClassMapper(value = TNTMode.class, local = true)
public TypeMapper<TNTMode> tntModeTypeMapper() {
Map<String, TNTMode> tntModeMap = new HashMap<>();
tntModeMap.put("an", TNTMode.ON);
tntModeMap.put("on", TNTMode.ON);
tntModeMap.put("aus", TNTMode.OFF);
tntModeMap.put("off", TNTMode.OFF);
if (Region.buildAreaEnabled()) {
tntModeMap.put("testblock", TNTMode.ONLY_TB);
tntModeMap.put("tb", TNTMode.ONLY_TB);
}
List<String> tabCompletes = new ArrayList<>(tntModeMap.keySet());
return SWCommandUtils.createMapper(s -> tntModeMap.getOrDefault(s, null), s -> tabCompletes);
}
private String getEnableMessage() {
return "§aTNT-Schaden aktiviert";
}
@ -67,46 +124,6 @@ public class CommandTNT implements CommandExecutor, Listener {
return "§aTNT-Schaden außerhalb Baurahmen aktiviert";
}
@Override
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
if (!(sender instanceof Player)) return false;
Player player = (Player) sender;
if (Welt.noPermission(player, Permission.world)) {
player.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht TNT-Schaden (de-)aktivieren");
return false;
}
TNTMode requestedMode = null;
String requestedMessage = null;
if (args.length != 0) {
switch (args[0].toLowerCase()) {
case "an":
case "on":
requestedMode = TNTMode.ON;
requestedMessage = getEnableMessage();
break;
case "aus":
case "off":
requestedMode = TNTMode.OFF;
requestedMessage = getDisableMessage();
break;
case "testblock":
case "tb":
if (!Region.buildAreaEnabled()) break;
requestedMode = TNTMode.ONLY_TB;
requestedMessage = getTestblockEnableMessage();
break;
default:
break;
}
}
Region region = Region.getRegion(player.getLocation());
tntToggle(region, requestedMode, requestedMessage);
return false;
}
private void tntToggle(Region region, TNTMode requestedMode, String requestedMessage) {
if (requestedMode != null && region.hasTestblock()) {
region.setTntMode(requestedMode);
@ -147,5 +164,4 @@ public class CommandTNT implements CommandExecutor, Listener {
return region.getTntMode() == TNTMode.OFF;
});
}
}

Datei anzeigen

@ -1,52 +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 <https://www.gnu.org/licenses/>.
* /
*/
package de.steamwar.bausystem.commands;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.world.Region;
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 CommandTNTTabComplete implements TabCompleter {
private List<String> arguments = new ArrayList<>(Arrays.asList("an", "on", "aus", "off"));
public CommandTNTTabComplete() {
if (Region.buildAreaEnabled()) arguments.addAll(Arrays.asList("testblock", "tb"));
}
@Override
public List<String> 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);
}
}

Datei anzeigen

@ -10,7 +10,6 @@ description: "So unseriös wie wir sind: BauSystem nur besser."
commands:
debugstick:
tnt:
fire:
testblock:
aliases: tb