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;
|
|
|
|
|
2021-03-06 10:58:44 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.Spell;
|
2021-03-01 11:56:30 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.spell.SpellType;
|
2020-09-22 15:11:20 +02:00
|
|
|
import com.minelittlepony.unicopia.entity.player.Pony;
|
2020-01-27 11:05:22 +01:00
|
|
|
import com.mojang.brigadier.CommandDispatcher;
|
|
|
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
|
|
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
|
|
|
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
|
|
|
|
2020-09-22 15:11:20 +02:00
|
|
|
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.text.TranslatableText;
|
|
|
|
import net.minecraft.util.Identifier;
|
|
|
|
import net.minecraft.world.GameRules;
|
|
|
|
|
2020-01-27 17:37:22 +01:00
|
|
|
public class DisguiseCommand {
|
2020-01-27 11:05:22 +01:00
|
|
|
private static final SimpleCommandExceptionType FAILED_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("commands.disguise.notfound"));
|
|
|
|
|
2020-01-27 17:37:22 +01:00
|
|
|
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
|
2020-01-27 11:05:22 +01:00
|
|
|
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"),
|
2021-08-04 15:38:03 +02:00
|
|
|
new NbtCompound(), true))
|
|
|
|
.then(CommandManager.argument("nbt", NbtCompoundArgumentType.nbtCompound())
|
2020-01-27 11:05:22 +01:00
|
|
|
.executes(context -> disguise(context.getSource(),
|
|
|
|
context.getSource().getPlayer(),
|
|
|
|
EntitySummonArgumentType.getEntitySummon(context, "entity"),
|
2021-08-04 15:38:03 +02:00
|
|
|
NbtCompoundArgumentType.getNbtCompound(context, "nbt"), false))
|
2020-01-27 11:05:22 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
dispatcher.register(builder);
|
|
|
|
}
|
|
|
|
|
2021-08-04 15:38:03 +02:00
|
|
|
static int disguise(ServerCommandSource source, PlayerEntity player, Identifier id, NbtCompound nbt, boolean isSelf) throws CommandSyntaxException {
|
2020-04-22 16:28:20 +02:00
|
|
|
nbt = nbt.copy();
|
2020-01-27 11:05:22 +01:00
|
|
|
nbt.putString("id", id.toString());
|
|
|
|
|
2020-04-15 18:12:00 +02:00
|
|
|
Pony iplayer = Pony.of(player);
|
2020-01-27 11:05:22 +01:00
|
|
|
|
|
|
|
Entity entity = EntityType.loadEntityWithPassengers(nbt, source.getWorld(), Function.identity());
|
|
|
|
|
|
|
|
if (entity == null) {
|
|
|
|
throw FAILED_EXCEPTION.create();
|
|
|
|
}
|
|
|
|
|
2021-03-07 10:04:32 +01:00
|
|
|
iplayer.getSpellSlot().get(SpellType.DISGUISE, true)
|
|
|
|
.orElseGet(() -> SpellType.DISGUISE.apply(iplayer))
|
2021-03-03 10:33:23 +01:00
|
|
|
.setDisguise(entity);
|
2020-01-27 11:05:22 +01:00
|
|
|
|
|
|
|
if (!isSelf) {
|
|
|
|
source.sendFeedback(new TranslatableText("commands.disguise.success.other", player.getName(), entity.getName()), true);
|
|
|
|
} else {
|
|
|
|
if (player.getEntityWorld().getGameRules().getBoolean(GameRules.SEND_COMMAND_FEEDBACK)) {
|
2020-06-26 11:44:47 +02:00
|
|
|
player.sendMessage(new TranslatableText("commands.disguise.success.self", entity.getName()), false);
|
2020-01-27 11:05:22 +01:00
|
|
|
}
|
|
|
|
source.sendFeedback(new TranslatableText("commands.disguise.success.otherself", player.getName(), entity.getName()), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int reveal(ServerCommandSource source, PlayerEntity player) {
|
2020-04-15 18:12:00 +02:00
|
|
|
Pony iplayer = Pony.of(player);
|
2021-03-06 10:58:44 +01:00
|
|
|
iplayer.getSpellSlot().get(SpellType.DISGUISE, true).ifPresent(Spell::setDead);
|
2020-01-27 11:05:22 +01:00
|
|
|
|
|
|
|
if (player.getEntityWorld().getGameRules().getBoolean(GameRules.SEND_COMMAND_FEEDBACK)) {
|
2020-06-26 11:44:47 +02:00
|
|
|
player.sendMessage(new TranslatableText("commands.disguise.removed"), false);
|
2020-01-27 11:05:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|