From b4b567c647780f319450ed3c2573b2dbd03d8b98 Mon Sep 17 00:00:00 2001 From: Sollace Date: Wed, 18 Aug 2021 17:31:31 +0200 Subject: [PATCH] Rewrite the disguise command. Admins can now disguise yourself as a named player, and change the disguises of other players --- .../unicopia/command/DisguiseCommand.java | 88 +++++++++++++------ 1 file changed, 61 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/command/DisguiseCommand.java b/src/main/java/com/minelittlepony/unicopia/command/DisguiseCommand.java index 1324e026..844650b8 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/DisguiseCommand.java +++ b/src/main/java/com/minelittlepony/unicopia/command/DisguiseCommand.java @@ -2,14 +2,19 @@ package com.minelittlepony.unicopia.command; import java.util.function.Function; +import com.minelittlepony.unicopia.InteractionManager; import com.minelittlepony.unicopia.ability.magic.Spell; import com.minelittlepony.unicopia.ability.magic.spell.SpellType; import com.minelittlepony.unicopia.entity.player.Pony; +import com.mojang.authlib.GameProfile; import com.mojang.brigadier.CommandDispatcher; -import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; +import net.minecraft.command.argument.EntityArgumentType; import net.minecraft.command.argument.EntitySummonArgumentType; import net.minecraft.command.argument.NbtCompoundArgumentType; import net.minecraft.command.suggestion.SuggestionProviders; @@ -19,6 +24,7 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.nbt.NbtCompound; import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.TranslatableText; import net.minecraft.util.Identifier; import net.minecraft.util.Util; @@ -28,40 +34,54 @@ public class DisguiseCommand { private static final SimpleCommandExceptionType FAILED_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("commands.disguise.notfound")); public static void register(CommandDispatcher dispatcher) { - LiteralArgumentBuilder builder = CommandManager - .literal("disguise") - .requires(s -> s.hasPermissionLevel(2)) - .executes(context -> reveal(context.getSource(), context.getSource().getPlayer())); - - builder.then(CommandManager - .argument("entity", EntitySummonArgumentType.entitySummon()) - .suggests(SuggestionProviders.SUMMONABLE_ENTITIES) - .executes(context -> disguise(context.getSource(), - context.getSource().getPlayer(), - EntitySummonArgumentType.getEntitySummon(context, "entity"), - new NbtCompound(), true)) - .then(CommandManager.argument("nbt", NbtCompoundArgumentType.nbtCompound()) - .executes(context -> disguise(context.getSource(), - context.getSource().getPlayer(), - EntitySummonArgumentType.getEntitySummon(context, "entity"), - NbtCompoundArgumentType.getNbtCompound(context, "nbt"), false)) - )); - - dispatcher.register(builder); + dispatcher.register(CommandManager + .literal("disguise") + .requires(s -> s.hasPermissionLevel(2)) + .executes(context -> reveal(context.getSource(), context.getSource().getPlayer())) + .then( + CommandManager.argument("target", EntityArgumentType.players()) + .then(buildEntityDisguise(context -> EntityArgumentType.getPlayer(context, "target"))) + .then(buildPlayerDisguise(context -> EntityArgumentType.getPlayer(context, "target"))) + ) + .then(buildEntityDisguise(context -> context.getSource().getPlayer())) + .then(buildPlayerDisguise(context -> context.getSource().getPlayer())) + ); } - static int disguise(ServerCommandSource source, PlayerEntity player, Identifier id, NbtCompound nbt, boolean isSelf) throws CommandSyntaxException { - nbt = nbt.copy(); - nbt.putString("id", id.toString()); + private static ArgumentBuilder buildEntityDisguise(Arg targetOp) { + return CommandManager.argument("entity", EntitySummonArgumentType.entitySummon()) + .suggests(SuggestionProviders.SUMMONABLE_ENTITIES) + .executes(context -> disguise( + context.getSource(), + targetOp.apply(context), + loadEntity(context.getSource(), + EntitySummonArgumentType.getEntitySummon(context, "entity"), + new NbtCompound()))) + .then( + CommandManager.argument("nbt", NbtCompoundArgumentType.nbtCompound()) + .executes(context -> disguise( + context.getSource(), + targetOp.apply(context), + loadEntity(context.getSource(), + EntitySummonArgumentType.getEntitySummon(context, "entity"), + NbtCompoundArgumentType.getNbtCompound(context, "nbt")))) + ); + } - Pony iplayer = Pony.of(player); - - Entity entity = EntityType.loadEntityWithPassengers(nbt, source.getWorld(), Function.identity()); + private static ArgumentBuilder buildPlayerDisguise(Arg targetOp) { + return CommandManager.argument("playername", StringArgumentType.string()) + .executes(context -> disguise( + context.getSource(), + targetOp.apply(context), + loadPlayer(context.getSource(), StringArgumentType.getString(context, "playername")))); + } + static int disguise(ServerCommandSource source, PlayerEntity player, Entity entity) throws CommandSyntaxException { if (entity == null) { throw FAILED_EXCEPTION.create(); } + Pony iplayer = Pony.of(player); iplayer.getSpellSlot().get(SpellType.DISGUISE, true) .orElseGet(() -> SpellType.DISGUISE.apply(iplayer)) .setDisguise(entity); @@ -79,6 +99,16 @@ public class DisguiseCommand { return 0; } + static Entity loadEntity(ServerCommandSource source, Identifier id, NbtCompound nbt) { + nbt = nbt.copy(); + nbt.putString("id", id.toString()); + return EntityType.loadEntityWithPassengers(nbt, source.getWorld(), Function.identity()); + } + + static Entity loadPlayer(ServerCommandSource source, String username) { + return InteractionManager.instance().createPlayer(source.getWorld(), new GameProfile(null, username)); + } + static int reveal(ServerCommandSource source, PlayerEntity player) { Pony iplayer = Pony.of(player); iplayer.getSpellSlot().get(SpellType.DISGUISE, true).ifPresent(Spell::setDead); @@ -95,4 +125,8 @@ public class DisguiseCommand { return 0; } + + interface Arg { + T apply(CommandContext context) throws CommandSyntaxException; + } }