2018-09-12 01:29:49 +02:00
|
|
|
package com.minelittlepony.unicopia.command;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import com.minelittlepony.unicopia.Race;
|
|
|
|
import com.minelittlepony.unicopia.player.PlayerSpeciesList;
|
|
|
|
|
|
|
|
import net.minecraft.command.CommandBase;
|
|
|
|
import net.minecraft.command.CommandException;
|
|
|
|
import net.minecraft.command.ICommandSender;
|
|
|
|
import net.minecraft.command.WrongUsageException;
|
2018-09-16 00:45:44 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2018-09-12 01:29:49 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
|
|
|
import net.minecraft.server.MinecraftServer;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.text.ITextComponent;
|
|
|
|
import net.minecraft.util.text.TextComponentString;
|
|
|
|
import net.minecraft.util.text.TextComponentTranslation;
|
|
|
|
import net.minecraft.util.text.TextFormatting;
|
|
|
|
|
|
|
|
class CommandSpecies extends CommandBase {
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return "race";
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getRequiredPermissionLevel() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean checkPermission(MinecraftServer server, ICommandSender sender) {
|
|
|
|
return sender.canUseCommand(getRequiredPermissionLevel(), "help");
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getRacesString() {
|
|
|
|
String values = "";
|
2018-09-16 00:45:44 +02:00
|
|
|
|
2018-09-12 01:29:49 +02:00
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getUsage(ICommandSender sender) {
|
|
|
|
return "commands.race.usage";
|
|
|
|
}
|
|
|
|
|
|
|
|
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
|
|
|
|
if (args.length < 1) {
|
|
|
|
throw new WrongUsageException(getUsage(sender));
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityPlayerMP player;
|
|
|
|
int playerIndex = 1;
|
|
|
|
|
2018-09-16 00:45:44 +02:00
|
|
|
if (args[0].contentEquals("set") || args[0].contentEquals("describe")) {
|
|
|
|
playerIndex++;
|
|
|
|
}
|
2018-09-12 01:29:49 +02:00
|
|
|
|
|
|
|
if (args.length > playerIndex) {
|
|
|
|
player = getPlayer(server, sender, args[playerIndex]);
|
|
|
|
} else {
|
|
|
|
player = getCommandSenderAsPlayer(sender);
|
|
|
|
}
|
|
|
|
|
2018-09-16 00:45:44 +02:00
|
|
|
if (args[0].contentEquals("set") && args.length >= 2) {
|
2019-01-29 13:13:06 +01:00
|
|
|
Race species = Race.fromName(args[1], Race.HUMAN);
|
2018-09-16 00:45:44 +02:00
|
|
|
|
2019-01-29 13:13:06 +01:00
|
|
|
if (species.isDefault()) {
|
|
|
|
ITextComponent message = new TextComponentTranslation("commands.race.fail", args[1].toUpperCase());
|
|
|
|
message.getStyle().setColor(TextFormatting.RED);
|
|
|
|
|
|
|
|
player.sendMessage(message);
|
2018-09-16 00:45:44 +02:00
|
|
|
} else {
|
|
|
|
if (PlayerSpeciesList.instance().speciesPermitted(species, player)) {
|
|
|
|
PlayerSpeciesList.instance().getPlayer(player).setPlayerSpecies(species);
|
|
|
|
|
2019-01-29 13:13:06 +01:00
|
|
|
ITextComponent formattedName = new TextComponentTranslation(species.name().toLowerCase());
|
2018-09-12 01:29:49 +02:00
|
|
|
|
2018-09-16 00:45:44 +02:00
|
|
|
if (player != sender) {
|
2019-01-27 19:07:34 +01:00
|
|
|
notifyCommandListener(sender, this, 1, "commands.race.success.other", formattedName, player.getName());
|
2018-09-16 00:45:44 +02:00
|
|
|
} else {
|
|
|
|
player.sendMessage(new TextComponentTranslation("commands.race.success.self"));
|
2019-01-27 19:07:34 +01:00
|
|
|
notifyCommandListener(sender, this, 1, "commands.race.success.otherself", formattedName, player.getName());
|
2018-09-16 00:45:44 +02:00
|
|
|
}
|
2018-09-12 01:29:49 +02:00
|
|
|
} else {
|
2018-09-16 00:45:44 +02:00
|
|
|
player.sendMessage(new TextComponentTranslation("commands.race.permission"));
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (args[0].contentEquals("get")) {
|
|
|
|
Race spec = PlayerSpeciesList.instance().getPlayer(player).getPlayerSpecies();
|
|
|
|
|
|
|
|
String name = "commands.race.tell.";
|
|
|
|
name += player == sender ? "self" : "other";
|
|
|
|
|
2018-09-12 22:37:06 +02:00
|
|
|
ITextComponent race = new TextComponentTranslation(spec.getTranslationString());
|
2019-01-29 13:13:06 +01:00
|
|
|
ITextComponent message = new TextComponentTranslation(name);
|
2018-09-12 01:29:49 +02:00
|
|
|
|
|
|
|
race.getStyle().setColor(TextFormatting.GOLD);
|
|
|
|
|
|
|
|
message.appendSibling(race);
|
|
|
|
|
|
|
|
player.sendMessage(message);
|
|
|
|
} else if (args[0].contentEquals("list")) {
|
|
|
|
player.sendMessage(new TextComponentTranslation("commands.race.list"));
|
|
|
|
|
2018-09-16 00:45:44 +02:00
|
|
|
ITextComponent message = new TextComponentString(getRacesString());
|
|
|
|
|
|
|
|
boolean first = true;
|
|
|
|
for (Race i : Race.values()) {
|
2019-01-29 13:13:06 +01:00
|
|
|
if (!i.isDefault() && PlayerSpeciesList.instance().speciesPermitted(i, player)) {
|
2018-09-16 00:45:44 +02:00
|
|
|
message.appendSibling(new TextComponentString((!first ? "\n" : "") + " - " + i.name().toLowerCase()));
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 01:29:49 +02:00
|
|
|
|
|
|
|
message.getStyle().setColor(TextFormatting.GOLD);
|
|
|
|
|
|
|
|
player.sendMessage(message);
|
2018-09-16 00:45:44 +02:00
|
|
|
} else if (args[0].contentEquals("describe") && args.length >= 2) {
|
|
|
|
Race species = Race.fromName(args[1], null);
|
|
|
|
|
|
|
|
if (species == null) {
|
|
|
|
player.sendMessage(new TextComponentTranslation("commands.race.fail", args[1].toUpperCase()));
|
|
|
|
} else {
|
|
|
|
String name = species.name().toLowerCase();
|
|
|
|
|
|
|
|
ITextComponent line1 = new TextComponentTranslation(String.format("commands.race.describe.%s.1", name));
|
|
|
|
line1.getStyle().setColor(TextFormatting.YELLOW);
|
|
|
|
|
|
|
|
player.sendMessage(line1);
|
|
|
|
|
|
|
|
player.sendMessage(new TextComponentTranslation(String.format("commands.race.describe.%s.2", name)));
|
|
|
|
|
|
|
|
ITextComponent line3 = new TextComponentTranslation(String.format("commands.race.describe.%s.3", name));
|
|
|
|
line3.getStyle().setColor(TextFormatting.RED);
|
|
|
|
|
|
|
|
player.sendMessage(line3);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new WrongUsageException(getUsage(sender));
|
|
|
|
}
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the strings available in this command to the given list of tab completion options.
|
|
|
|
*/
|
2018-09-16 00:45:44 +02:00
|
|
|
public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, BlockPos pos) {
|
|
|
|
|
2018-09-12 01:29:49 +02:00
|
|
|
if (args.length == 1) {
|
2018-09-16 00:45:44 +02:00
|
|
|
return getListOfStringsMatchingLastWord(args, "get", "set", "list", "describe");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.length == 2 && (args[0].contentEquals("set") || args[0].contentEquals("describe"))) {
|
2018-09-12 01:29:49 +02:00
|
|
|
ArrayList<String> names = new ArrayList<String>();
|
2018-09-16 00:45:44 +02:00
|
|
|
|
|
|
|
EntityPlayer player = sender instanceof EntityPlayer ? (EntityPlayer)sender : null;
|
|
|
|
|
2018-09-12 01:29:49 +02:00
|
|
|
for (Race i : Race.values()) {
|
2019-01-29 13:13:06 +01:00
|
|
|
if (args[0].contentEquals("describe") || !(i.isDefault() && PlayerSpeciesList.instance().speciesPermitted(i, player))) {
|
2018-09-16 00:45:44 +02:00
|
|
|
names.add(i.name().toLowerCase());
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-16 00:45:44 +02:00
|
|
|
|
2019-01-13 21:07:44 +01:00
|
|
|
return getListOfStringsMatchingLastWord(args, names.stream().toArray(String[]::new));
|
2018-09-16 00:45:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((args.length == 3 && args[0].contentEquals("set")) || (args[0].contentEquals("get") && args.length == 2)) {
|
2018-09-12 01:29:49 +02:00
|
|
|
return getListOfStringsMatchingLastWord(args, server.getOnlinePlayerNames());
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isUsernameIndex(String[] args, int index) {
|
|
|
|
if (args[0].contentEquals("get")) {
|
|
|
|
return index == 1;
|
|
|
|
} else if (args[0].contentEquals("set")) {
|
|
|
|
return index == 2;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|