| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.arguments.IntegerArgumentType;
|
| 5 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 6 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 7 | import com.mojang.brigadier.tree.LiteralCommandNode;
|
| 8 | import java.util.Collection;
|
| 9 | import java.util.function.BiConsumer;
|
| 10 | import java.util.function.BiPredicate;
|
| 11 | import java.util.function.ToIntFunction;
|
| 12 | import net.minecraft.commands.CommandSourceStack;
|
| 13 | import net.minecraft.commands.Commands;
|
| 14 | import net.minecraft.commands.arguments.EntityArgument;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.server.level.ServerPlayer;
|
| 17 | import net.minecraft.util.Mth;
|
| 18 | import net.minecraft.world.entity.player.Player;
|
| 19 |
|
| 20 | public class ExperienceCommand {
|
| 21 | private static final SimpleCommandExceptionType ERROR_SET_POINTS_INVALID = new SimpleCommandExceptionType(
|
| 22 | Component.translatable("commands.experience.set.points.invalid")
|
| 23 | );
|
| 24 |
|
| 25 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 26 | LiteralCommandNode<CommandSourceStack> command = dispatcher.register(
|
| 27 | Commands.literal("experience")
|
| 28 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 29 | .then(
|
| 30 | Commands.literal("add")
|
| 31 | .then(
|
| 32 | Commands.argument("target", EntityArgument.players())
|
| 33 | .then(
|
| 34 | Commands.argument("amount", IntegerArgumentType.integer())
|
| 35 | .executes(
|
| 36 | c -> addExperience(c.getSource(), EntityArgument.getPlayers(c, "target"), IntegerArgumentType.getInteger(c, "amount"), ExperienceCommand.Type.POINTS)
|
| 37 | )
|
| 38 | .then(
|
| 39 | Commands.literal("points")
|
| 40 | .executes(
|
| 41 | c -> addExperience(
|
| 42 | c.getSource(), EntityArgument.getPlayers(c, "target"), IntegerArgumentType.getInteger(c, "amount"), ExperienceCommand.Type.POINTS
|
| 43 | )
|
| 44 | )
|
| 45 | )
|
| 46 | .then(
|
| 47 | Commands.literal("levels")
|
| 48 | .executes(
|
| 49 | c -> addExperience(
|
| 50 | c.getSource(), EntityArgument.getPlayers(c, "target"), IntegerArgumentType.getInteger(c, "amount"), ExperienceCommand.Type.LEVELS
|
| 51 | )
|
| 52 | )
|
| 53 | )
|
| 54 | )
|
| 55 | )
|
| 56 | )
|
| 57 | .then(
|
| 58 | Commands.literal("set")
|
| 59 | .then(
|
| 60 | Commands.argument("target", EntityArgument.players())
|
| 61 | .then(
|
| 62 | Commands.argument("amount", IntegerArgumentType.integer(0))
|
| 63 | .executes(
|
| 64 | c -> setExperience(c.getSource(), EntityArgument.getPlayers(c, "target"), IntegerArgumentType.getInteger(c, "amount"), ExperienceCommand.Type.POINTS)
|
| 65 | )
|
| 66 | .then(
|
| 67 | Commands.literal("points")
|
| 68 | .executes(
|
| 69 | c -> setExperience(
|
| 70 | c.getSource(), EntityArgument.getPlayers(c, "target"), IntegerArgumentType.getInteger(c, "amount"), ExperienceCommand.Type.POINTS
|
| 71 | )
|
| 72 | )
|
| 73 | )
|
| 74 | .then(
|
| 75 | Commands.literal("levels")
|
| 76 | .executes(
|
| 77 | c -> setExperience(
|
| 78 | c.getSource(), EntityArgument.getPlayers(c, "target"), IntegerArgumentType.getInteger(c, "amount"), ExperienceCommand.Type.LEVELS
|
| 79 | )
|
| 80 | )
|
| 81 | )
|
| 82 | )
|
| 83 | )
|
| 84 | )
|
| 85 | .then(
|
| 86 | Commands.literal("query")
|
| 87 | .then(
|
| 88 | Commands.argument("target", EntityArgument.player())
|
| 89 | .then(Commands.literal("points").executes(c -> queryExperience(c.getSource(), EntityArgument.getPlayer(c, "target"), ExperienceCommand.Type.POINTS)))
|
| 90 | .then(Commands.literal("levels").executes(c -> queryExperience(c.getSource(), EntityArgument.getPlayer(c, "target"), ExperienceCommand.Type.LEVELS)))
|
| 91 | )
|
| 92 | )
|
| 93 | );
|
| 94 | dispatcher.register(Commands.literal("xp").requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS)).redirect(command));
|
| 95 | }
|
| 96 |
|
| 97 | private static int queryExperience(final CommandSourceStack source, final ServerPlayer target, final ExperienceCommand.Type type) {
|
| 98 | int result = type.query.applyAsInt(target);
|
| 99 | source.sendSuccess(() -> type.queryResponse(target, result), false);
|
| 100 | return result;
|
| 101 | }
|
| 102 |
|
| 103 | private static int addExperience(
|
| 104 | final CommandSourceStack source, final Collection<? extends ServerPlayer> players, final int amount, final ExperienceCommand.Type type
|
| 105 | ) throws CommandSyntaxException {
|
| 106 | CommandResponseTracker<ServerPlayer> tracker = CommandResponseTracker.create();
|
| 107 |
|
| 108 | for (ServerPlayer player : players) {
|
| 109 | type.add.accept(player, amount);
|
| 110 | tracker.track(player);
|
| 111 | }
|
| 112 |
|
| 113 | return tracker.sendFeedback(source, true, type.addResponse, amount);
|
| 114 | }
|
| 115 |
|
| 116 | private static int setExperience(
|
| 117 | final CommandSourceStack source, final Collection<? extends ServerPlayer> players, final int amount, final ExperienceCommand.Type type
|
| 118 | ) throws CommandSyntaxException {
|
| 119 | CommandResponseTracker<ServerPlayer> tracker = CommandResponseTracker.create();
|
| 120 |
|
| 121 | for (ServerPlayer player : players) {
|
| 122 | tracker.track(player, type.set.test(player, amount));
|
| 123 | }
|
| 124 |
|
| 125 | return tracker.sendFeedback(source, true, type.setResponse, amount);
|
| 126 | }
|
| 127 |
|
| 128 | private enum Type {
|
| 129 | POINTS("points", Player::giveExperiencePoints, (p, a) -> {
|
| 130 | if (a >= p.getXpNeededForNextLevel()) {
|
| 131 | return false;
|
| 132 | }
|
| 133 |
|
| 134 | p.setExperiencePoints(a);
|
| 135 | return true;
|
| 136 | }, p -> Mth.floor(p.experienceProgress * p.getXpNeededForNextLevel())),
|
| 137 | LEVELS("levels", ServerPlayer::giveExperienceLevels, (p, a) -> {
|
| 138 | p.setExperienceLevels(a);
|
| 139 | return true;
|
| 140 | }, p -> p.experienceLevel);
|
| 141 |
|
| 142 | public final BiConsumer<ServerPlayer, Integer> add;
|
| 143 | public final BiPredicate<ServerPlayer, Integer> set;
|
| 144 | public final ToIntFunction<ServerPlayer> query;
|
| 145 | public final String queryTranslationKey;
|
| 146 | public final CommandResponseTracker.MessagesWithArg<ServerPlayer, Integer> addResponse;
|
| 147 | public final CommandResponseTracker.MessagesWithArg<ServerPlayer, Integer> setResponse;
|
| 148 |
|
| 149 | Type(final String name, final BiConsumer<ServerPlayer, Integer> add, final BiPredicate<ServerPlayer, Integer> set, final ToIntFunction<ServerPlayer> query) {
|
| 150 | this.add = add;
|
| 151 | this.addResponse = CommandResponseTracker.messages(
|
| 152 | (player, var2, amount) -> Component.translatable("commands.experience.add." + name + ".success.single", amount, player.getDisplayName()),
|
| 153 | (playerCount, var2, amount) -> Component.translatable("commands.experience.add." + name + ".success.multiple", amount, playerCount)
|
| 154 | );
|
| 155 | this.set = set;
|
| 156 | this.setResponse = CommandResponseTracker.messages(
|
| 157 | ExperienceCommand.ERROR_SET_POINTS_INVALID,
|
| 158 | (player, var2, amount) -> Component.translatable("commands.experience.set." + name + ".success.single", amount, player.getDisplayName()),
|
| 159 | (playerCount, var2, amount) -> Component.translatable("commands.experience.set." + name + ".success.multiple", amount, playerCount)
|
| 160 | );
|
| 161 | this.query = query;
|
| 162 | this.queryTranslationKey = "commands.experience.query." + name;
|
| 163 | }
|
| 164 |
|
| 165 | public Component queryResponse(final ServerPlayer target, final int result) {
|
| 166 | return Component.translatable(this.queryTranslationKey, target.getDisplayName(), result);
|
| 167 | }
|
| 168 | }
|
| 169 | }
|
| 170 |
|