Paper/paper-server/patches/features/0003-Improve-Maps-in-item-frames-performance-and-bug-fixe.patch
2024-12-17 22:04:50 +01:00

136 lines
7.0 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Fri, 29 Apr 2016 20:02:00 -0400
Subject: [PATCH] Improve Maps (in item frames) performance and bug fixes
Maps used a modified version of rendering to support plugin controlled
imaging on maps. The Craft Map Renderer is much slower than Vanilla,
causing maps in item frames to cause a noticeable hit on server performance.
This updates the map system to not use the Craft system if we detect that no
custom renderers are in use, defaulting to the much simpler Vanilla system.
Additionally, numerous issues to player position tracking on maps has been fixed.
diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java
index 678b3027e8c53e6021ea49afa69cdbe5f60dcf25..cce78d73e8adafd66d0f3ffb3fabb5e6c025c7df 100644
--- a/net/minecraft/server/level/ServerLevel.java
+++ b/net/minecraft/server/level/ServerLevel.java
@@ -2282,7 +2282,9 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe
}
map.carriedByPlayers.remove(player);
- map.carriedBy.removeIf(holdingPlayer -> holdingPlayer.player == player);
+ if (map.carriedBy.removeIf(holdingPlayer -> holdingPlayer.player == player)) {
+ map.decorations.remove(player.getName().getString()); // Paper
+ }
}
}
}
diff --git a/net/minecraft/server/level/ServerPlayer.java b/net/minecraft/server/level/ServerPlayer.java
index 68e3282a0aa23bd41ab7c77be287d2b49461e33c..def43c515030412b147afd6049b100a3153733d2 100644
--- a/net/minecraft/server/level/ServerPlayer.java
+++ b/net/minecraft/server/level/ServerPlayer.java
@@ -2607,6 +2607,14 @@ public class ServerPlayer extends Player {
this.awardStat(Stats.DROP);
}
+ // Paper start - remove player from map on drop
+ if (item.getItem() == net.minecraft.world.item.Items.FILLED_MAP) {
+ final MapItemSavedData mapData = MapItem.getSavedData(item, this.level());
+ if (mapData != null) {
+ mapData.tickCarriedBy(this, item);
+ }
+ }
+ // Paper end - remove player from map on drop
return itemEntity;
}
}
diff --git a/net/minecraft/world/level/saveddata/maps/MapItemSavedData.java b/net/minecraft/world/level/saveddata/maps/MapItemSavedData.java
index fd50bd77e4dc7b18240afe5505c6e6f0f869c127..f60c2f3a3dfc69f50225b6ee7333ada5e9dfd090 100644
--- a/net/minecraft/world/level/saveddata/maps/MapItemSavedData.java
+++ b/net/minecraft/world/level/saveddata/maps/MapItemSavedData.java
@@ -67,6 +67,7 @@ public class MapItemSavedData extends SavedData {
public final Map<String, MapDecoration> decorations = Maps.newLinkedHashMap();
private final Map<String, MapFrame> frameMarkers = Maps.newHashMap();
private int trackedDecorationCount;
+ private org.bukkit.craftbukkit.map.RenderData vanillaRender = new org.bukkit.craftbukkit.map.RenderData(); // Paper
// CraftBukkit start
public final org.bukkit.craftbukkit.map.CraftMapView mapView;
@@ -92,6 +93,7 @@ public class MapItemSavedData extends SavedData {
// CraftBukkit start
this.mapView = new org.bukkit.craftbukkit.map.CraftMapView(this);
this.server = (org.bukkit.craftbukkit.CraftServer) org.bukkit.Bukkit.getServer();
+ this.vanillaRender.buffer = colors; // Paper
// CraftBukkit end
}
@@ -163,6 +165,7 @@ public class MapItemSavedData extends SavedData {
if (byteArray.length == 16384) {
mapItemSavedData.colors = byteArray;
}
+ mapItemSavedData.vanillaRender.buffer = byteArray; // Paper
RegistryOps<Tag> registryOps = levelRegistry.createSerializationContext(NbtOps.INSTANCE);
@@ -337,7 +340,7 @@ public class MapItemSavedData extends SavedData {
this.trackedDecorationCount--;
}
- this.setDecorationsDirty();
+ if (mapDecoration != null) this.setDecorationsDirty(); // Paper - only mark dirty if a change occurs
}
public static void addTargetDecoration(ItemStack stack, BlockPos pos, String type, Holder<MapDecorationType> mapDecorationType) {
@@ -610,7 +613,16 @@ public class MapItemSavedData extends SavedData {
@Nullable
Packet<?> nextUpdatePacket(MapId mapId) {
MapItemSavedData.MapPatch mapPatch;
- org.bukkit.craftbukkit.map.RenderData render = MapItemSavedData.this.mapView.render((org.bukkit.craftbukkit.entity.CraftPlayer) this.player.getBukkitEntity()); // CraftBukkit
+ // Paper start
+ if (!this.dirtyData && this.tick % 5 != 0) {
+ // this won't end up sending, so don't render it!
+ this.tick++;
+ return null;
+ }
+
+ final boolean vanillaMaps = this.shouldUseVanillaMap();
+ org.bukkit.craftbukkit.map.RenderData render = !vanillaMaps ? MapItemSavedData.this.mapView.render((org.bukkit.craftbukkit.entity.CraftPlayer) this.player.getBukkitEntity()) : MapItemSavedData.this.vanillaRender; // CraftBukkit // Paper
+ // Paper end
if (this.dirtyData) {
this.dirtyData = false;
mapPatch = this.createPatch(render.buffer); // CraftBukkit
@@ -623,6 +635,7 @@ public class MapItemSavedData extends SavedData {
this.dirtyDecorations = false;
// CraftBukkit start
java.util.Collection<MapDecoration> icons = new java.util.ArrayList<MapDecoration>();
+ if (vanillaMaps) this.addSeenPlayers(icons); // Paper
for (org.bukkit.map.MapCursor cursor : render.cursors) {
if (cursor.isVisible()) {
@@ -658,6 +671,23 @@ public class MapItemSavedData extends SavedData {
private void markDecorationsDirty() {
this.dirtyDecorations = true;
}
+
+ // Paper start
+ private void addSeenPlayers(java.util.Collection<MapDecoration> icons) {
+ org.bukkit.entity.Player player = (org.bukkit.entity.Player) this.player.getBukkitEntity();
+ MapItemSavedData.this.decorations.forEach((name, mapIcon) -> {
+ // If this cursor is for a player check visibility with vanish system
+ org.bukkit.entity.Player other = org.bukkit.Bukkit.getPlayerExact(name); // Spigot
+ if (other == null || player.canSee(other)) {
+ icons.add(mapIcon);
+ }
+ });
+ }
+
+ private boolean shouldUseVanillaMap() {
+ return mapView.getRenderers().size() == 1 && mapView.getRenderers().getFirst().getClass() == org.bukkit.craftbukkit.map.CraftMapRenderer.class;
+ }
+ // Paper end
}
record MapDecorationLocation(Holder<MapDecorationType> type, byte x, byte y, byte rot) {