Unicopia/src/main/java/com/minelittlepony/unicopia/command/DisguiseCommand.java

133 lines
6.2 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.command;
2020-01-27 11:05:22 +01:00
import java.util.function.Function;
import com.minelittlepony.unicopia.InteractionManager;
import com.minelittlepony.unicopia.ability.magic.SpellPredicate;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.mojang.authlib.GameProfile;
2020-01-27 11:05:22 +01:00
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
2020-01-27 11:05:22 +01:00
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.command.argument.EntitySummonArgumentType;
2021-08-04 15:38:03 +02:00
import net.minecraft.command.argument.NbtCompoundArgumentType;
2020-01-27 11:05:22 +01:00
import net.minecraft.command.suggestion.SuggestionProviders;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.player.PlayerEntity;
2021-08-04 15:38:03 +02:00
import net.minecraft.nbt.NbtCompound;
2020-01-27 11:05:22 +01:00
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
2020-01-27 11:05:22 +01:00
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Identifier;
2021-08-07 22:32:05 +02:00
import net.minecraft.util.Util;
2020-01-27 11:05:22 +01:00
import net.minecraft.world.GameRules;
public class DisguiseCommand {
2020-01-27 11:05:22 +01:00
private static final SimpleCommandExceptionType FAILED_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("commands.disguise.notfound"));
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
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()))
);
2020-01-27 11:05:22 +01:00
}
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"))))
);
}
2020-01-27 11:05:22 +01:00
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"))));
}
2020-01-27 11:05:22 +01:00
static int disguise(ServerCommandSource source, PlayerEntity player, Entity entity) throws CommandSyntaxException {
2020-01-27 11:05:22 +01:00
if (entity == null) {
throw FAILED_EXCEPTION.create();
}
Pony iplayer = Pony.of(player);
iplayer.getSpellSlot().get(SpellType.CHANGELING_DISGUISE, true)
.orElseGet(() -> SpellType.CHANGELING_DISGUISE.withTraits().apply(iplayer))
2021-03-03 10:33:23 +01:00
.setDisguise(entity);
2020-01-27 11:05:22 +01:00
2021-08-07 22:32:05 +02:00
if (source.getEntity() == player) {
source.sendFeedback(new TranslatableText("commands.disguise.success.self", entity.getName()), true);
2020-01-27 11:05:22 +01:00
} else {
if (player.getEntityWorld().getGameRules().getBoolean(GameRules.SEND_COMMAND_FEEDBACK)) {
2021-08-07 22:32:05 +02:00
player.sendSystemMessage(new TranslatableText("commands.disguise.success", entity.getName()), Util.NIL_UUID);
2020-01-27 11:05:22 +01:00
}
2021-08-07 22:32:05 +02:00
source.sendFeedback(new TranslatableText("commands.disguise.success.other", player.getName(), entity.getName()), true);
2020-01-27 11:05:22 +01:00
}
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));
}
2020-01-27 11:05:22 +01:00
static int reveal(ServerCommandSource source, PlayerEntity player) {
2020-04-15 18:12:00 +02:00
Pony iplayer = Pony.of(player);
iplayer.getSpellSlot().removeIf(SpellPredicate.IS_DISGUISE, true);
2020-01-27 11:05:22 +01:00
2021-08-07 22:32:05 +02:00
if (source.getEntity() == player) {
source.sendFeedback(new TranslatableText("commands.disguise.removed.self"), true);
} else {
if (player.getEntityWorld().getGameRules().getBoolean(GameRules.SEND_COMMAND_FEEDBACK)) {
player.sendSystemMessage(new TranslatableText("commands.disguise.removed"), Util.NIL_UUID);
}
source.sendFeedback(new TranslatableText("commands.disguise.removed.other", player.getName()), true);
2020-01-27 11:05:22 +01:00
}
return 0;
}
interface Arg<T> {
T apply(CommandContext<ServerCommandSource> context) throws CommandSyntaxException;
}
2020-01-27 11:05:22 +01:00
}