| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import java.util.HexFormat;
|
| 5 | import java.util.Optional;
|
| 6 | import java.util.Set;
|
| 7 | import java.util.function.Consumer;
|
| 8 | import java.util.function.Function;
|
| 9 | import net.minecraft.commands.CommandBuildContext;
|
| 10 | import net.minecraft.commands.CommandSourceStack;
|
| 11 | import net.minecraft.commands.Commands;
|
| 12 | import net.minecraft.commands.arguments.EntityArgument;
|
| 13 | import net.minecraft.commands.arguments.HexColorArgument;
|
| 14 | import net.minecraft.commands.arguments.IdentifierArgument;
|
| 15 | import net.minecraft.commands.arguments.TeamColorArgument;
|
| 16 | import net.minecraft.commands.arguments.WaypointArgument;
|
| 17 | import net.minecraft.core.BlockPos;
|
| 18 | import net.minecraft.network.chat.ClickEvent;
|
| 19 | import net.minecraft.network.chat.Component;
|
| 20 | import net.minecraft.network.chat.ComponentUtils;
|
| 21 | import net.minecraft.network.chat.HoverEvent;
|
| 22 | import net.minecraft.resources.ResourceKey;
|
| 23 | import net.minecraft.server.level.ServerLevel;
|
| 24 | import net.minecraft.util.ARGB;
|
| 25 | import net.minecraft.world.entity.LivingEntity;
|
| 26 | import net.minecraft.world.scores.TeamColor;
|
| 27 | import net.minecraft.world.waypoints.Waypoint;
|
| 28 | import net.minecraft.world.waypoints.WaypointStyleAsset;
|
| 29 | import net.minecraft.world.waypoints.WaypointStyleAssets;
|
| 30 | import net.minecraft.world.waypoints.WaypointTransmitter;
|
| 31 |
|
| 32 | public class WaypointCommand {
|
| 33 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
|
| 34 | dispatcher.register(
|
| 35 | Commands.literal("waypoint")
|
| 36 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 37 | .then(Commands.literal("list").executes(c -> listWaypoints(c.getSource())))
|
| 38 | .then(
|
| 39 | Commands.literal("modify")
|
| 40 | .then(
|
| 41 | Commands.argument("waypoint", EntityArgument.entity())
|
| 42 | .then(
|
| 43 | Commands.literal("color")
|
| 44 | .then(
|
| 45 | Commands.argument("color", TeamColorArgument.teamColor())
|
| 46 | .executes(c -> setWaypointColor(c.getSource(), WaypointArgument.getWaypoint(c, "waypoint"), TeamColorArgument.getTeamColor(c, "color")))
|
| 47 | )
|
| 48 | .then(
|
| 49 | Commands.literal("hex")
|
| 50 | .then(
|
| 51 | Commands.argument("color", HexColorArgument.hexColor())
|
| 52 | .executes(c -> setWaypointColor(c.getSource(), WaypointArgument.getWaypoint(c, "waypoint"), HexColorArgument.getHexColor(c, "color")))
|
| 53 | )
|
| 54 | )
|
| 55 | .then(Commands.literal("reset").executes(c -> resetWaypointColor(c.getSource(), WaypointArgument.getWaypoint(c, "waypoint"))))
|
| 56 | )
|
| 57 | .then(
|
| 58 | Commands.literal("style")
|
| 59 | .then(
|
| 60 | Commands.literal("reset").executes(c -> setWaypointStyle(c.getSource(), WaypointArgument.getWaypoint(c, "waypoint"), WaypointStyleAssets.DEFAULT))
|
| 61 | )
|
| 62 | .then(
|
| 63 | Commands.literal("set")
|
| 64 | .then(
|
| 65 | Commands.argument("style", IdentifierArgument.id())
|
| 66 | .executes(
|
| 67 | c -> setWaypointStyle(
|
| 68 | c.getSource(),
|
| 69 | WaypointArgument.getWaypoint(c, "waypoint"),
|
| 70 | ResourceKey.create(WaypointStyleAssets.ROOT_ID, IdentifierArgument.getId(c, "style"))
|
| 71 | )
|
| 72 | )
|
| 73 | )
|
| 74 | )
|
| 75 | )
|
| 76 | )
|
| 77 | )
|
| 78 | );
|
| 79 | }
|
| 80 |
|
| 81 | private static int setWaypointStyle(final CommandSourceStack source, final WaypointTransmitter waypoint, final ResourceKey<WaypointStyleAsset> style) {
|
| 82 | mutateIcon(source, waypoint, icon -> icon.style = style);
|
| 83 | source.sendSuccess(() -> Component.translatable("commands.waypoint.modify.style"), false);
|
| 84 | return 0;
|
| 85 | }
|
| 86 |
|
| 87 | private static int setWaypointColor(final CommandSourceStack source, final WaypointTransmitter waypoint, final TeamColor color) {
|
| 88 | mutateIcon(source, waypoint, icon -> icon.color = Optional.of(color.rgb()));
|
| 89 | source.sendSuccess(
|
| 90 | () -> Component.translatable("commands.waypoint.modify.color", Component.literal(color.getSerializedName()).withColor(color.textColor())), false
|
| 91 | );
|
| 92 | return 0;
|
| 93 | }
|
| 94 |
|
| 95 | private static int setWaypointColor(final CommandSourceStack source, final WaypointTransmitter waypoint, final Integer color) {
|
| 96 | mutateIcon(source, waypoint, icon -> icon.color = Optional.of(color));
|
| 97 | source.sendSuccess(
|
| 98 | () -> Component.translatable(
|
| 99 | "commands.waypoint.modify.color", Component.literal(HexFormat.of().withUpperCase().toHexDigits(ARGB.color(0, color), 6)).withColor(color)
|
| 100 | ),
|
| 101 | false
|
| 102 | );
|
| 103 | return 0;
|
| 104 | }
|
| 105 |
|
| 106 | private static int resetWaypointColor(final CommandSourceStack source, final WaypointTransmitter waypoint) {
|
| 107 | mutateIcon(source, waypoint, icon -> icon.color = Optional.empty());
|
| 108 | source.sendSuccess(() -> Component.translatable("commands.waypoint.modify.color.reset"), false);
|
| 109 | return 0;
|
| 110 | }
|
| 111 |
|
| 112 | private static int listWaypoints(final CommandSourceStack source) {
|
| 113 | ServerLevel level = source.getLevel();
|
| 114 | Set<WaypointTransmitter> waypoints = level.getWaypointManager().transmitters();
|
| 115 | String dimension = level.dimension().identifier().toString();
|
| 116 | if (waypoints.isEmpty()) {
|
| 117 | source.sendSuccess(() -> Component.translatable("commands.waypoint.list.empty", dimension), false);
|
| 118 | return 0;
|
| 119 | } else {
|
| 120 | Component waypointNames = ComponentUtils.formatList(
|
| 121 | waypoints.stream()
|
| 122 | .map(
|
| 123 | transmitter -> {
|
| 124 | if (transmitter instanceof LivingEntity livingEntity) {
|
| 125 | BlockPos pos = livingEntity.blockPosition();
|
| 126 | return livingEntity.getFeedbackDisplayName()
|
| 127 | .copy()
|
| 128 | .withStyle(
|
| 129 | s -> s.withClickEvent(new ClickEvent.SuggestCommand("/execute in " + dimension + " run tp @s " + pos.getX() + " " + pos.getY() + " " + pos.getZ()))
|
| 130 | .withHoverEvent(new HoverEvent.ShowText(Component.translatable("chat.coordinates.tooltip")))
|
| 131 | .withColor(transmitter.waypointIcon().color.orElse(-1))
|
| 132 | );
|
| 133 | } else {
|
| 134 | return Component.literal(transmitter.toString());
|
| 135 | }
|
| 136 | }
|
| 137 | )
|
| 138 | .toList(),
|
| 139 | Function.identity()
|
| 140 | );
|
| 141 | source.sendSuccess(() -> Component.translatable("commands.waypoint.list.success", waypoints.size(), dimension, waypointNames), false);
|
| 142 | return waypoints.size();
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | private static void mutateIcon(final CommandSourceStack source, final WaypointTransmitter waypoint, final Consumer<Waypoint.Icon> iconConsumer) {
|
| 147 | ServerLevel level = source.getLevel();
|
| 148 | level.getWaypointManager().untrackWaypoint(waypoint);
|
| 149 | iconConsumer.accept(waypoint.waypointIcon());
|
| 150 | level.getWaypointManager().trackWaypoint(waypoint);
|
| 151 | }
|
| 152 | }
|
| 153 |
|