Move marker applier to the packet api

Signed-off-by: Irmo van den Berge <irmo.vandenberge@ziggo.nl>
This commit is contained in:
Irmo van den Berge committed 2024-05-05 14:05:04 +02:00
1 parent 44732dfc6c
commit 0b48ca783d
5 files changed
+98 -63

No files matched your search

@@ -1,9 +1,12 @@
package com.bergerkiller.generated.net.minecraft.network.protocol.game;
import com.bergerkiller.mountiplex.reflection.declarations.Template;
import com.bergerkiller.generated.net.minecraft.network.protocol.PacketHandle;
import org.bukkit.map.MapCursor;
import com.bergerkiller.generated.net.minecraft.world.level.saveddata.maps.MapIconHandle;
import com.bergerkiller.mountiplex.conversion.type.DuplexConverter;
import com.bergerkiller.mountiplex.conversion.util.ConvertingList;
import com.bergerkiller.mountiplex.reflection.declarations.Template;
import java.util.List;
import org.bukkit.map.MapCursor;
/**
* Instance wrapper handle for type <b>net.minecraft.network.protocol.game.PacketPlayOutMap</b>.
@@ -42,12 +45,24 @@ public abstract class PacketPlayOutMapHandle extends PacketHandle {
private int mapId = 0;
private byte scale = (byte) 1;
private boolean locked = false;
private java.util.Optional<java.util.List<Object>> nmsMapIcons = java.util.Optional.empty();
private java.util.Optional<java.util.List<MapIconHandle>> mapIcons = java.util.Optional.empty();
private boolean hasData = false;
private int startX, startY;
private int width, height;
private byte[] colors = null;
private static final DuplexConverter<MapIconHandle, MapCursor> mapCursorHandleConversion = new DuplexConverter<MapIconHandle, MapCursor>(MapIconHandle.class, MapCursor.class) {
@Override
public MapCursor convertInput(MapIconHandle mapIconHandle) {
return mapIconHandle.toCursor();
}
@Override
public MapIconHandle convertOutput(MapCursor mapCursor) {
return MapIconHandle.fromCursor(mapCursor);
}
};
private Builder() {
}
@@ -64,12 +79,11 @@ public abstract class PacketPlayOutMapHandle extends PacketHandle {
}
public java.util.Optional<java.util.List<org.bukkit.map.MapCursor>> get_cursors() {
return nmsMapIcons.map(list -> new com.bergerkiller.mountiplex.conversion.util.ConvertingList<>(list,
com.bergerkiller.bukkit.common.conversion.DuplexConversion.mapCursor));
return mapIcons.map(list -> new ConvertingList<>(list, mapCursorHandleConversion));
}
public java.util.Optional<java.util.List<Object>> get_cursors_raw() {
return nmsMapIcons;
public java.util.Optional<java.util.List<MapIconHandle>> get_cursors_nms() {
return mapIcons;
}
public boolean has_data() {
@@ -111,37 +125,36 @@ public abstract class PacketPlayOutMapHandle extends PacketHandle {
return this;
}
private List<Object> convertCursors(java.util.List<org.bukkit.map.MapCursor> cursors) {
return new com.bergerkiller.mountiplex.conversion.util.ConvertingList<>(cursors,
com.bergerkiller.bukkit.common.conversion.DuplexConversion.mapCursor.reverse());
private List<MapIconHandle> convertCursors(java.util.List<org.bukkit.map.MapCursor> cursors) {
return new ConvertingList<>(cursors, mapCursorHandleConversion.reverse());
}
public Builder no_cursors() {
return this.cursors_raw(java.util.Collections.emptyList());
return this.cursors_nms(java.util.Collections.emptyList());
}
public Builder cursors(java.util.List<org.bukkit.map.MapCursor> cursors) {
return this.cursors_raw(convertCursors(cursors));
return this.cursors_nms(convertCursors(cursors));
}
public Builder cursors_raw(java.util.List<Object> rawCursors) {
this.nmsMapIcons = java.util.Optional.of(rawCursors);
public Builder cursors_nms(java.util.List<MapIconHandle> rawCursors) {
this.mapIcons = java.util.Optional.ofNullable(rawCursors);
return this;
}
public Builder add_cursors(java.util.List<org.bukkit.map.MapCursor> cursors) {
return add_cursors_raw(convertCursors(cursors));
return add_cursors_nms(convertCursors(cursors));
}
public Builder add_cursors_raw(java.util.List<Object> rawCursors) {
if (!this.nmsMapIcons.isPresent()) {
this.cursors_raw(rawCursors);
} else if (!rawCursors.isEmpty()) {
java.util.List<Object> existing = this.nmsMapIcons.get();
java.util.List<Object> newList = new java.util.ArrayList<>(existing.size() + rawCursors.size());
public Builder add_cursors_nms(java.util.List<MapIconHandle> rawCursors) {
if (!this.mapIcons.isPresent()) {
this.cursors_nms(rawCursors);
} else if (rawCursors != null && !rawCursors.isEmpty()) {
java.util.List<MapIconHandle> existing = this.mapIcons.get();
java.util.List<MapIconHandle> newList = new java.util.ArrayList<>(existing.size() + rawCursors.size());
newList.addAll(existing);
newList.addAll(rawCursors);
this.nmsMapIcons = java.util.Optional.of(newList);
this.mapIcons = java.util.Optional.of(newList);
}
return this;
}
@@ -162,7 +175,7 @@ public abstract class PacketPlayOutMapHandle extends PacketHandle {
copy.mapId = this.mapId;
copy.scale = this.scale;
copy.locked = this.locked;
copy.nmsMapIcons = this.nmsMapIcons;
copy.mapIcons = this.mapIcons;
copy.startX = this.startX;
copy.startY = this.startY;
copy.width = this.width;
@@ -5,6 +5,7 @@ import java.util.List;
import java.util.Set;
import java.util.UUID;
import com.bergerkiller.generated.net.minecraft.world.level.saveddata.maps.MapIconHandle;
import org.bukkit.Chunk;
import org.bukkit.Difficulty;
import org.bukkit.GameMode;
@@ -4,24 +4,24 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.bergerkiller.bukkit.common.utils.StreamUtil;
import com.bergerkiller.generated.net.minecraft.world.level.saveddata.maps.MapIconHandle;
import org.bukkit.entity.Player;
import com.bergerkiller.bukkit.common.Common;
import com.bergerkiller.bukkit.common.bases.IntVector2;
import com.bergerkiller.bukkit.common.map.MapDisplayTile;
import com.bergerkiller.bukkit.common.map.MapMarker;
import com.bergerkiller.bukkit.common.map.MapSession;
import com.bergerkiller.bukkit.common.utils.MathUtil;
import com.bergerkiller.bukkit.common.utils.PacketUtil;
import com.bergerkiller.mountiplex.reflection.declarations.Template;
import com.bergerkiller.mountiplex.reflection.util.UniqueHash;
public class MapDisplayMarkers {
public static final UniqueHash RANDOM_NAME_SOURCE = new UniqueHash();
public static final MapDisplayMarkerConverter APPLIER = Template.Class.create(MapDisplayMarkerConverter.class, Common.TEMPLATE_RESOLVER);
private final Map<String, Entry> markersById = new HashMap<>();
private final Map<IntVector2, MapDisplayMarkerTile> markersByTile = new HashMap<>();
@@ -37,6 +37,17 @@ public class MapDisplayMarkers {
.stream().map(e -> e.value).collect(Collectors.toList()));
}
List<MapIconHandle> serializeValuesAsMapIcons(MapDisplayMarkerTile tile) {
return markersById.values().stream()
.map(e -> e.value)
.map(marker -> MapIconHandle.createNew(marker.getType(),
tile.encodeX(marker.getPositionX()),
tile.encodeY(marker.getPositionY()),
tile.encodeRotation(marker.getRotation()),
marker.getFormattedCaption()))
.collect(StreamUtil.toUnmodifiableList());
}
public MapMarker get(String id) {
Entry e = markersById.get(id);
return (e == null) ? null : e.value;
@@ -140,7 +151,7 @@ public class MapDisplayMarkers {
// Requires update, ask the tile to do this
if (mapUpdate == null) {
mapUpdate = new MapDisplayTile.Update(displayedTile.tile, displayedTile.getMapId());
mapUpdate.packet.cursors_raw(APPLIER.getMapIcons(tile));
mapUpdate.packet.cursors_nms(serializeValuesAsMapIcons(tile));
}
// Send to this player. Makes a new packet each time, as the pixels byte[] array is mutable.
@@ -177,7 +188,7 @@ public class MapDisplayMarkers {
mapUpdate.packet.no_cursors();
return false;
} else {
mapUpdate.packet.cursors_raw(APPLIER.getMapIcons(tile));
mapUpdate.packet.cursors_nms(serializeValuesAsMapIcons(tile));
return tile.isChangedFor(viewer);
}
}
@@ -5,22 +5,39 @@ import net.minecraft.world.level.saveddata.maps.MapIcon;
import net.minecraft.world.level.saveddata.maps.WorldMap;
import net.minecraft.world.level.saveddata.maps.WorldMap$PatchData;
import com.bergerkiller.generated.net.minecraft.world.level.saveddata.maps.MapIconHandle;
import com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutMapHandle;
import com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutMapHandle.Builder;
class PacketPlayOutMap extends Packet {
<code>
import com.bergerkiller.generated.net.minecraft.world.level.saveddata.maps.MapIconHandle;
import com.bergerkiller.mountiplex.conversion.type.DuplexConverter;
import com.bergerkiller.mountiplex.conversion.util.ConvertingList;
public static class Builder implements Cloneable {
private int mapId = 0;
private byte scale = (byte) 1;
private boolean locked = false;
private java.util.Optional<java.util.List<Object>> nmsMapIcons = java.util.Optional.empty();
private java.util.Optional<java.util.List<MapIconHandle>> mapIcons = java.util.Optional.empty();
private boolean hasData = false;
private int startX, startY;
private int width, height;
private byte[] colors = null;
private static final DuplexConverter<MapIconHandle, MapCursor> mapCursorHandleConversion = new DuplexConverter<MapIconHandle, MapCursor>(MapIconHandle.class, MapCursor.class) {
@Override
public MapCursor convertInput(MapIconHandle mapIconHandle) {
return mapIconHandle.toCursor();
}
@Override
public MapIconHandle convertOutput(MapCursor mapCursor) {
return MapIconHandle.fromCursor(mapCursor);
}
};
private Builder() {
}
@@ -37,12 +54,11 @@ class PacketPlayOutMap extends Packet {
}
public java.util.Optional<java.util.List<org.bukkit.map.MapCursor>> get_cursors() {
return nmsMapIcons.map(list -> new com.bergerkiller.mountiplex.conversion.util.ConvertingList<>(list,
com.bergerkiller.bukkit.common.conversion.DuplexConversion.mapCursor));
return mapIcons.map(list -> new ConvertingList<>(list, mapCursorHandleConversion));
}
public java.util.Optional<java.util.List<Object>> get_cursors_raw() {
return nmsMapIcons;
public java.util.Optional<java.util.List<MapIconHandle>> get_cursors_nms() {
return mapIcons;
}
public boolean has_data() {
@@ -84,37 +100,36 @@ class PacketPlayOutMap extends Packet {
return this;
}
private List<Object> convertCursors(java.util.List<org.bukkit.map.MapCursor> cursors) {
return new com.bergerkiller.mountiplex.conversion.util.ConvertingList<>(cursors,
com.bergerkiller.bukkit.common.conversion.DuplexConversion.mapCursor.reverse());
private List<MapIconHandle> convertCursors(java.util.List<org.bukkit.map.MapCursor> cursors) {
return new ConvertingList<>(cursors, mapCursorHandleConversion.reverse());
}
public Builder no_cursors() {
return this.cursors_raw(java.util.Collections.emptyList());
return this.cursors_nms(java.util.Collections.emptyList());
}
public Builder cursors(java.util.List<org.bukkit.map.MapCursor> cursors) {
return this.cursors_raw(convertCursors(cursors));
return this.cursors_nms(convertCursors(cursors));
}
public Builder cursors_raw(java.util.List<Object> rawCursors) {
this.nmsMapIcons = java.util.Optional.of(rawCursors);
public Builder cursors_nms(java.util.List<MapIconHandle> rawCursors) {
this.mapIcons = java.util.Optional.ofNullable(rawCursors);
return this;
}
public Builder add_cursors(java.util.List<org.bukkit.map.MapCursor> cursors) {
return add_cursors_raw(convertCursors(cursors));
return add_cursors_nms(convertCursors(cursors));
}
public Builder add_cursors_raw(java.util.List<Object> rawCursors) {
if (!this.nmsMapIcons.isPresent()) {
this.cursors_raw(rawCursors);
} else if (!rawCursors.isEmpty()) {
java.util.List<Object> existing = this.nmsMapIcons.get();
java.util.List<Object> newList = new java.util.ArrayList<>(existing.size() + rawCursors.size());
public Builder add_cursors_nms(java.util.List<MapIconHandle> rawCursors) {
if (!this.mapIcons.isPresent()) {
this.cursors_nms(rawCursors);
} else if (rawCursors != null && !rawCursors.isEmpty()) {
java.util.List<MapIconHandle> existing = this.mapIcons.get();
java.util.List<MapIconHandle> newList = new java.util.ArrayList<>(existing.size() + rawCursors.size());
newList.addAll(existing);
newList.addAll(rawCursors);
this.nmsMapIcons = java.util.Optional.of(newList);
this.mapIcons = java.util.Optional.of(newList);
}
return this;
}
@@ -135,7 +150,7 @@ class PacketPlayOutMap extends Packet {
copy.mapId = this.mapId;
copy.scale = this.scale;
copy.locked = this.locked;
copy.nmsMapIcons = this.nmsMapIcons;
copy.mapIcons = this.mapIcons;
copy.startX = this.startX;
copy.startY = this.startY;
copy.width = this.width;
@@ -333,14 +348,15 @@ class PacketPlayOutMap extends Packet {
public static (PacketPlayOutMapHandle) PacketPlayOutMap createNew(PacketPlayOutMapHandle.Builder builder) {
// Convert Bukkit MapCursor -> MapIcon
java.util.List mapIconsList;
if (builder.get_cursors().isPresent()) {
java.util.List mapCursors = (java.util.List) builder.get_cursors().get();
mapIconsList = new java.util.ArrayList(mapCursors.size());
for (int i = 0; i < mapCursors.size(); i++) {
org.bukkit.map.MapCursor cursor = (org.bukkit.map.MapCursor) mapCursors.get(i);
MapIcon icon = (MapIcon) com.bergerkiller.bukkit.common.conversion.type.MapConversion.toMapIconHandle(cursor);
if (icon != null) {
mapIconsList.add(icon);
if (builder.get_cursors_nms().isPresent()) {
java.util.List mapIconHandleList = (java.util.List) builder.get_cursors_nms().get();
if (mapIconHandleList.isEmpty()) {
mapIconsList = java.util.Collections.emptyList();
} else {
mapIconsList = new java.util.ArrayList(mapIconHandleList.size());
for (int i = 0; i < mapIconHandleList.size(); i++) {
MapIconHandle iconHandle = (MapIconHandle) mapIconHandleList.get(i);
mapIconsList.add(iconHandle.getRaw());
}
}
} else {
@@ -19,7 +19,6 @@ import com.bergerkiller.bukkit.common.internal.logic.NullPacketDataSerializerIni
import com.bergerkiller.bukkit.common.internal.logic.PlayerFileDataHandler;
import com.bergerkiller.bukkit.common.internal.logic.PortalHandler;
import com.bergerkiller.bukkit.common.internal.logic.RegionHandler;
import com.bergerkiller.bukkit.common.map.markers.MapDisplayMarkers;
import com.bergerkiller.bukkit.common.resources.DimensionType;
import com.bergerkiller.mountiplex.reflection.resolver.Resolver;
@@ -87,11 +86,6 @@ public class InternalLogicInitializeTest {
EntityMoveHandler.assertInitialized();
}
@Test
public void testMapDisplayMarkerApplier() {
MapDisplayMarkers.APPLIER.forceInitialization();
}
@Test
public void testNullPacketDataSerializer() {
NullPacketDataSerializerInit.initialize();