2020-04-15 14:22:03 +02:00
|
|
|
package com.minelittlepony.unicopia.command;
|
2020-01-27 11:05:22 +01:00
|
|
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
2022-10-01 18:20:53 +02:00
|
|
|
import com.minelittlepony.unicopia.EquinePredicates;
|
2021-08-18 17:31:31 +02:00
|
|
|
import com.minelittlepony.unicopia.InteractionManager;
|
2021-12-22 15:43:06 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.SpellPredicate;
|
2023-08-06 19:26:17 +02:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.spell.CastingMethod;
|
2021-11-05 14:18:32 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
|
2020-09-22 15:11:20 +02:00
|
|
|
import com.minelittlepony.unicopia.entity.player.Pony;
|
2021-08-18 17:31:31 +02:00
|
|
|
import com.mojang.authlib.GameProfile;
|
|
|
|
import com.mojang.brigadier.arguments.StringArgumentType;
|
|
|
|
import com.mojang.brigadier.builder.ArgumentBuilder;
|
2023-09-05 00:40:41 +02:00
|
|
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
2021-08-18 17:31:31 +02:00
|
|
|
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;
|
|
|
|
|
2022-12-18 22:07:24 +01:00
|
|
|
import net.minecraft.command.CommandRegistryAccess;
|
|
|
|
import net.minecraft.command.argument.*;
|
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;
|
2022-12-18 22:07:24 +01:00
|
|
|
import net.minecraft.registry.RegistryKeys;
|
|
|
|
import net.minecraft.registry.entry.RegistryEntry;
|
2020-01-27 11:05:22 +01:00
|
|
|
import net.minecraft.server.command.CommandManager;
|
|
|
|
import net.minecraft.server.command.ServerCommandSource;
|
2021-08-18 17:31:31 +02:00
|
|
|
import net.minecraft.server.network.ServerPlayerEntity;
|
2022-06-25 00:19:55 +02:00
|
|
|
import net.minecraft.text.Text;
|
2020-01-27 11:05:22 +01:00
|
|
|
import net.minecraft.world.GameRules;
|
|
|
|
|
2020-01-27 17:37:22 +01:00
|
|
|
public class DisguiseCommand {
|
2022-06-25 00:19:55 +02:00
|
|
|
private static final SimpleCommandExceptionType FAILED_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.disguise.notfound"));
|
2020-01-27 11:05:22 +01:00
|
|
|
|
2023-09-05 00:40:41 +02:00
|
|
|
public static LiteralArgumentBuilder<ServerCommandSource> create(CommandRegistryAccess registries) {
|
|
|
|
return CommandManager.literal("disguise").requires(s -> s.hasPermissionLevel(2))
|
2021-08-18 17:31:31 +02:00
|
|
|
.executes(context -> reveal(context.getSource(), context.getSource().getPlayer()))
|
|
|
|
.then(
|
|
|
|
CommandManager.argument("target", EntityArgumentType.players())
|
2022-12-18 22:07:24 +01:00
|
|
|
.then(buildEntityDisguise(context -> EntityArgumentType.getPlayer(context, "target"), registries))
|
2021-08-18 17:31:31 +02:00
|
|
|
.then(buildPlayerDisguise(context -> EntityArgumentType.getPlayer(context, "target")))
|
|
|
|
)
|
2022-12-18 22:07:24 +01:00
|
|
|
.then(buildEntityDisguise(context -> context.getSource().getPlayer(), registries))
|
2023-09-05 00:40:41 +02:00
|
|
|
.then(buildPlayerDisguise(context -> context.getSource().getPlayer()));
|
2020-01-27 11:05:22 +01:00
|
|
|
}
|
|
|
|
|
2022-12-18 22:07:24 +01:00
|
|
|
private static ArgumentBuilder<ServerCommandSource, ?> buildEntityDisguise(Arg<ServerPlayerEntity> targetOp, CommandRegistryAccess registries) {
|
|
|
|
return CommandManager.argument("entity", RegistryEntryArgumentType.registryEntry(registries, RegistryKeys.ENTITY_TYPE))
|
2021-08-18 17:31:31 +02:00
|
|
|
.suggests(SuggestionProviders.SUMMONABLE_ENTITIES)
|
|
|
|
.executes(context -> disguise(
|
|
|
|
context.getSource(),
|
|
|
|
targetOp.apply(context),
|
|
|
|
loadEntity(context.getSource(),
|
2022-12-18 22:07:24 +01:00
|
|
|
RegistryEntryArgumentType.getSummonableEntityType(context, "entity"),
|
2021-08-18 17:31:31 +02:00
|
|
|
new NbtCompound())))
|
|
|
|
.then(
|
|
|
|
CommandManager.argument("nbt", NbtCompoundArgumentType.nbtCompound())
|
|
|
|
.executes(context -> disguise(
|
|
|
|
context.getSource(),
|
|
|
|
targetOp.apply(context),
|
|
|
|
loadEntity(context.getSource(),
|
2022-12-18 22:07:24 +01:00
|
|
|
RegistryEntryArgumentType.getSummonableEntityType(context, "entity"),
|
2021-08-18 17:31:31 +02:00
|
|
|
NbtCompoundArgumentType.getNbtCompound(context, "nbt"))))
|
|
|
|
);
|
|
|
|
}
|
2020-01-27 11:05:22 +01:00
|
|
|
|
2021-08-18 17:31:31 +02: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
|
|
|
|
2021-08-18 17:31:31 +02:00
|
|
|
static int disguise(ServerCommandSource source, PlayerEntity player, Entity entity) throws CommandSyntaxException {
|
2022-10-01 18:20:53 +02:00
|
|
|
if (entity == null || !EquinePredicates.VALID_FOR_DISGUISE.test(entity)) {
|
2020-01-27 11:05:22 +01:00
|
|
|
throw FAILED_EXCEPTION.create();
|
|
|
|
}
|
|
|
|
|
2021-08-18 17:31:31 +02:00
|
|
|
Pony iplayer = Pony.of(player);
|
2021-12-22 15:43:06 +01:00
|
|
|
iplayer.getSpellSlot().get(SpellType.CHANGELING_DISGUISE, true)
|
2023-08-06 19:26:17 +02:00
|
|
|
.orElseGet(() -> SpellType.CHANGELING_DISGUISE.withTraits().apply(iplayer, CastingMethod.INNATE))
|
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) {
|
2023-06-03 13:40:54 +02:00
|
|
|
source.sendFeedback(() -> Text.translatable("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)) {
|
2022-06-25 00:19:55 +02:00
|
|
|
player.sendMessage(Text.translatable("commands.disguise.success", entity.getName()));
|
2020-01-27 11:05:22 +01:00
|
|
|
}
|
2021-08-07 22:32:05 +02:00
|
|
|
|
2023-06-03 13:40:54 +02:00
|
|
|
source.sendFeedback(() -> Text.translatable("commands.disguise.success.other", player.getName(), entity.getName()), true);
|
2020-01-27 11:05:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-18 22:07:24 +01:00
|
|
|
static Entity loadEntity(ServerCommandSource source, RegistryEntry.Reference<EntityType<?>> entityType, NbtCompound nbt) {
|
2021-08-18 17:31:31 +02:00
|
|
|
nbt = nbt.copy();
|
2022-12-18 22:07:24 +01:00
|
|
|
nbt.putString("id", entityType.registryKey().getValue().toString());
|
2021-08-18 17:31:31 +02:00
|
|
|
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);
|
2021-12-22 15:43:06 +01:00
|
|
|
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) {
|
2023-06-03 13:40:54 +02:00
|
|
|
source.sendFeedback(() -> Text.translatable("commands.disguise.removed.self"), true);
|
2021-08-07 22:32:05 +02:00
|
|
|
} else {
|
|
|
|
if (player.getEntityWorld().getGameRules().getBoolean(GameRules.SEND_COMMAND_FEEDBACK)) {
|
2022-06-25 00:19:55 +02:00
|
|
|
player.sendMessage(Text.translatable("commands.disguise.removed"));
|
2021-08-07 22:32:05 +02:00
|
|
|
}
|
|
|
|
|
2023-06-03 13:40:54 +02:00
|
|
|
source.sendFeedback(() -> Text.translatable("commands.disguise.removed.other", player.getName()), true);
|
2020-01-27 11:05:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-08-18 17:31:31 +02:00
|
|
|
|
|
|
|
interface Arg<T> {
|
|
|
|
T apply(CommandContext<ServerCommandSource> context) throws CommandSyntaxException;
|
|
|
|
}
|
2020-01-27 11:05:22 +01:00
|
|
|
}
|