Rewrite the disguise command. Admins can now disguise yourself as a named player, and change the disguises of other players

This commit is contained in:
Sollace 2021-08-18 17:31:31 +02:00
parent 0e007b3611
commit b4b567c647

View file

@ -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<ServerCommandSource> dispatcher) {
LiteralArgumentBuilder<ServerCommandSource> 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<ServerCommandSource, ?> buildEntityDisguise(Arg<ServerPlayerEntity> 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<ServerCommandSource, ?> buildPlayerDisguise(Arg<ServerPlayerEntity> 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> {
T apply(CommandContext<ServerCommandSource> context) throws CommandSyntaxException;
}
}