121 lines
7.6 KiB
Java
121 lines
7.6 KiB
Java
package com.thebrokenrail.sorcerycraft.command;
|
|
|
|
import com.mojang.brigadier.CommandDispatcher;
|
|
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
|
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
|
import com.thebrokenrail.sorcerycraft.SorceryCraft;
|
|
import com.thebrokenrail.sorcerycraft.spell.util.SpellPlayerEntity;
|
|
import com.thebrokenrail.sorcerycraft.spell.util.SpellTag;
|
|
import net.minecraft.command.arguments.EntityArgumentType;
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.server.command.CommandManager;
|
|
import net.minecraft.server.command.ServerCommandSource;
|
|
import net.minecraft.text.TranslatableText;
|
|
import net.minecraft.util.Formatting;
|
|
import net.minecraft.util.Identifier;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public class SpellCommand {
|
|
private static final DynamicCommandExceptionType NOT_HOLDING_SPELL_EXCEPTION = new DynamicCommandExceptionType(object -> new TranslatableText("command."+ SorceryCraft.NAMESPACE + ".spell.not_holding_spell", object));
|
|
|
|
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
|
|
dispatcher.register(CommandManager.literal("spell")
|
|
.requires(source -> source.hasPermissionLevel(4))
|
|
.then(CommandManager.literal("list")
|
|
.then(CommandManager.argument("player", EntityArgumentType.player())
|
|
.executes(ctx -> {
|
|
PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
|
|
ctx.getSource().sendFeedback(new TranslatableText("command." + SorceryCraft.NAMESPACE + ".spell.listing_spells", player.getDisplayName()), false);
|
|
SpellPlayerEntity spellPlayer = (SpellPlayerEntity) player;
|
|
Map<Identifier, Integer> spells = spellPlayer.getDiscoveredSpells();
|
|
for (Map.Entry<Identifier, Integer> entry : spells.entrySet()) {
|
|
ctx.getSource().sendFeedback(SpellTag.getTranslatedSpell(entry.getKey(), entry.getValue(), true).formatted(Formatting.YELLOW), false);
|
|
}
|
|
return 0;
|
|
})
|
|
)
|
|
)
|
|
.then(CommandManager.literal("clear")
|
|
.then(CommandManager.argument("player", EntityArgumentType.player())
|
|
.executes(ctx -> {
|
|
PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
|
|
SpellPlayerEntity spellPlayer = (SpellPlayerEntity) player;
|
|
spellPlayer.setDiscoveredSpells(new HashMap<>());
|
|
ctx.getSource().sendFeedback(new TranslatableText("command." + SorceryCraft.NAMESPACE + ".spell.cleared_spells", player.getDisplayName()), true);
|
|
return 1;
|
|
})
|
|
)
|
|
)
|
|
.then(CommandManager.literal("discover")
|
|
.then(CommandManager.argument("player", EntityArgumentType.player())
|
|
.then(CommandManager.argument("spell", SpellArgumentType.spell())
|
|
.then(CommandManager.argument("level", IntegerArgumentType.integer())
|
|
.executes(ctx -> {
|
|
PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
|
|
Identifier spell = SpellArgumentType.getSpell(ctx, "spell");
|
|
int level = IntegerArgumentType.getInteger(ctx, "level") - 1;
|
|
Map<Identifier, Integer> spellMap = new HashMap<>();
|
|
spellMap.put(spell, level);
|
|
SpellTag.learnSpells(player, spellMap);
|
|
return 1;
|
|
})
|
|
)
|
|
)
|
|
)
|
|
)
|
|
.then(CommandManager.literal("apply")
|
|
.then(CommandManager.argument("player", EntityArgumentType.player())
|
|
.then(CommandManager.argument("spell", SpellArgumentType.spell())
|
|
.then(CommandManager.argument("level", IntegerArgumentType.integer())
|
|
.executes(ctx -> {
|
|
PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
|
|
Identifier spell = SpellArgumentType.getSpell(ctx, "spell");
|
|
int level = IntegerArgumentType.getInteger(ctx, "level") - 1;
|
|
|
|
ItemStack stack = player.getMainHandStack();
|
|
|
|
if (stack.getItem() != SorceryCraft.SPELL_ITEM) {
|
|
throw NOT_HOLDING_SPELL_EXCEPTION.create(player);
|
|
}
|
|
|
|
Map<Identifier, Integer> spellMap = SpellTag.getSpells(stack);
|
|
spellMap.put(spell, level);
|
|
SpellTag.setSpells(stack, spellMap);
|
|
|
|
ctx.getSource().sendFeedback(new TranslatableText("command.sorcerycraft.spell.applied_spell", SpellTag.getTranslatedSpell(spell, level, true)), true);
|
|
return 1;
|
|
})
|
|
)
|
|
)
|
|
)
|
|
)
|
|
.then(CommandManager.literal("remove")
|
|
.then(CommandManager.argument("player", EntityArgumentType.player())
|
|
.then(CommandManager.argument("spell", SpellArgumentType.spell())
|
|
.executes(ctx -> {
|
|
PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
|
|
Identifier spell = SpellArgumentType.getSpell(ctx, "spell");
|
|
|
|
ItemStack stack = player.getMainHandStack();
|
|
|
|
if (stack.getItem() != SorceryCraft.SPELL_ITEM) {
|
|
throw NOT_HOLDING_SPELL_EXCEPTION.create(player);
|
|
}
|
|
|
|
Map<Identifier, Integer> spellMap = SpellTag.getSpells(stack);
|
|
spellMap.remove(spell);
|
|
SpellTag.setSpells(stack, spellMap);
|
|
|
|
ctx.getSource().sendFeedback(new TranslatableText("command.sorcerycraft.spell.removed_spell", SpellTag.getTranslatedSpell(spell, 0, false)), true);
|
|
return 1;
|
|
})
|
|
)
|
|
)
|
|
)
|
|
);
|
|
}
|
|
}
|