| 1 | package net.minecraft.world.level.saveddata.maps;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import com.mojang.datafixers.util.Pair;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 8 | import io.netty.buffer.ByteBuf;
|
| 9 | import java.nio.ByteBuffer;
|
| 10 | import java.util.Collection;
|
| 11 | import java.util.Iterator;
|
| 12 | import java.util.List;
|
| 13 | import java.util.Map;
|
| 14 | import java.util.Objects;
|
| 15 | import java.util.Optional;
|
| 16 | import java.util.function.Predicate;
|
| 17 | import net.minecraft.core.BlockPos;
|
| 18 | import net.minecraft.core.Holder;
|
| 19 | import net.minecraft.core.component.DataComponents;
|
| 20 | import net.minecraft.network.FriendlyByteBuf;
|
| 21 | import net.minecraft.network.chat.Component;
|
| 22 | import net.minecraft.network.codec.StreamCodec;
|
| 23 | import net.minecraft.network.protocol.Packet;
|
| 24 | import net.minecraft.network.protocol.game.ClientboundMapItemDataPacket;
|
| 25 | import net.minecraft.resources.Identifier;
|
| 26 | import net.minecraft.resources.ResourceKey;
|
| 27 | import net.minecraft.tags.ItemTags;
|
| 28 | import net.minecraft.util.Mth;
|
| 29 | import net.minecraft.util.datafix.DataFixTypes;
|
| 30 | import net.minecraft.world.entity.EquipmentSlot;
|
| 31 | import net.minecraft.world.entity.decoration.ItemFrame;
|
| 32 | import net.minecraft.world.entity.player.Player;
|
| 33 | import net.minecraft.world.item.ItemStack;
|
| 34 | import net.minecraft.world.item.component.MapDecorations;
|
| 35 | import net.minecraft.world.level.BlockGetter;
|
| 36 | import net.minecraft.world.level.Level;
|
| 37 | import net.minecraft.world.level.LevelAccessor;
|
| 38 | import net.minecraft.world.level.saveddata.SavedData;
|
| 39 | import net.minecraft.world.level.saveddata.SavedDataType;
|
| 40 | import org.jspecify.annotations.Nullable;
|
| 41 |
|
| 42 | public class MapItemSavedData extends SavedData {
|
| 43 | private static final int MAP_SIZE = 128;
|
| 44 | private static final int HALF_MAP_SIZE = 64;
|
| 45 | public static final int MAX_SCALE = 4;
|
| 46 | public static final int TRACKED_DECORATION_LIMIT = 256;
|
| 47 | private static final String FRAME_PREFIX = "frame-";
|
| 48 | public static final Codec<MapItemSavedData> CODEC = RecordCodecBuilder.create(
|
| 49 | i -> i.group(
|
| 50 | Level.RESOURCE_KEY_CODEC.fieldOf("dimension").forGetter(m -> m.dimension),
|
| 51 | Codec.INT.fieldOf("xCenter").forGetter(m -> m.centerX),
|
| 52 | Codec.INT.fieldOf("zCenter").forGetter(m -> m.centerZ),
|
| 53 | Codec.BYTE.optionalFieldOf("scale", (byte)0).forGetter(m -> m.scale),
|
| 54 | Codec.BYTE_BUFFER.fieldOf("colors").forGetter(m -> ByteBuffer.wrap(m.colors)),
|
| 55 | Codec.BOOL.optionalFieldOf("trackingPosition", true).forGetter(m -> m.trackingPosition),
|
| 56 | Codec.BOOL.optionalFieldOf("unlimitedTracking", false).forGetter(m -> m.unlimitedTracking),
|
| 57 | Codec.BOOL.optionalFieldOf("locked", false).forGetter(m -> m.locked),
|
| 58 | MapBanner.CODEC.listOf().optionalFieldOf("banners", List.of()).forGetter(m -> List.copyOf(m.bannerMarkers.values())),
|
| 59 | MapFrame.CODEC.listOf().optionalFieldOf("frames", List.of()).forGetter(m -> List.copyOf(m.frameMarkers.values()))
|
| 60 | )
|
| 61 | .apply(i, MapItemSavedData::new)
|
| 62 | );
|
| 63 | public final int centerX;
|
| 64 | public final int centerZ;
|
| 65 | public final ResourceKey<Level> dimension;
|
| 66 | private final boolean trackingPosition;
|
| 67 | private final boolean unlimitedTracking;
|
| 68 | public final byte scale;
|
| 69 | public byte[] colors = new byte[16384];
|
| 70 | public final boolean locked;
|
| 71 | private final List<MapItemSavedData.HoldingPlayer> carriedBy = Lists.newArrayList();
|
| 72 | private final Map<Player, MapItemSavedData.HoldingPlayer> carriedByPlayers = Maps.newHashMap();
|
| 73 | private final Map<String, MapBanner> bannerMarkers = Maps.newHashMap();
|
| 74 | private final Map<String, MapDecoration> decorations = Maps.newLinkedHashMap();
|
| 75 | private final Map<String, MapFrame> frameMarkers = Maps.newHashMap();
|
| 76 | private int trackedDecorationCount;
|
| 77 |
|
| 78 | public static SavedDataType<MapItemSavedData> type(final MapId id) {
|
| 79 | return new SavedDataType<>(Identifier.withDefaultNamespace(id.key()), () -> {
|
| 80 | throw new IllegalStateException("Should never create an empty map saved data");
|
| 81 | }, CODEC, DataFixTypes.SAVED_DATA_MAP_DATA);
|
| 82 | }
|
| 83 |
|
| 84 | private MapItemSavedData(
|
| 85 | final int centerX,
|
| 86 | final int centerZ,
|
| 87 | final byte scale,
|
| 88 | final boolean trackingPosition,
|
| 89 | final boolean unlimitedTracking,
|
| 90 | final boolean locked,
|
| 91 | final ResourceKey<Level> dimension
|
| 92 | ) {
|
| 93 | this.scale = scale;
|
| 94 | this.centerX = centerX;
|
| 95 | this.centerZ = centerZ;
|
| 96 | this.dimension = dimension;
|
| 97 | this.trackingPosition = trackingPosition;
|
| 98 | this.unlimitedTracking = unlimitedTracking;
|
| 99 | this.locked = locked;
|
| 100 | }
|
| 101 |
|
| 102 | private MapItemSavedData(
|
| 103 | final ResourceKey<Level> dimension,
|
| 104 | final int centerX,
|
| 105 | final int centerZ,
|
| 106 | final byte scale,
|
| 107 | final ByteBuffer colors,
|
| 108 | final boolean trackingPosition,
|
| 109 | final boolean unlimitedTracking,
|
| 110 | final boolean locked,
|
| 111 | final List<MapBanner> banners,
|
| 112 | final List<MapFrame> frames
|
| 113 | ) {
|
| 114 | this(centerX, centerZ, (byte)Mth.clamp(scale, 0, 4), trackingPosition, unlimitedTracking, locked, dimension);
|
| 115 | if (colors.array().length == 16384) {
|
| 116 | this.colors = colors.array();
|
| 117 | }
|
| 118 |
|
| 119 | for (MapBanner banner : banners) {
|
| 120 | this.bannerMarkers.put(banner.getId(), banner);
|
| 121 | this.addDecoration(banner.getDecoration(), null, banner.getId(), banner.pos().getX(), banner.pos().getZ(), 180.0, banner.name().orElse(null));
|
| 122 | }
|
| 123 |
|
| 124 | for (MapFrame frame : frames) {
|
| 125 | this.frameMarkers.put(frame.getId(), frame);
|
| 126 | this.addDecoration(MapDecorationTypes.FRAME, null, getFrameKey(frame.entityId()), frame.pos().getX(), frame.pos().getZ(), frame.rotation(), null);
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | public static MapItemSavedData createFresh(
|
| 131 | final double originX,
|
| 132 | final double originY,
|
| 133 | final byte scale,
|
| 134 | final boolean trackingPosition,
|
| 135 | final boolean unlimitedTracking,
|
| 136 | final ResourceKey<Level> dimension
|
| 137 | ) {
|
| 138 | int size = 128 * (1 << scale);
|
| 139 | int areaX = Mth.floor((originX + 64.0) / size);
|
| 140 | int areaZ = Mth.floor((originY + 64.0) / size);
|
| 141 | int x = areaX * size + size / 2 - 64;
|
| 142 | int z = areaZ * size + size / 2 - 64;
|
| 143 | return new MapItemSavedData(x, z, scale, trackingPosition, unlimitedTracking, false, dimension);
|
| 144 | }
|
| 145 |
|
| 146 | public static MapItemSavedData createForClient(final byte scale, final boolean isLocked, final ResourceKey<Level> dimension) {
|
| 147 | return new MapItemSavedData(0, 0, scale, false, false, isLocked, dimension);
|
| 148 | }
|
| 149 |
|
| 150 | public MapItemSavedData locked() {
|
| 151 | MapItemSavedData result = new MapItemSavedData(this.centerX, this.centerZ, this.scale, this.trackingPosition, this.unlimitedTracking, true, this.dimension);
|
| 152 | result.bannerMarkers.putAll(this.bannerMarkers);
|
| 153 | result.decorations.putAll(this.decorations);
|
| 154 | result.trackedDecorationCount = this.trackedDecorationCount;
|
| 155 | System.arraycopy(this.colors, 0, result.colors, 0, this.colors.length);
|
| 156 | return result;
|
| 157 | }
|
| 158 |
|
| 159 | public MapItemSavedData scaled() {
|
| 160 | return createFresh(this.centerX, this.centerZ, (byte)Mth.clamp(this.scale + 1, 0, 4), this.trackingPosition, this.unlimitedTracking, this.dimension);
|
| 161 | }
|
| 162 |
|
| 163 | private static Predicate<ItemStack> mapMatcher(final ItemStack mapStack) {
|
| 164 | MapId mapId = mapStack.get(DataComponents.MAP_ID);
|
| 165 | return stack -> stack == mapStack ? true : stack.is(mapStack.getItem()) && Objects.equals(mapId, stack.get(DataComponents.MAP_ID));
|
| 166 | }
|
| 167 |
|
| 168 | public void tickCarriedBy(final Player tickingPlayer, final ItemStack itemStack, final @Nullable ItemFrame placedInFrame) {
|
| 169 | if (!this.carriedByPlayers.containsKey(tickingPlayer)) {
|
| 170 | MapItemSavedData.HoldingPlayer holdingPlayer = new MapItemSavedData.HoldingPlayer(tickingPlayer);
|
| 171 | this.carriedByPlayers.put(tickingPlayer, holdingPlayer);
|
| 172 | this.carriedBy.add(holdingPlayer);
|
| 173 | }
|
| 174 |
|
| 175 | Predicate<ItemStack> mapMatcher = mapMatcher(itemStack);
|
| 176 | if (!tickingPlayer.getInventory().contains(mapMatcher)) {
|
| 177 | this.removeDecoration(tickingPlayer.getPlainTextName());
|
| 178 | }
|
| 179 |
|
| 180 | for (int i = 0; i < this.carriedBy.size(); i++) {
|
| 181 | MapItemSavedData.HoldingPlayer otherHoldingPlayer = this.carriedBy.get(i);
|
| 182 | Player otherPlayer = otherHoldingPlayer.player;
|
| 183 | String otherPlayerName = otherPlayer.getPlainTextName();
|
| 184 | if (!otherPlayer.isRemoved() && (placedInFrame != null || otherPlayer.getInventory().contains(mapMatcher))) {
|
| 185 | if (placedInFrame == null && otherPlayer.level().dimension() == this.dimension && this.trackingPosition) {
|
| 186 | this.addDecoration(MapDecorationTypes.PLAYER, otherPlayer.level(), otherPlayerName, otherPlayer.getX(), otherPlayer.getZ(), otherPlayer.getYRot(), null);
|
| 187 | }
|
| 188 | } else {
|
| 189 | this.carriedByPlayers.remove(otherPlayer);
|
| 190 | this.carriedBy.remove(otherHoldingPlayer);
|
| 191 | this.removeDecoration(otherPlayerName);
|
| 192 | }
|
| 193 |
|
| 194 | if (!otherPlayer.equals(tickingPlayer) && hasMapInvisibilityItemEquipped(otherPlayer)) {
|
| 195 | this.removeDecoration(otherPlayerName);
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | if (placedInFrame != null && this.trackingPosition) {
|
| 200 | BlockPos pos = placedInFrame.getPos();
|
| 201 | MapFrame existingFrame = this.frameMarkers.get(MapFrame.frameId(pos));
|
| 202 | if (existingFrame != null && placedInFrame.getId() != existingFrame.entityId() && this.frameMarkers.containsKey(existingFrame.getId())) {
|
| 203 | this.removeDecoration(getFrameKey(existingFrame.entityId()));
|
| 204 | }
|
| 205 |
|
| 206 | MapFrame mapFrame = new MapFrame(pos, placedInFrame.getDirection().get2DDataValue() * 90, placedInFrame.getId());
|
| 207 | this.addDecoration(
|
| 208 | MapDecorationTypes.FRAME,
|
| 209 | tickingPlayer.level(),
|
| 210 | getFrameKey(placedInFrame.getId()),
|
| 211 | pos.getX(),
|
| 212 | pos.getZ(),
|
| 213 | placedInFrame.getDirection().get2DDataValue() * 90,
|
| 214 | null
|
| 215 | );
|
| 216 | MapFrame oldFrame = this.frameMarkers.put(mapFrame.getId(), mapFrame);
|
| 217 | if (!mapFrame.equals(oldFrame)) {
|
| 218 | this.setDirty();
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | MapDecorations staticDecorations = itemStack.getOrDefault(DataComponents.MAP_DECORATIONS, MapDecorations.EMPTY);
|
| 223 | if (!this.decorations.keySet().containsAll(staticDecorations.decorations().keySet())) {
|
| 224 | staticDecorations.decorations().forEach((id, entry) -> {
|
| 225 | if (!this.decorations.containsKey(id)) {
|
| 226 | this.addDecoration(entry.type(), tickingPlayer.level(), id, entry.x(), entry.z(), entry.rotation(), null);
|
| 227 | }
|
| 228 | });
|
| 229 | }
|
| 230 | }
|
| 231 |
|
| 232 | private static boolean hasMapInvisibilityItemEquipped(final Player player) {
|
| 233 | for (EquipmentSlot equipmentSlot : EquipmentSlot.values()) {
|
| 234 | if (equipmentSlot != EquipmentSlot.MAINHAND
|
| 235 | && equipmentSlot != EquipmentSlot.OFFHAND
|
| 236 | && player.getItemBySlot(equipmentSlot).is(ItemTags.MAP_INVISIBILITY_EQUIPMENT)) {
|
| 237 | return true;
|
| 238 | }
|
| 239 | }
|
| 240 |
|
| 241 | return false;
|
| 242 | }
|
| 243 |
|
| 244 | private void removeDecoration(final String string) {
|
| 245 | MapDecoration decoration = this.decorations.remove(string);
|
| 246 | if (decoration != null && decoration.type().value().trackCount()) {
|
| 247 | this.trackedDecorationCount--;
|
| 248 | }
|
| 249 |
|
| 250 | this.setDecorationsDirty();
|
| 251 | }
|
| 252 |
|
| 253 | public static void addTargetDecoration(final ItemStack itemStack, final BlockPos position, final String key, final Holder<MapDecorationType> decorationType) {
|
| 254 | MapDecorations.Entry newDecoration = new MapDecorations.Entry(decorationType, position.getX(), position.getZ(), 180.0F);
|
| 255 | itemStack.update(DataComponents.MAP_DECORATIONS, MapDecorations.EMPTY, decorations -> decorations.withDecoration(key, newDecoration));
|
| 256 | }
|
| 257 |
|
| 258 | private void addDecoration(
|
| 259 | final Holder<MapDecorationType> type,
|
| 260 | final @Nullable LevelAccessor level,
|
| 261 | final String key,
|
| 262 | final double xPos,
|
| 263 | final double zPos,
|
| 264 | final double yRot,
|
| 265 | final @Nullable Component name
|
| 266 | ) {
|
| 267 | int scaling = 1 << this.scale;
|
| 268 | float xDeltaFromCenter = (float)(xPos - this.centerX) / scaling;
|
| 269 | float yDeltaFromCenter = (float)(zPos - this.centerZ) / scaling;
|
| 270 | MapItemSavedData.MapDecorationLocation locationAndType = this.calculateDecorationLocationAndType(type, level, yRot, xDeltaFromCenter, yDeltaFromCenter);
|
| 271 | if (locationAndType == null) {
|
| 272 | this.removeDecoration(key);
|
| 273 | } else {
|
| 274 | MapDecoration newDecoration = new MapDecoration(
|
| 275 | locationAndType.type(), locationAndType.x(), locationAndType.y(), locationAndType.rot(), Optional.ofNullable(name)
|
| 276 | );
|
| 277 | MapDecoration previousDecoration = this.decorations.put(key, newDecoration);
|
| 278 | if (!newDecoration.equals(previousDecoration)) {
|
| 279 | if (previousDecoration != null && previousDecoration.type().value().trackCount()) {
|
| 280 | this.trackedDecorationCount--;
|
| 281 | }
|
| 282 |
|
| 283 | if (locationAndType.type().value().trackCount()) {
|
| 284 | this.trackedDecorationCount++;
|
| 285 | }
|
| 286 |
|
| 287 | this.setDecorationsDirty();
|
| 288 | }
|
| 289 | }
|
| 290 | }
|
| 291 |
|
| 292 | private MapItemSavedData.@Nullable MapDecorationLocation calculateDecorationLocationAndType(
|
| 293 | final Holder<MapDecorationType> type, final @Nullable LevelAccessor level, final double yRot, final float xDeltaFromCenter, final float yDeltaFromCenter
|
| 294 | ) {
|
| 295 | byte clampedXDeltaFromCenter = clampMapCoordinate(xDeltaFromCenter);
|
| 296 | byte clampedYDeltaFromCenter = clampMapCoordinate(yDeltaFromCenter);
|
| 297 | if (type.is(MapDecorationTypes.PLAYER)) {
|
| 298 | Pair<Holder<MapDecorationType>, Byte> typeAndRotation = this.playerDecorationTypeAndRotation(type, level, yRot, xDeltaFromCenter, yDeltaFromCenter);
|
| 299 | return typeAndRotation == null
|
| 300 | ? null
|
| 301 | : new MapItemSavedData.MapDecorationLocation(typeAndRotation.getFirst(), clampedXDeltaFromCenter, clampedYDeltaFromCenter, typeAndRotation.getSecond());
|
| 302 | } else {
|
| 303 | return !isInsideMap(xDeltaFromCenter, yDeltaFromCenter) && !this.unlimitedTracking
|
| 304 | ? null
|
| 305 | : new MapItemSavedData.MapDecorationLocation(type, clampedXDeltaFromCenter, clampedYDeltaFromCenter, this.calculateRotation(level, yRot));
|
| 306 | }
|
| 307 | }
|
| 308 |
|
| 309 | private @Nullable Pair<Holder<MapDecorationType>, Byte> playerDecorationTypeAndRotation(
|
| 310 | final Holder<MapDecorationType> type, final @Nullable LevelAccessor level, final double yRot, final float xDeltaFromCenter, final float yDeltaFromCenter
|
| 311 | ) {
|
| 312 | if (isInsideMap(xDeltaFromCenter, yDeltaFromCenter)) {
|
| 313 | return Pair.of(type, this.calculateRotation(level, yRot));
|
| 314 | }
|
| 315 |
|
| 316 | Holder<MapDecorationType> outsideMapDecorationType = this.decorationTypeForPlayerOutsideMap(xDeltaFromCenter, yDeltaFromCenter);
|
| 317 | return outsideMapDecorationType == null ? null : Pair.of(outsideMapDecorationType, this.calculateRotation(level, yRot));
|
| 318 | }
|
| 319 |
|
| 320 | private byte calculateRotation(final @Nullable LevelAccessor level, final double yRot) {
|
| 321 | if (this.dimension == Level.NETHER && level != null) {
|
| 322 | int s = (int)(level.getGameTime() / 10L);
|
| 323 | return (byte)(s * s * 34187121 + s * 121 >> 15 & 15);
|
| 324 | } else {
|
| 325 | double adjustedYRot = yRot < 0.0 ? yRot - 8.0 : yRot + 8.0;
|
| 326 | return (byte)(adjustedYRot * 16.0 / 360.0);
|
| 327 | }
|
| 328 | }
|
| 329 |
|
| 330 | private static boolean isInsideMap(final float xd, final float yd) {
|
| 331 | int halfSize = 63;
|
| 332 | return xd >= -63.0F && yd >= -63.0F && xd <= 63.0F && yd <= 63.0F;
|
| 333 | }
|
| 334 |
|
| 335 | private @Nullable Holder<MapDecorationType> decorationTypeForPlayerOutsideMap(final float xDeltaFromCenter, final float yDeltaFromCenter) {
|
| 336 | int rangeLimit = 320;
|
| 337 | boolean isWithinLimits = Math.abs(xDeltaFromCenter) < 320.0F && Math.abs(yDeltaFromCenter) < 320.0F;
|
| 338 | if (isWithinLimits) {
|
| 339 | return MapDecorationTypes.PLAYER_OFF_MAP;
|
| 340 | } else {
|
| 341 | return this.unlimitedTracking ? MapDecorationTypes.PLAYER_OFF_LIMITS : null;
|
| 342 | }
|
| 343 | }
|
| 344 |
|
| 345 | private static byte clampMapCoordinate(final float deltaFromCenter) {
|
| 346 | int halfSize = 63;
|
| 347 | if (deltaFromCenter <= -63.0F) {
|
| 348 | return -128;
|
| 349 | } else {
|
| 350 | return deltaFromCenter >= 63.0F ? 127 : (byte)(deltaFromCenter * 2.0F + 0.5);
|
| 351 | }
|
| 352 | }
|
| 353 |
|
| 354 | public @Nullable Packet<?> getUpdatePacket(final MapId id, final Player player) {
|
| 355 | MapItemSavedData.HoldingPlayer holdingPlayer = this.carriedByPlayers.get(player);
|
| 356 | return holdingPlayer == null ? null : holdingPlayer.nextUpdatePacket(id);
|
| 357 | }
|
| 358 |
|
| 359 | private void setColorsDirty(final int x, final int y) {
|
| 360 | this.setDirty();
|
| 361 |
|
| 362 | for (MapItemSavedData.HoldingPlayer holdingPlayer : this.carriedBy) {
|
| 363 | holdingPlayer.markColorsDirty(x, y);
|
| 364 | }
|
| 365 | }
|
| 366 |
|
| 367 | private void setDecorationsDirty() {
|
| 368 | this.carriedBy.forEach(MapItemSavedData.HoldingPlayer::markDecorationsDirty);
|
| 369 | }
|
| 370 |
|
| 371 | public MapItemSavedData.HoldingPlayer getHoldingPlayer(final Player player) {
|
| 372 | MapItemSavedData.HoldingPlayer holdingPlayer = this.carriedByPlayers.get(player);
|
| 373 | if (holdingPlayer == null) {
|
| 374 | holdingPlayer = new MapItemSavedData.HoldingPlayer(player);
|
| 375 | this.carriedByPlayers.put(player, holdingPlayer);
|
| 376 | this.carriedBy.add(holdingPlayer);
|
| 377 | }
|
| 378 |
|
| 379 | return holdingPlayer;
|
| 380 | }
|
| 381 |
|
| 382 | public boolean toggleBanner(final LevelAccessor level, final BlockPos pos) {
|
| 383 | double xPos = pos.getX() + 0.5;
|
| 384 | double zPos = pos.getZ() + 0.5;
|
| 385 | int scale = 1 << this.scale;
|
| 386 | double xd = (xPos - this.centerX) / scale;
|
| 387 | double yd = (zPos - this.centerZ) / scale;
|
| 388 | int halfSize = 63;
|
| 389 | if (xd >= -63.0 && yd >= -63.0 && xd <= 63.0 && yd <= 63.0) {
|
| 390 | MapBanner banner = MapBanner.fromWorld(level, pos);
|
| 391 | if (banner == null) {
|
| 392 | return false;
|
| 393 | }
|
| 394 |
|
| 395 | if (this.bannerMarkers.remove(banner.getId(), banner)) {
|
| 396 | this.removeDecoration(banner.getId());
|
| 397 | this.setDirty();
|
| 398 | return true;
|
| 399 | }
|
| 400 |
|
| 401 | if (!this.isTrackedCountOverLimit(256)) {
|
| 402 | this.bannerMarkers.put(banner.getId(), banner);
|
| 403 | this.addDecoration(banner.getDecoration(), level, banner.getId(), xPos, zPos, 180.0, banner.name().orElse(null));
|
| 404 | this.setDirty();
|
| 405 | return true;
|
| 406 | }
|
| 407 | }
|
| 408 |
|
| 409 | return false;
|
| 410 | }
|
| 411 |
|
| 412 | public void checkBanners(final BlockGetter level, final int x, final int z) {
|
| 413 | Iterator<MapBanner> iterator = this.bannerMarkers.values().iterator();
|
| 414 |
|
| 415 | while (iterator.hasNext()) {
|
| 416 | MapBanner expected = iterator.next();
|
| 417 | if (expected.pos().getX() == x && expected.pos().getZ() == z) {
|
| 418 | MapBanner current = MapBanner.fromWorld(level, expected.pos());
|
| 419 | if (!expected.equals(current)) {
|
| 420 | iterator.remove();
|
| 421 | this.removeDecoration(expected.getId());
|
| 422 | this.setDirty();
|
| 423 | }
|
| 424 | }
|
| 425 | }
|
| 426 | }
|
| 427 |
|
| 428 | public Collection<MapBanner> getBanners() {
|
| 429 | return this.bannerMarkers.values();
|
| 430 | }
|
| 431 |
|
| 432 | public void removedFromFrame(final BlockPos pos, final int entityID) {
|
| 433 | this.removeDecoration(getFrameKey(entityID));
|
| 434 | this.frameMarkers.remove(MapFrame.frameId(pos));
|
| 435 | this.setDirty();
|
| 436 | }
|
| 437 |
|
| 438 | public boolean updateColor(final int x, final int y, final byte newColor) {
|
| 439 | byte oldColor = this.colors[x + y * 128];
|
| 440 | if (oldColor != newColor) {
|
| 441 | this.setColor(x, y, newColor);
|
| 442 | return true;
|
| 443 | } else {
|
| 444 | return false;
|
| 445 | }
|
| 446 | }
|
| 447 |
|
| 448 | public void setColor(final int x, final int y, final byte newColor) {
|
| 449 | this.colors[x + y * 128] = newColor;
|
| 450 | this.setColorsDirty(x, y);
|
| 451 | }
|
| 452 |
|
| 453 | public void addClientSideDecorations(final List<MapDecoration> decorations) {
|
| 454 | this.decorations.clear();
|
| 455 | this.trackedDecorationCount = 0;
|
| 456 |
|
| 457 | for (int i = 0; i < decorations.size(); i++) {
|
| 458 | MapDecoration decoration = decorations.get(i);
|
| 459 | this.decorations.put("icon-" + i, decoration);
|
| 460 | if (decoration.type().value().trackCount()) {
|
| 461 | this.trackedDecorationCount++;
|
| 462 | }
|
| 463 | }
|
| 464 | }
|
| 465 |
|
| 466 | public Iterable<MapDecoration> getDecorations() {
|
| 467 | return this.decorations.values();
|
| 468 | }
|
| 469 |
|
| 470 | public boolean isTrackedCountOverLimit(final int limit) {
|
| 471 | return this.trackedDecorationCount > limit;
|
| 472 | }
|
| 473 |
|
| 474 | private static String getFrameKey(final int id) {
|
| 475 | return "frame-" + id;
|
| 476 | }
|
| 477 |
|
| 478 | public class HoldingPlayer {
|
| 479 | public final Player player;
|
| 480 | private boolean dirtyData = true;
|
| 481 | private int minDirtyX;
|
| 482 | private int minDirtyY;
|
| 483 | private int maxDirtyX = 127;
|
| 484 | private int maxDirtyY = 127;
|
| 485 | private boolean dirtyDecorations = true;
|
| 486 | private int tick;
|
| 487 | public int step;
|
| 488 |
|
| 489 | private HoldingPlayer(final Player player) {
|
| 490 | this.player = player;
|
| 491 | }
|
| 492 |
|
| 493 | private MapItemSavedData.MapPatch createPatch() {
|
| 494 | int startX = this.minDirtyX;
|
| 495 | int startY = this.minDirtyY;
|
| 496 | int width = this.maxDirtyX + 1 - this.minDirtyX;
|
| 497 | int height = this.maxDirtyY + 1 - this.minDirtyY;
|
| 498 | byte[] patch = new byte[width * height];
|
| 499 |
|
| 500 | for (int x = 0; x < width; x++) {
|
| 501 | for (int y = 0; y < height; y++) {
|
| 502 | patch[x + y * width] = MapItemSavedData.this.colors[startX + x + (startY + y) * 128];
|
| 503 | }
|
| 504 | }
|
| 505 |
|
| 506 | return new MapItemSavedData.MapPatch(startX, startY, width, height, patch);
|
| 507 | }
|
| 508 |
|
| 509 | private @Nullable Packet<?> nextUpdatePacket(final MapId id) {
|
| 510 | MapItemSavedData.MapPatch patch;
|
| 511 | if (this.dirtyData) {
|
| 512 | this.dirtyData = false;
|
| 513 | patch = this.createPatch();
|
| 514 | } else {
|
| 515 | patch = null;
|
| 516 | }
|
| 517 |
|
| 518 | Collection<MapDecoration> decorations;
|
| 519 | if (this.dirtyDecorations && this.tick++ % 5 == 0) {
|
| 520 | this.dirtyDecorations = false;
|
| 521 | decorations = MapItemSavedData.this.decorations.values();
|
| 522 | } else {
|
| 523 | decorations = null;
|
| 524 | }
|
| 525 |
|
| 526 | return decorations == null && patch == null
|
| 527 | ? null
|
| 528 | : new ClientboundMapItemDataPacket(id, MapItemSavedData.this.scale, MapItemSavedData.this.locked, decorations, patch);
|
| 529 | }
|
| 530 |
|
| 531 | private void markColorsDirty(final int x, final int y) {
|
| 532 | if (this.dirtyData) {
|
| 533 | this.minDirtyX = Math.min(this.minDirtyX, x);
|
| 534 | this.minDirtyY = Math.min(this.minDirtyY, y);
|
| 535 | this.maxDirtyX = Math.max(this.maxDirtyX, x);
|
| 536 | this.maxDirtyY = Math.max(this.maxDirtyY, y);
|
| 537 | } else {
|
| 538 | this.dirtyData = true;
|
| 539 | this.minDirtyX = x;
|
| 540 | this.minDirtyY = y;
|
| 541 | this.maxDirtyX = x;
|
| 542 | this.maxDirtyY = y;
|
| 543 | }
|
| 544 | }
|
| 545 |
|
| 546 | private void markDecorationsDirty() {
|
| 547 | this.dirtyDecorations = true;
|
| 548 | }
|
| 549 | }
|
| 550 |
|
| 551 | private record MapDecorationLocation(Holder<MapDecorationType> type, byte x, byte y, byte rot) {
|
| 552 | }
|
| 553 |
|
| 554 | public record MapPatch(int startX, int startY, int width, int height, byte[] mapColors) {
|
| 555 | public static final StreamCodec<ByteBuf, Optional<MapItemSavedData.MapPatch>> STREAM_CODEC = StreamCodec.of(
|
| 556 | MapItemSavedData.MapPatch::write, MapItemSavedData.MapPatch::read
|
| 557 | );
|
| 558 |
|
| 559 | private static void write(final ByteBuf output, final Optional<MapItemSavedData.MapPatch> optional) {
|
| 560 | if (optional.isPresent()) {
|
| 561 | MapItemSavedData.MapPatch patch = optional.get();
|
| 562 | output.writeByte(patch.width);
|
| 563 | output.writeByte(patch.height);
|
| 564 | output.writeByte(patch.startX);
|
| 565 | output.writeByte(patch.startY);
|
| 566 | FriendlyByteBuf.writeByteArray(output, patch.mapColors);
|
| 567 | } else {
|
| 568 | output.writeByte(0);
|
| 569 | }
|
| 570 | }
|
| 571 |
|
| 572 | private static Optional<MapItemSavedData.MapPatch> read(final ByteBuf input) {
|
| 573 | int width = input.readUnsignedByte();
|
| 574 | if (width > 0) {
|
| 575 | int height = input.readUnsignedByte();
|
| 576 | int startX = input.readUnsignedByte();
|
| 577 | int startY = input.readUnsignedByte();
|
| 578 | byte[] mapColors = FriendlyByteBuf.readByteArray(input);
|
| 579 | return Optional.of(new MapItemSavedData.MapPatch(startX, startY, width, height, mapColors));
|
| 580 | } else {
|
| 581 | return Optional.empty();
|
| 582 | }
|
| 583 | }
|
| 584 |
|
| 585 | public void applyToMap(final MapItemSavedData map) {
|
| 586 | for (int x = 0; x < this.width; x++) {
|
| 587 | for (int y = 0; y < this.height; y++) {
|
| 588 | map.setColor(this.startX + x, this.startY + y, this.mapColors[x + y * this.width]);
|
| 589 | }
|
| 590 | }
|
| 591 | }
|
| 592 | }
|
| 593 | }
|
| 594 |
|