cn.nukkit.command.defaults.TitlerawCommand Maven / Gradle / Ivy
package cn.nukkit.command.defaults;
import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.api.PowerNukkitXOnly;
import cn.nukkit.api.Since;
import cn.nukkit.command.CommandSender;
import cn.nukkit.command.data.CommandEnum;
import cn.nukkit.command.data.CommandParamType;
import cn.nukkit.command.data.CommandParameter;
import cn.nukkit.command.utils.EntitySelector;
import cn.nukkit.command.utils.RawText;
import cn.nukkit.lang.TranslationContainer;
import cn.nukkit.utils.TextFormat;
import java.util.Collections;
import java.util.List;
@PowerNukkitXOnly
@Since("1.6.0.0-PNX")
public class TitlerawCommand extends VanillaCommand {
public TitlerawCommand(String name) {
super(name, "commands.titleraw.description");
this.setPermission("nukkit.command.titleraw");
this.commandParameters.clear();
this.commandParameters.put("clear", new CommandParameter[]{
CommandParameter.newType("player", CommandParamType.TARGET),
CommandParameter.newEnum("clear", new CommandEnum("TitleClear", "clear"))
});
this.commandParameters.put("reset", new CommandParameter[]{
CommandParameter.newType("player", CommandParamType.TARGET),
CommandParameter.newEnum("reset", new CommandEnum("TitleReset", "reset"))
});
this.commandParameters.put("set", new CommandParameter[]{
CommandParameter.newType("player", CommandParamType.TARGET),
CommandParameter.newEnum("titleLocation", new CommandEnum("TitleSet", "title", "subtitle", "actionbar")),
CommandParameter.newType("titleJson", CommandParamType.RAWTEXT)
});
this.commandParameters.put("times", new CommandParameter[]{
CommandParameter.newType("player", CommandParamType.TARGET),
CommandParameter.newEnum("times", new CommandEnum("TitleTimes", "times")),
CommandParameter.newType("fadeIn", CommandParamType.INT),
CommandParameter.newType("stay", CommandParamType.INT),
CommandParameter.newType("fadeOut", CommandParamType.INT)
});
}
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (!this.testPermission(sender)) {
return true;
}
if (args.length < 2) {
sender.sendMessage(new TranslationContainer("commands.generic.usage", "\n" + this.getCommandFormatTips()));
return false;
}
List players = EntitySelector.hasArguments(args[0]) ? EntitySelector.matchEntities(sender, args[0]).stream().filter(e -> e instanceof Player).map(e -> (Player) e).toList() : null;
if (players == null) players = Collections.singletonList(Server.getInstance().getPlayerExact(args[0]));
if (players == null) {
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
return true;
}
for (Player player : players) {
if (args.length == 2) {
switch (args[1].toLowerCase()) {
case "clear":
player.clearTitle();
sender.sendMessage(new TranslationContainer("nukkit.command.title.clear", player.getName()));
break;
case "reset":
player.resetTitleSettings();
sender.sendMessage(new TranslationContainer("nukkit.command.title.reset", player.getName()));
break;
default:
sender.sendMessage(new TranslationContainer("commands.generic.usage", "\n" + this.getCommandFormatTips()));
return false;
}
} else if (args.length == 3) {
RawText text = RawText.fromRawText(args[2]);
text.preParse(sender);
switch (args[1].toLowerCase()) {
case "title":
player.setRawTextTitle(text);
sender.sendMessage(new TranslationContainer("commands.titleraw.success"));
break;
case "subtitle":
player.setRawTextSubTitle(text);
sender.sendMessage(new TranslationContainer("commands.titleraw.success"));
break;
case "actionbar":
player.setRawTextActionBar(text);
sender.sendMessage(new TranslationContainer("commands.titleraw.success"));
break;
default:
sender.sendMessage(new TranslationContainer("commands.generic.usage", "\n" + this.getCommandFormatTips()));
return false;
}
} else if (args.length == 5) {
if (args[1].toLowerCase().equals("times")) {
try {
sender.sendMessage(new TranslationContainer("nukkit.command.title.times.success",
args[2], args[3], args[4], player.getName()));
} catch (NumberFormatException exception) {
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%nukkit.command.title.times.fail"));
}
} else {
sender.sendMessage(new TranslationContainer("commands.generic.usage", "\n" + this.getCommandFormatTips()));
return false;
}
} else {
sender.sendMessage(new TranslationContainer("commands.generic.usage", "\n" + this.getCommandFormatTips()));
return false;
}
}
return true;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy