From 851c1650bee0b651138c69c8b0278ea2c3e085ae Mon Sep 17 00:00:00 2001 From: Sollace Date: Sun, 17 Feb 2019 02:03:55 +0200 Subject: [PATCH] Added a /disguise command --- .../unicopia/command/CommandDisguise.java | 113 ++++++++++++++++++ .../unicopia/command/CommandSpecies.java | 13 +- .../unicopia/command/Commands.java | 1 + .../unicopia/spell/SpellDisguise.java | 7 ++ .../resources/assets/unicopia/lang/en_US.lang | 6 + 5 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/command/CommandDisguise.java diff --git a/src/main/java/com/minelittlepony/unicopia/command/CommandDisguise.java b/src/main/java/com/minelittlepony/unicopia/command/CommandDisguise.java new file mode 100644 index 00000000..8d37ec7e --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/command/CommandDisguise.java @@ -0,0 +1,113 @@ +package com.minelittlepony.unicopia.command; + +import java.util.List; + +import com.minelittlepony.unicopia.player.IPlayer; +import com.minelittlepony.unicopia.player.PlayerSpeciesList; +import com.minelittlepony.unicopia.spell.SpellDisguise; + +import net.minecraft.command.CommandBase; +import net.minecraft.command.CommandException; +import net.minecraft.command.ICommandSender; +import net.minecraft.command.WrongUsageException; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityList; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.nbt.JsonToNBT; +import net.minecraft.nbt.NBTException; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.text.TextComponentTranslation; +import net.minecraft.world.World; +import net.minecraft.world.chunk.storage.AnvilChunkLoader; + +class CommandDisguise extends CommandBase { + + @Override + public String getName() { + return "disguise"; + } + + @Override + public int getRequiredPermissionLevel() { + return 2; + } + + @Override + public String getUsage(ICommandSender sender) { + return "commands.disguise.usage"; + } + + @Override + public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + if (args.length < 2) { + throw new WrongUsageException(getUsage(sender)); + } + + EntityPlayerMP player = args.length > 1 ? getPlayer(server, sender, args[0]) : getCommandSenderAsPlayer(sender); + + IPlayer iplayer = PlayerSpeciesList.instance().getPlayer(player); + + Entity entity = constructDisguiseEntity(player.world, args); + + if (entity == null) { + throw new CommandException("commands.disguise.notfound", args[1]); + } + + SpellDisguise effect = iplayer.getEffect(SpellDisguise.class, true); + + if (effect == null) { + iplayer.setEffect(new SpellDisguise().setDisguise(entity)); + } else { + effect.setDisguise(entity); + } + + if (player != sender) { + notifyCommandListener(sender, this, 1, "commands.disguise.success.other", player.getName(), entity.getName()); + } else { + if (player.getEntityWorld().getGameRules().getBoolean("sendCommandFeedback")) { + player.sendMessage(new TextComponentTranslation("commands.disguise.success.self", entity.getName())); + } + notifyCommandListener(sender, this, 1, "commands.disguise.success.otherself", player.getName(), entity.getName()); + } + } + + protected Entity constructDisguiseEntity(World world, String[] args) throws CommandException { + NBTTagCompound nbt = getEntityNBT(args); + nbt.setString("id", args[1]); + + return AnvilChunkLoader.readWorldEntityPos(nbt, world, 0, 0, 0, false); + } + + protected NBTTagCompound getEntityNBT(String[] args) throws CommandException { + if (args.length > 2) { + try { + return JsonToNBT.getTagFromJson(buildString(args, 2)); + } catch (NBTException e) { + throw new CommandException("commands.summon.tagError", e.getMessage()); + } + } + + return new NBTTagCompound(); + } + + @Override + public List getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, BlockPos pos) { + + if (args.length == 1) { + return getListOfStringsMatchingLastWord(args, server.getOnlinePlayerNames()); + } + + if (args.length == 2) { + return getListOfStringsMatchingLastWord(args, EntityList.getEntityNameList()); + } + + return null; + } + + @Override + public boolean isUsernameIndex(String[] args, int index) { + return index == 1; + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/command/CommandSpecies.java b/src/main/java/com/minelittlepony/unicopia/command/CommandSpecies.java index 0d5b53cc..d907d3af 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/CommandSpecies.java +++ b/src/main/java/com/minelittlepony/unicopia/command/CommandSpecies.java @@ -21,28 +21,27 @@ import net.minecraft.util.text.TextFormatting; class CommandSpecies extends CommandBase { + @Override public String getName() { return "race"; } + @Override public int getRequiredPermissionLevel() { return 0; } + @Override public boolean checkPermission(MinecraftServer server, ICommandSender sender) { return sender.canUseCommand(getRequiredPermissionLevel(), "help"); } - private String getRacesString() { - String values = ""; - - return values; - } - + @Override public String getUsage(ICommandSender sender) { return "commands.race.usage"; } + @Override public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { if (!processCommand(server, sender, args)) { throw new WrongUsageException(getUsage(sender)); @@ -133,7 +132,7 @@ class CommandSpecies extends CommandBase { protected boolean list(EntityPlayer player) { player.sendMessage(new TextComponentTranslation("commands.race.list")); - ITextComponent message = new TextComponentString(getRacesString()); + ITextComponent message = new TextComponentString(""); boolean first = true; for (Race i : Race.values()) { diff --git a/src/main/java/com/minelittlepony/unicopia/command/Commands.java b/src/main/java/com/minelittlepony/unicopia/command/Commands.java index 8b322275..8f963e6d 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/Commands.java +++ b/src/main/java/com/minelittlepony/unicopia/command/Commands.java @@ -8,6 +8,7 @@ public class Commands { event.registerServerCommand(new CommandOverrideGameMode()); event.registerServerCommand(new CommandSpecies()); event.registerServerCommand(new CommandRacelist()); + event.registerServerCommand(new CommandDisguise()); event.getServer().setAllowFlight(true); } diff --git a/src/main/java/com/minelittlepony/unicopia/spell/SpellDisguise.java b/src/main/java/com/minelittlepony/unicopia/spell/SpellDisguise.java index 18e9299c..d3c25b2b 100644 --- a/src/main/java/com/minelittlepony/unicopia/spell/SpellDisguise.java +++ b/src/main/java/com/minelittlepony/unicopia/spell/SpellDisguise.java @@ -19,6 +19,7 @@ import com.mojang.authlib.GameProfile; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityFlying; +import net.minecraft.entity.EntityHanging; import net.minecraft.entity.EntityList; import net.minecraft.entity.EntityLiving; import net.minecraft.entity.EntityLivingBase; @@ -26,6 +27,7 @@ import net.minecraft.entity.boss.EntityDragon; import net.minecraft.entity.item.EntityFallingBlock; import net.minecraft.entity.item.EntityMinecart; import net.minecraft.entity.monster.EntityShulker; +import net.minecraft.entity.monster.EntityVex; import net.minecraft.entity.passive.EntityAmbientCreature; import net.minecraft.entity.passive.EntityTameable; import net.minecraft.entity.player.EntityPlayer; @@ -103,6 +105,8 @@ public class SpellDisguise extends AbstractSpell implements ISuppressable, IFlyi entityId = entityNbt.getString("id"); } + setDirty(true); + return this; } @@ -449,6 +453,7 @@ public class SpellDisguise extends AbstractSpell implements ISuppressable, IFlyi return entity instanceof EntityFlying || entity instanceof net.minecraft.entity.passive.EntityFlying || entity instanceof EntityDragon + || entity instanceof EntityVex || entity instanceof EntityAmbientCreature || entity instanceof EntityShulkerBullet || ProjectileUtil.isProjectile(entity); @@ -478,11 +483,13 @@ public class SpellDisguise extends AbstractSpell implements ISuppressable, IFlyi public static boolean skipsUpdate(Entity entity) { return entity instanceof EntityFallingBlock + || entity instanceof EntityHanging || entity instanceof EntityPlayer; } public static boolean isAttachedEntity(Entity entity) { return entity instanceof EntityShulker + || entity instanceof EntityHanging || entity instanceof EntityFallingBlock; } } diff --git a/src/main/resources/assets/unicopia/lang/en_US.lang b/src/main/resources/assets/unicopia/lang/en_US.lang index f561ab02..2333cc9f 100644 --- a/src/main/resources/assets/unicopia/lang/en_US.lang +++ b/src/main/resources/assets/unicopia/lang/en_US.lang @@ -192,6 +192,12 @@ commands.racelist.disallowed.failed=%1$s is not on the whitelist. commands.racelist.disallowed.other=%1$s was removed from the whitelist by %2$s. commands.racelist.disallowed.failed.other=%2$s tried to remove %1$s from the whitelist but it has not been added yet. +commands.disguise.usage=/disguise [nbt] +commands.disguise.notfound=The entity id '%s' does not exist. +commands.disguise.success.other=%1$s is now disguised as %2$s +commands.disguise.success.self=Updated disguise to %s +commands.disguise.success.otherself=%1$s changed their disguise to %2$s + unicopia.race.human=Human unicopia.race.human.alt=Humans unicopia.race.earth=Earth Pony