mirror of
https://github.com/Sollace/Unicopia.git
synced 2024-11-23 21:38:00 +01:00
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:
parent
0e007b3611
commit
b4b567c647
1 changed files with 61 additions and 27 deletions
|
@ -2,14 +2,19 @@ package com.minelittlepony.unicopia.command;
|
||||||
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import com.minelittlepony.unicopia.InteractionManager;
|
||||||
import com.minelittlepony.unicopia.ability.magic.Spell;
|
import com.minelittlepony.unicopia.ability.magic.Spell;
|
||||||
import com.minelittlepony.unicopia.ability.magic.spell.SpellType;
|
import com.minelittlepony.unicopia.ability.magic.spell.SpellType;
|
||||||
import com.minelittlepony.unicopia.entity.player.Pony;
|
import com.minelittlepony.unicopia.entity.player.Pony;
|
||||||
|
import com.mojang.authlib.GameProfile;
|
||||||
import com.mojang.brigadier.CommandDispatcher;
|
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.CommandSyntaxException;
|
||||||
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
||||||
|
|
||||||
|
import net.minecraft.command.argument.EntityArgumentType;
|
||||||
import net.minecraft.command.argument.EntitySummonArgumentType;
|
import net.minecraft.command.argument.EntitySummonArgumentType;
|
||||||
import net.minecraft.command.argument.NbtCompoundArgumentType;
|
import net.minecraft.command.argument.NbtCompoundArgumentType;
|
||||||
import net.minecraft.command.suggestion.SuggestionProviders;
|
import net.minecraft.command.suggestion.SuggestionProviders;
|
||||||
|
@ -19,6 +24,7 @@ import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.nbt.NbtCompound;
|
import net.minecraft.nbt.NbtCompound;
|
||||||
import net.minecraft.server.command.CommandManager;
|
import net.minecraft.server.command.CommandManager;
|
||||||
import net.minecraft.server.command.ServerCommandSource;
|
import net.minecraft.server.command.ServerCommandSource;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
import net.minecraft.text.TranslatableText;
|
import net.minecraft.text.TranslatableText;
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
import net.minecraft.util.Util;
|
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"));
|
private static final SimpleCommandExceptionType FAILED_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("commands.disguise.notfound"));
|
||||||
|
|
||||||
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
|
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
|
||||||
LiteralArgumentBuilder<ServerCommandSource> builder = CommandManager
|
dispatcher.register(CommandManager
|
||||||
.literal("disguise")
|
.literal("disguise")
|
||||||
.requires(s -> s.hasPermissionLevel(2))
|
.requires(s -> s.hasPermissionLevel(2))
|
||||||
.executes(context -> reveal(context.getSource(), context.getSource().getPlayer()));
|
.executes(context -> reveal(context.getSource(), context.getSource().getPlayer()))
|
||||||
|
.then(
|
||||||
builder.then(CommandManager
|
CommandManager.argument("target", EntityArgumentType.players())
|
||||||
.argument("entity", EntitySummonArgumentType.entitySummon())
|
.then(buildEntityDisguise(context -> EntityArgumentType.getPlayer(context, "target")))
|
||||||
.suggests(SuggestionProviders.SUMMONABLE_ENTITIES)
|
.then(buildPlayerDisguise(context -> EntityArgumentType.getPlayer(context, "target")))
|
||||||
.executes(context -> disguise(context.getSource(),
|
)
|
||||||
context.getSource().getPlayer(),
|
.then(buildEntityDisguise(context -> context.getSource().getPlayer()))
|
||||||
EntitySummonArgumentType.getEntitySummon(context, "entity"),
|
.then(buildPlayerDisguise(context -> context.getSource().getPlayer()))
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int disguise(ServerCommandSource source, PlayerEntity player, Identifier id, NbtCompound nbt, boolean isSelf) throws CommandSyntaxException {
|
private static ArgumentBuilder<ServerCommandSource, ?> buildEntityDisguise(Arg<ServerPlayerEntity> targetOp) {
|
||||||
nbt = nbt.copy();
|
return CommandManager.argument("entity", EntitySummonArgumentType.entitySummon())
|
||||||
nbt.putString("id", id.toString());
|
.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);
|
private static ArgumentBuilder<ServerCommandSource, ?> buildPlayerDisguise(Arg<ServerPlayerEntity> targetOp) {
|
||||||
|
return CommandManager.argument("playername", StringArgumentType.string())
|
||||||
Entity entity = EntityType.loadEntityWithPassengers(nbt, source.getWorld(), Function.identity());
|
.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) {
|
if (entity == null) {
|
||||||
throw FAILED_EXCEPTION.create();
|
throw FAILED_EXCEPTION.create();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Pony iplayer = Pony.of(player);
|
||||||
iplayer.getSpellSlot().get(SpellType.DISGUISE, true)
|
iplayer.getSpellSlot().get(SpellType.DISGUISE, true)
|
||||||
.orElseGet(() -> SpellType.DISGUISE.apply(iplayer))
|
.orElseGet(() -> SpellType.DISGUISE.apply(iplayer))
|
||||||
.setDisguise(entity);
|
.setDisguise(entity);
|
||||||
|
@ -79,6 +99,16 @@ public class DisguiseCommand {
|
||||||
return 0;
|
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) {
|
static int reveal(ServerCommandSource source, PlayerEntity player) {
|
||||||
Pony iplayer = Pony.of(player);
|
Pony iplayer = Pony.of(player);
|
||||||
iplayer.getSpellSlot().get(SpellType.DISGUISE, true).ifPresent(Spell::setDead);
|
iplayer.getSpellSlot().get(SpellType.DISGUISE, true).ifPresent(Spell::setDead);
|
||||||
|
@ -95,4 +125,8 @@ public class DisguiseCommand {
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Arg<T> {
|
||||||
|
T apply(CommandContext<ServerCommandSource> context) throws CommandSyntaxException;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue