88 Zeilen
3.0 KiB
Java
88 Zeilen
3.0 KiB
Java
/*
|
|
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.BauSystem;
|
|
import de.steamwar.bausystem.Permission;
|
|
import de.steamwar.bausystem.world.Welt;
|
|
import net.md_5.bungee.api.ChatMessageType;
|
|
import net.md_5.bungee.api.chat.TextComponent;
|
|
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.HandlerList;
|
|
import org.bukkit.event.Listener;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public abstract class ToggleCommand implements CommandExecutor, Listener {
|
|
|
|
private static Map<Class<? extends ToggleCommand>, Boolean> enabled = new HashMap<>();
|
|
private static Map<Class<? extends ToggleCommand>, ToggleCommand> instance = new HashMap<>();
|
|
|
|
ToggleCommand(boolean on){
|
|
enabled.put(getClass(), false);
|
|
if(on)
|
|
toggle();
|
|
instance.put(getClass(), this);
|
|
}
|
|
|
|
@Override
|
|
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
|
if(!(sender instanceof Player))
|
|
return false;
|
|
Player player = (Player) sender;
|
|
|
|
if (Welt.noPermission(player, Permission.world)){
|
|
player.sendMessage(BauSystem.PREFIX + getNoPermMessage());
|
|
return false;
|
|
}
|
|
|
|
toggle();
|
|
return false;
|
|
}
|
|
|
|
static ToggleCommand getInstance(Class<? extends ToggleCommand> clazz){
|
|
return instance.get(clazz);
|
|
}
|
|
|
|
abstract String getNoPermMessage();
|
|
abstract String getEnableMessage();
|
|
abstract String getDisableMessage();
|
|
|
|
public boolean isOn(){
|
|
return enabled.get(getClass());
|
|
}
|
|
|
|
public void toggle(){
|
|
enabled.compute(getClass(), (clazz, value) -> !value);
|
|
if(enabled.get(getClass())){
|
|
Bukkit.getPluginManager().registerEvents(this, BauSystem.getPlugin());
|
|
Bukkit.getOnlinePlayers().forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(getEnableMessage())));
|
|
}else{
|
|
HandlerList.unregisterAll(this);
|
|
Bukkit.getOnlinePlayers().forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(getDisableMessage())));
|
|
}
|
|
}
|
|
}
|