| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.arguments.DoubleArgumentType;
|
| 5 | import com.mojang.brigadier.arguments.FloatArgumentType;
|
| 6 | import com.mojang.brigadier.arguments.IntegerArgumentType;
|
| 7 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 8 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 9 | import java.util.Locale;
|
| 10 | import net.minecraft.commands.CommandSourceStack;
|
| 11 | import net.minecraft.commands.Commands;
|
| 12 | import net.minecraft.commands.arguments.TimeArgument;
|
| 13 | import net.minecraft.commands.arguments.coordinates.Vec2Argument;
|
| 14 | import net.minecraft.network.chat.Component;
|
| 15 | import net.minecraft.server.level.ServerLevel;
|
| 16 | import net.minecraft.util.Mth;
|
| 17 | import net.minecraft.world.level.border.WorldBorder;
|
| 18 | import net.minecraft.world.phys.Vec2;
|
| 19 |
|
| 20 | public class WorldBorderCommand {
|
| 21 | private static final SimpleCommandExceptionType ERROR_SAME_CENTER = new SimpleCommandExceptionType(
|
| 22 | Component.translatable("commands.worldborder.center.failed")
|
| 23 | );
|
| 24 | private static final SimpleCommandExceptionType ERROR_SAME_SIZE = new SimpleCommandExceptionType(
|
| 25 | Component.translatable("commands.worldborder.set.failed.nochange")
|
| 26 | );
|
| 27 | private static final SimpleCommandExceptionType ERROR_TOO_SMALL = new SimpleCommandExceptionType(
|
| 28 | Component.translatable("commands.worldborder.set.failed.small")
|
| 29 | );
|
| 30 | private static final SimpleCommandExceptionType ERROR_TOO_BIG = new SimpleCommandExceptionType(
|
| 31 | Component.translatable("commands.worldborder.set.failed.big", 5.999997E7F)
|
| 32 | );
|
| 33 | private static final SimpleCommandExceptionType ERROR_TOO_FAR_OUT = new SimpleCommandExceptionType(
|
| 34 | Component.translatable("commands.worldborder.set.failed.far", 2.9999984E7)
|
| 35 | );
|
| 36 | private static final SimpleCommandExceptionType ERROR_SAME_WARNING_TIME = new SimpleCommandExceptionType(
|
| 37 | Component.translatable("commands.worldborder.warning.time.failed")
|
| 38 | );
|
| 39 | private static final SimpleCommandExceptionType ERROR_SAME_WARNING_DISTANCE = new SimpleCommandExceptionType(
|
| 40 | Component.translatable("commands.worldborder.warning.distance.failed")
|
| 41 | );
|
| 42 | private static final SimpleCommandExceptionType ERROR_SAME_DAMAGE_BUFFER = new SimpleCommandExceptionType(
|
| 43 | Component.translatable("commands.worldborder.damage.buffer.failed")
|
| 44 | );
|
| 45 | private static final SimpleCommandExceptionType ERROR_SAME_DAMAGE_AMOUNT = new SimpleCommandExceptionType(
|
| 46 | Component.translatable("commands.worldborder.damage.amount.failed")
|
| 47 | );
|
| 48 |
|
| 49 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 50 | dispatcher.register(
|
| 51 | Commands.literal("worldborder")
|
| 52 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 53 | .then(
|
| 54 | Commands.literal("add")
|
| 55 | .then(
|
| 56 | Commands.argument("distance", DoubleArgumentType.doubleArg(-5.999997E7F, 5.999997E7F))
|
| 57 | .executes(c -> setSize(c.getSource(), c.getSource().getLevel().getWorldBorder().getSize() + DoubleArgumentType.getDouble(c, "distance"), 0L))
|
| 58 | .then(
|
| 59 | Commands.argument("time", TimeArgument.time(0))
|
| 60 | .executes(
|
| 61 | c -> setSize(
|
| 62 | c.getSource(),
|
| 63 | c.getSource().getLevel().getWorldBorder().getSize() + DoubleArgumentType.getDouble(c, "distance"),
|
| 64 | c.getSource().getLevel().getWorldBorder().getLerpTime() + IntegerArgumentType.getInteger(c, "time")
|
| 65 | )
|
| 66 | )
|
| 67 | )
|
| 68 | )
|
| 69 | )
|
| 70 | .then(
|
| 71 | Commands.literal("set")
|
| 72 | .then(
|
| 73 | Commands.argument("distance", DoubleArgumentType.doubleArg(-5.999997E7F, 5.999997E7F))
|
| 74 | .executes(c -> setSize(c.getSource(), DoubleArgumentType.getDouble(c, "distance"), 0L))
|
| 75 | .then(
|
| 76 | Commands.argument("time", TimeArgument.time(0))
|
| 77 | .executes(c -> setSize(c.getSource(), DoubleArgumentType.getDouble(c, "distance"), IntegerArgumentType.getInteger(c, "time")))
|
| 78 | )
|
| 79 | )
|
| 80 | )
|
| 81 | .then(
|
| 82 | Commands.literal("center").then(Commands.argument("pos", Vec2Argument.vec2()).executes(c -> setCenter(c.getSource(), Vec2Argument.getVec2(c, "pos"))))
|
| 83 | )
|
| 84 | .then(
|
| 85 | Commands.literal("damage")
|
| 86 | .then(
|
| 87 | Commands.literal("amount")
|
| 88 | .then(
|
| 89 | Commands.argument("damagePerBlock", FloatArgumentType.floatArg(0.0F))
|
| 90 | .executes(c -> setDamageAmount(c.getSource(), FloatArgumentType.getFloat(c, "damagePerBlock")))
|
| 91 | )
|
| 92 | )
|
| 93 | .then(
|
| 94 | Commands.literal("buffer")
|
| 95 | .then(
|
| 96 | Commands.argument("distance", FloatArgumentType.floatArg(0.0F))
|
| 97 | .executes(c -> setDamageBuffer(c.getSource(), FloatArgumentType.getFloat(c, "distance")))
|
| 98 | )
|
| 99 | )
|
| 100 | )
|
| 101 | .then(Commands.literal("get").executes(c -> getSize(c.getSource())))
|
| 102 | .then(
|
| 103 | Commands.literal("warning")
|
| 104 | .then(
|
| 105 | Commands.literal("distance")
|
| 106 | .then(
|
| 107 | Commands.argument("distance", IntegerArgumentType.integer(0))
|
| 108 | .executes(c -> setWarningDistance(c.getSource(), IntegerArgumentType.getInteger(c, "distance")))
|
| 109 | )
|
| 110 | )
|
| 111 | .then(
|
| 112 | Commands.literal("time")
|
| 113 | .then(Commands.argument("time", TimeArgument.time(0)).executes(c -> setWarningTime(c.getSource(), IntegerArgumentType.getInteger(c, "time"))))
|
| 114 | )
|
| 115 | )
|
| 116 | );
|
| 117 | }
|
| 118 |
|
| 119 | private static int setDamageBuffer(final CommandSourceStack source, final float distance) throws CommandSyntaxException {
|
| 120 | WorldBorder border = source.getLevel().getWorldBorder();
|
| 121 | if (border.getSafeZone() == distance) {
|
| 122 | throw ERROR_SAME_DAMAGE_BUFFER.create();
|
| 123 | }
|
| 124 |
|
| 125 | border.setSafeZone(distance);
|
| 126 | source.sendSuccess(() -> Component.translatable("commands.worldborder.damage.buffer.success", String.format(Locale.ROOT, "%.2f", distance)), true);
|
| 127 | return (int)distance;
|
| 128 | }
|
| 129 |
|
| 130 | private static int setDamageAmount(final CommandSourceStack source, final float damagePerBlock) throws CommandSyntaxException {
|
| 131 | WorldBorder border = source.getLevel().getWorldBorder();
|
| 132 | if (border.getDamagePerBlock() == damagePerBlock) {
|
| 133 | throw ERROR_SAME_DAMAGE_AMOUNT.create();
|
| 134 | }
|
| 135 |
|
| 136 | border.setDamagePerBlock(damagePerBlock);
|
| 137 | source.sendSuccess(() -> Component.translatable("commands.worldborder.damage.amount.success", String.format(Locale.ROOT, "%.2f", damagePerBlock)), true);
|
| 138 | return (int)damagePerBlock;
|
| 139 | }
|
| 140 |
|
| 141 | private static int setWarningTime(final CommandSourceStack source, final int ticks) throws CommandSyntaxException {
|
| 142 | WorldBorder border = source.getLevel().getWorldBorder();
|
| 143 | if (border.getWarningTime() == ticks) {
|
| 144 | throw ERROR_SAME_WARNING_TIME.create();
|
| 145 | }
|
| 146 |
|
| 147 | border.setWarningTime(ticks);
|
| 148 | source.sendSuccess(() -> Component.translatable("commands.worldborder.warning.time.success", formatTicksToSeconds(ticks)), true);
|
| 149 | return ticks;
|
| 150 | }
|
| 151 |
|
| 152 | private static int setWarningDistance(final CommandSourceStack source, final int distance) throws CommandSyntaxException {
|
| 153 | WorldBorder border = source.getLevel().getWorldBorder();
|
| 154 | if (border.getWarningBlocks() == distance) {
|
| 155 | throw ERROR_SAME_WARNING_DISTANCE.create();
|
| 156 | }
|
| 157 |
|
| 158 | border.setWarningBlocks(distance);
|
| 159 | source.sendSuccess(() -> Component.translatable("commands.worldborder.warning.distance.success", distance), true);
|
| 160 | return distance;
|
| 161 | }
|
| 162 |
|
| 163 | private static int getSize(final CommandSourceStack source) {
|
| 164 | double size = source.getLevel().getWorldBorder().getSize();
|
| 165 | source.sendSuccess(() -> Component.translatable("commands.worldborder.get", String.format(Locale.ROOT, "%.0f", size)), false);
|
| 166 | return Mth.floor(size + 0.5);
|
| 167 | }
|
| 168 |
|
| 169 | private static int setCenter(final CommandSourceStack source, final Vec2 center) throws CommandSyntaxException {
|
| 170 | WorldBorder border = source.getLevel().getWorldBorder();
|
| 171 | if (border.getCenterX() == center.x && border.getCenterZ() == center.y) {
|
| 172 | throw ERROR_SAME_CENTER.create();
|
| 173 | } else if (!(Math.abs(center.x) > 2.9999984E7) && !(Math.abs(center.y) > 2.9999984E7)) {
|
| 174 | border.setCenter(center.x, center.y);
|
| 175 | source.sendSuccess(
|
| 176 | () -> Component.translatable(
|
| 177 | "commands.worldborder.center.success", String.format(Locale.ROOT, "%.2f", center.x), String.format(Locale.ROOT, "%.2f", center.y)
|
| 178 | ),
|
| 179 | true
|
| 180 | );
|
| 181 | return 0;
|
| 182 | } else {
|
| 183 | throw ERROR_TOO_FAR_OUT.create();
|
| 184 | }
|
| 185 | }
|
| 186 |
|
| 187 | private static int setSize(final CommandSourceStack source, final double distance, final long ticks) throws CommandSyntaxException {
|
| 188 | ServerLevel level = source.getLevel();
|
| 189 | WorldBorder border = level.getWorldBorder();
|
| 190 | double current = border.getSize();
|
| 191 | if (current == distance) {
|
| 192 | throw ERROR_SAME_SIZE.create();
|
| 193 | }
|
| 194 |
|
| 195 | if (distance < 1.0) {
|
| 196 | throw ERROR_TOO_SMALL.create();
|
| 197 | }
|
| 198 |
|
| 199 | if (distance > 5.999997E7F) {
|
| 200 | throw ERROR_TOO_BIG.create();
|
| 201 | }
|
| 202 |
|
| 203 | String formattedDistance = String.format(Locale.ROOT, "%.1f", distance);
|
| 204 | if (ticks > 0L) {
|
| 205 | border.lerpSizeBetween(current, distance, ticks, level.getGameTime());
|
| 206 | if (distance > current) {
|
| 207 | source.sendSuccess(() -> Component.translatable("commands.worldborder.set.grow", formattedDistance, formatTicksToSeconds(ticks)), true);
|
| 208 | } else {
|
| 209 | source.sendSuccess(() -> Component.translatable("commands.worldborder.set.shrink", formattedDistance, formatTicksToSeconds(ticks)), true);
|
| 210 | }
|
| 211 | } else {
|
| 212 | border.setSize(distance);
|
| 213 | source.sendSuccess(() -> Component.translatable("commands.worldborder.set.immediate", formattedDistance), true);
|
| 214 | }
|
| 215 |
|
| 216 | return (int)(distance - current);
|
| 217 | }
|
| 218 |
|
| 219 | private static String formatTicksToSeconds(final long ticks) {
|
| 220 | return String.format(Locale.ROOT, "%.2f", ticks / 20.0);
|
| 221 | }
|
| 222 | }
|
| 223 |
|