From 259819c7dcd99e6e2ecccfa2ee2b8584849120fd Mon Sep 17 00:00:00 2001 From: Sollace Date: Sat, 9 Feb 2019 14:26:03 +0200 Subject: [PATCH] Rewrote the commands --- .../com/minelittlepony/unicopia/Race.java | 8 +- .../command/CommandOverrideGameMode.java | 33 ++-- .../unicopia/command/CommandSpecies.java | 173 +++++++++++------- .../unicopia/player/PlayerCapabilities.java | 10 +- .../unicopia/player/PlayerSpeciesList.java | 14 +- .../unicopia/spell/IMagicEffect.java | 4 +- 6 files changed, 145 insertions(+), 97 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/Race.java b/src/main/java/com/minelittlepony/unicopia/Race.java index 4cf901eb..f28f3d96 100644 --- a/src/main/java/com/minelittlepony/unicopia/Race.java +++ b/src/main/java/com/minelittlepony/unicopia/Race.java @@ -38,6 +38,10 @@ public enum Race { return this == HUMAN; } + public boolean isOp() { + return this == ALICORN; + } + public boolean canFly() { return flight; } @@ -58,7 +62,7 @@ public enum Race { return String.format("unicopia.race.%s", name().toLowerCase()); } - public boolean isSameAs(String s) { + public boolean equals(String s) { return name().equalsIgnoreCase(s) || getTranslationKey().equalsIgnoreCase(s); } @@ -66,7 +70,7 @@ public enum Race { public static Race fromName(String s, Race def) { if (!Strings.isNullOrEmpty(s)) { for (Race i : values()) { - if (i.isSameAs(s)) return i; + if (i.equals(s)) return i; } } diff --git a/src/main/java/com/minelittlepony/unicopia/command/CommandOverrideGameMode.java b/src/main/java/com/minelittlepony/unicopia/command/CommandOverrideGameMode.java index 0df80010..2bc9000a 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/CommandOverrideGameMode.java +++ b/src/main/java/com/minelittlepony/unicopia/command/CommandOverrideGameMode.java @@ -1,48 +1,51 @@ package com.minelittlepony.unicopia.command; +import com.minelittlepony.unicopia.player.IPlayer; import com.minelittlepony.unicopia.player.PlayerSpeciesList; import net.minecraft.command.CommandException; import net.minecraft.command.CommandGameMode; import net.minecraft.command.ICommandSender; import net.minecraft.command.WrongUsageException; -import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.server.MinecraftServer; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TextComponentTranslation; import net.minecraft.world.GameType; class CommandOverrideGameMode extends CommandGameMode { + + @Override public void execute(MinecraftServer server, ICommandSender sender, String[] params) throws CommandException { + if (params.length <= 0) { throw new WrongUsageException("commands.gamemode.usage"); } GameType gametype = getGameModeFromCommand(sender, params[0]); - EntityPlayerMP entityplayermp = params.length >= 2 ? getPlayer(server, sender, params[1]) : getCommandSenderAsPlayer(sender); + EntityPlayer player = params.length >= 2 ? getPlayer(server, sender, params[1]) : getCommandSenderAsPlayer(sender); - updateGameMode(entityplayermp, gametype); + updateGameMode(player, gametype); - ITextComponent chatcomponenttranslation = new TextComponentTranslation("gameMode." + gametype.getName(), new Object[0]); + ITextComponent mode = new TextComponentTranslation("gameMode." + gametype.getName(), new Object[0]); - if (entityplayermp != sender) { - notifyCommandListener(sender, this, 1, "commands.gamemode.success.other", entityplayermp.getName(), chatcomponenttranslation); + if (sender.getEntityWorld().getGameRules().getBoolean("sendCommandFeedback")) { + player.sendMessage(new TextComponentTranslation("gameMode.changed", mode)); + } + + if (player == sender) { + notifyCommandListener(sender, this, 1, "commands.gamemode.success.self", mode); } else { - notifyCommandListener(sender, this, 1, "commands.gamemode.success.self", chatcomponenttranslation); + notifyCommandListener(sender, this, 1, "commands.gamemode.success.other", player.getName(), mode); } } - private void updateGameMode(EntityPlayerMP player, GameType m) { - boolean flying = player.capabilities.isFlying; - + protected void updateGameMode(EntityPlayer player, GameType m) { player.setGameType(m); - player.capabilities.isFlying = PlayerSpeciesList.instance().getPlayer(player).getPlayerSpecies().canFly(); - if (flying != player.capabilities.isFlying) { - player.sendPlayerAbilities(); - } + IPlayer iplayer = PlayerSpeciesList.instance().getPlayer(player); - player.fallDistance = 0; + iplayer.setPlayerSpecies(iplayer.getPlayerSpecies()); } } diff --git a/src/main/java/com/minelittlepony/unicopia/command/CommandSpecies.java b/src/main/java/com/minelittlepony/unicopia/command/CommandSpecies.java index 5a8aeb1d..0d5b53cc 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/CommandSpecies.java +++ b/src/main/java/com/minelittlepony/unicopia/command/CommandSpecies.java @@ -44,105 +44,142 @@ class CommandSpecies extends CommandBase { } public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { - if (args.length < 1) { + if (!processCommand(server, sender, args)) { throw new WrongUsageException(getUsage(sender)); } + } - EntityPlayerMP player; - int playerIndex = 1; + protected boolean processCommand(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { - if (args[0].contentEquals("set") || args[0].contentEquals("describe")) { - playerIndex++; + if (args.length < 1) { + return false; } - if (args.length > playerIndex) { - player = getPlayer(server, sender, args[playerIndex]); - } else { - player = getCommandSenderAsPlayer(sender); + int playerIndex = this.getPlayerIndex(args); + + EntityPlayerMP player = args.length > playerIndex ? getPlayer(server, sender, args[playerIndex]) : getCommandSenderAsPlayer(sender); + + if (args.length >= 2) { + switch (args[0]) { + case "set": return updateSpecies(sender, player, args); + case "describe": return describeSpecies(player, args); + } } - if (args[0].contentEquals("set") && args.length >= 2) { - Race species = Race.fromName(args[1], Race.HUMAN); + switch (args[0]) { + case "get": return printSpecies(sender, player); + case "list": return list(player); + } - if (species.isDefault()) { + return false; + } + + protected int getPlayerIndex(String[] args) { + switch (args[0]) { + case "set": + case "describe": return 2; + default: return 1; + } + } + + protected boolean updateSpecies(ICommandSender sender, EntityPlayer player, String[] args) { + Race species = Race.fromName(args[1], Race.HUMAN); + + if (species.isDefault()) { + if (player.getEntityWorld().getGameRules().getBoolean("sendCommandFeedback")) { ITextComponent message = new TextComponentTranslation("commands.race.fail", args[1].toUpperCase()); message.getStyle().setColor(TextFormatting.RED); player.sendMessage(message); + } + } else if (PlayerSpeciesList.instance().speciesPermitted(species, player)) { + PlayerSpeciesList.instance().getPlayer(player).setPlayerSpecies(species); + + ITextComponent formattedName = new TextComponentTranslation(species.name().toLowerCase()); + + if (player != sender) { + notifyCommandListener(sender, this, 1, "commands.race.success.other", player.getName(), formattedName); } else { - if (PlayerSpeciesList.instance().speciesPermitted(species, player)) { - PlayerSpeciesList.instance().getPlayer(player).setPlayerSpecies(species); - - ITextComponent formattedName = new TextComponentTranslation(species.name().toLowerCase()); - - if (player != sender) { - notifyCommandListener(sender, this, 1, "commands.race.success.other", player.getName(), formattedName); - } else { - player.sendMessage(new TextComponentTranslation("commands.race.success.self")); - notifyCommandListener(sender, this, 1, "commands.race.success.otherself", player.getName(), formattedName); - } - } else { - player.sendMessage(new TextComponentTranslation("commands.race.permission")); + if (player.getEntityWorld().getGameRules().getBoolean("sendCommandFeedback")) { + player.sendMessage(new TextComponentTranslation("commands.race.success.self")); } + notifyCommandListener(sender, this, 1, "commands.race.success.otherself", player.getName(), formattedName); } - } else if (args[0].contentEquals("get")) { - Race spec = PlayerSpeciesList.instance().getPlayer(player).getPlayerSpecies(); + } else if (player.getEntityWorld().getGameRules().getBoolean("sendCommandFeedback")) { + player.sendMessage(new TextComponentTranslation("commands.race.permission")); + } - String name = "commands.race.tell."; - name += player == sender ? "self" : "other"; + return true; + } - ITextComponent race = new TextComponentTranslation(spec.getTranslationKey()); - ITextComponent message = new TextComponentTranslation(name); + protected boolean printSpecies(ICommandSender sender, EntityPlayer player) { + Race spec = PlayerSpeciesList.instance().getPlayer(player).getPlayerSpecies(); - race.getStyle().setColor(TextFormatting.GOLD); + String name = "commands.race.tell."; + name += player == sender ? "self" : "other"; - message.appendSibling(race); + ITextComponent race = new TextComponentTranslation(spec.getTranslationKey()); + ITextComponent message = new TextComponentTranslation(name); - player.sendMessage(message); - } else if (args[0].contentEquals("list")) { - player.sendMessage(new TextComponentTranslation("commands.race.list")); + race.getStyle().setColor(TextFormatting.GOLD); - ITextComponent message = new TextComponentString(getRacesString()); + message.appendSibling(race); - boolean first = true; - for (Race i : Race.values()) { - if (!i.isDefault() && PlayerSpeciesList.instance().speciesPermitted(i, player)) { - message.appendSibling(new TextComponentString((!first ? "\n" : "") + " - " + i.name().toLowerCase())); - first = false; - } + player.sendMessage(message); + + return true; + } + + protected boolean list(EntityPlayer player) { + player.sendMessage(new TextComponentTranslation("commands.race.list")); + + ITextComponent message = new TextComponentString(getRacesString()); + + boolean first = true; + for (Race i : Race.values()) { + if (!i.isDefault() && PlayerSpeciesList.instance().speciesPermitted(i, player)) { + message.appendSibling(new TextComponentString((!first ? "\n" : "") + " - " + i.name().toLowerCase())); + first = false; } + } - message.getStyle().setColor(TextFormatting.GOLD); + message.getStyle().setColor(TextFormatting.GOLD); - player.sendMessage(message); - } else if (args[0].contentEquals("describe") && args.length >= 2) { - Race species = Race.fromName(args[1], null); + player.sendMessage(message); - if (species == null) { + return true; + } + + protected boolean describeSpecies(EntityPlayer player, String[] args) { + Race species = Race.fromName(args[1], null); + + if (species == null) { + if (player.getEntityWorld().getGameRules().getBoolean("sendCommandFeedback")) { 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)); + 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); } + + return true; } /** * Adds the strings available in this command to the given list of tab completion options. */ + @Override public List getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, BlockPos pos) { if (args.length == 1) { @@ -155,7 +192,7 @@ class CommandSpecies extends CommandBase { EntityPlayer player = sender instanceof EntityPlayer ? (EntityPlayer)sender : null; for (Race i : Race.values()) { - if (args[0].contentEquals("describe") || !(i.isDefault() && PlayerSpeciesList.instance().speciesPermitted(i, player))) { + if (args[0].contentEquals("describe") || (!i.isDefault() && PlayerSpeciesList.instance().speciesPermitted(i, player))) { names.add(i.name().toLowerCase()); } } @@ -170,12 +207,8 @@ class CommandSpecies extends CommandBase { return null; } + @Override 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; + return index == getPlayerIndex(args); } } diff --git a/src/main/java/com/minelittlepony/unicopia/player/PlayerCapabilities.java b/src/main/java/com/minelittlepony/unicopia/player/PlayerCapabilities.java index 0fdf821b..c3d34345 100644 --- a/src/main/java/com/minelittlepony/unicopia/player/PlayerCapabilities.java +++ b/src/main/java/com/minelittlepony/unicopia/player/PlayerCapabilities.java @@ -93,13 +93,7 @@ class PlayerCapabilities implements IPlayer { public void setPlayerSpecies(Race race) { EntityPlayer player = getOwner(); - if (!PlayerSpeciesList.instance().speciesPermitted(race, player)) { - race = Race.EARTH; - - if (!PlayerSpeciesList.instance().speciesPermitted(race, player)) { - race = Race.HUMAN; - } - } + race = PlayerSpeciesList.instance().validate(race, player); player.getDataManager().set(PLAYER_RACE, race.ordinal()); @@ -132,7 +126,7 @@ class PlayerCapabilities implements IPlayer { @Override public boolean isInvisible() { - return invisible; + return invisible && hasEffect(); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/player/PlayerSpeciesList.java b/src/main/java/com/minelittlepony/unicopia/player/PlayerSpeciesList.java index 01e87434..9cc5a4c9 100644 --- a/src/main/java/com/minelittlepony/unicopia/player/PlayerSpeciesList.java +++ b/src/main/java/com/minelittlepony/unicopia/player/PlayerSpeciesList.java @@ -35,13 +35,25 @@ public class PlayerSpeciesList { } public boolean speciesPermitted(Race race, EntityPlayer sender) { - if (race == Race.ALICORN && (sender == null || !sender.capabilities.isCreativeMode)) { + if (race.isOp() && (sender == null || !sender.capabilities.isCreativeMode)) { return false; } return race.isDefault() || UConfig.getInstance().getSpeciesWhiteList().isEmpty() || UConfig.getInstance().getSpeciesWhiteList().contains(race); } + public Race validate(Race race, EntityPlayer sender) { + if (!speciesPermitted(race, sender)) { + race = Race.EARTH; + + if (!speciesPermitted(race, sender)) { + race = Race.HUMAN; + } + } + + return race; + } + public IRaceContainer emptyContainer(Entity entity) { if (entity instanceof EntityPlayer) { return new PlayerCapabilities((EntityPlayer)entity); diff --git a/src/main/java/com/minelittlepony/unicopia/spell/IMagicEffect.java b/src/main/java/com/minelittlepony/unicopia/spell/IMagicEffect.java index b2725a54..cb5e72c4 100644 --- a/src/main/java/com/minelittlepony/unicopia/spell/IMagicEffect.java +++ b/src/main/java/com/minelittlepony/unicopia/spell/IMagicEffect.java @@ -42,7 +42,6 @@ public interface IMagicEffect extends InbtSerialisable, IAligned { */ boolean isCraftable(); - /** * Gets the highest level this spell can be safely operated at. * Gems may go higher, however chance of explosion/exhaustion increases with every level. @@ -106,6 +105,9 @@ public interface IMagicEffect extends InbtSerialisable, IAligned { return false; } + /** + * Returns a new, deep-copied instance of this spell. + */ default IMagicEffect copy() { return SpellRegistry.instance().copyInstance(this); }