Skip to content

Commit 83a944d

Browse files
committed
schedule mapview modifications and use a smart ratelimit
1 parent 796c3fc commit 83a944d

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/patches/AntiMapPlayerTracking.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99
import com.github.retrooper.packetevents.resources.ResourceLocation;
1010
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerMapData;
1111
import me.xginko.aef.modules.packets.PacketModule;
12+
import me.xginko.aef.utils.models.ExpiringSet;
13+
import me.xginko.aef.utils.models.Lazy;
1214
import org.bukkit.map.MapView;
1315

16+
import java.time.Duration;
1417
import java.util.List;
1518
import java.util.stream.Collectors;
1619

1720
public class AntiMapPlayerTracking extends PacketModule {
1821

22+
private static final Lazy<ExpiringSet<Integer>> MAP_VIEW_CACHE = Lazy.of(() -> new ExpiringSet<>(Duration.ofHours(4)));
23+
1924
private final List<MapDecorationType> blacklistedDecorationTypes;
2025
private final boolean unlimitedTracking;
2126

@@ -52,9 +57,15 @@ public void onPacketSend(PacketSendEvent event) {
5257

5358
WrapperPlayServerMapData mapData = new WrapperPlayServerMapData(event);
5459

55-
MapView bukkitMapView = plugin.getServer().getMap(mapData.getMapId());
56-
if (bukkitMapView != null && !bukkitMapView.isVirtual() && bukkitMapView.isUnlimitedTracking() != unlimitedTracking) {
57-
bukkitMapView.setUnlimitedTracking(unlimitedTracking);
60+
final int mapId = mapData.getMapId();
61+
62+
if (!MAP_VIEW_CACHE.get().contains(mapId)) {
63+
plugin.getServer().getGlobalRegionScheduler().run(plugin, setTrackingTask -> {
64+
MapView bukkitMapView = plugin.getServer().getMap(mapData.getMapId());
65+
if (bukkitMapView != null && !bukkitMapView.isVirtual() && bukkitMapView.isUnlimitedTracking() != unlimitedTracking) {
66+
bukkitMapView.setUnlimitedTracking(unlimitedTracking);
67+
}
68+
});
5869
}
5970

6071
if (mapData.getDecorations() != null) {

AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/packets/MapCursorLag.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@
88
import me.xginko.aef.utils.ItemUtil;
99
import me.xginko.aef.utils.MathUtil;
1010
import me.xginko.aef.utils.enums.Platform;
11+
import me.xginko.aef.utils.models.ExpiringSet;
12+
import me.xginko.aef.utils.models.Lazy;
1113
import org.bukkit.Bukkit;
1214
import org.bukkit.map.MapView;
1315

16+
import java.time.Duration;
17+
1418
public class MapCursorLag extends PacketModule {
1519

20+
private static final Lazy<ExpiringSet<Integer>> MAP_VIEW_CACHE = Lazy.of(() -> new ExpiringSet<>(Duration.ofHours(4)));
21+
1622
private final int maxCursors;
1723
private final boolean disableTracking;
1824

@@ -31,16 +37,23 @@ public void onPacketSend(PacketSendEvent event) {
3137
WrapperPlayServerMapData mapData = new WrapperPlayServerMapData(event);
3238

3339
if (disableTracking) {
34-
MapView bukkitMapView = Platform.getServerVersion().isOlderThanOrEquals(ServerVersion.V_1_12_2) ?
35-
Bukkit.getMap((short) mapData.getMapId()) : ItemUtil.getMapById(mapData.getMapId());
36-
if (bukkitMapView != null && !bukkitMapView.isVirtual()) {
37-
ItemUtil.setTrackingMapPosition(bukkitMapView, false);
40+
final int mapId = mapData.getMapId();
41+
if (!MAP_VIEW_CACHE.get().contains(mapId)) {
42+
plugin.getServer().getScheduler().runTask(plugin, () -> {
43+
MapView bukkitMapView = Platform.getServerVersion().isOlderThanOrEquals(ServerVersion.V_1_12_2) ?
44+
Bukkit.getMap((short) mapId) : ItemUtil.getMapById(mapId);
45+
if (bukkitMapView != null && !bukkitMapView.isVirtual()) {
46+
ItemUtil.setTrackingMapPosition(bukkitMapView, false);
47+
}
48+
});
3849
}
3950
}
4051

41-
if (mapData.getDecorations() != null) {
52+
if (mapData.getDecorations() != null && !mapData.getDecorations().isEmpty()) {
4253
while (mapData.getDecorations().size() > maxCursors) {
43-
// Todo: remove cursors on underlying canvas instead of the packet using reflection
54+
// This currently only removes any cursors over the size limit, which means players won't lag
55+
// but for the server, the cursors remain unchanged.
56+
// Todo: remove cursors on underlying minecraft map instead of the packet
4457
mapData.getDecorations().remove(MathUtil.RANDOM.nextInt(mapData.getDecorations().size()));
4558
event.markForReEncode(true);
4659
}

AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/patches/AntiMapPlayerTracking.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@
1212
import me.xginko.aef.modules.packets.PacketModule;
1313
import me.xginko.aef.utils.ItemUtil;
1414
import me.xginko.aef.utils.enums.Platform;
15+
import me.xginko.aef.utils.models.ExpiringSet;
16+
import me.xginko.aef.utils.models.Lazy;
1517
import org.bukkit.Bukkit;
1618
import org.bukkit.map.MapView;
1719

20+
import java.time.Duration;
1821
import java.util.Arrays;
1922
import java.util.List;
2023
import java.util.stream.Collectors;
2124

2225
public class AntiMapPlayerTracking extends PacketModule {
2326

27+
private static final Lazy<ExpiringSet<Integer>> MAP_VIEW_CACHE = Lazy.of(() -> new ExpiringSet<>(Duration.ofHours(4)));
28+
2429
private final List<MapDecorationType> blacklistedDecorationTypes;
2530
private final boolean unlimitedTracking;
2631

@@ -57,10 +62,16 @@ public void onPacketSend(PacketSendEvent event) {
5762

5863
WrapperPlayServerMapData mapData = new WrapperPlayServerMapData(event);
5964

60-
MapView bukkitMapView = Platform.getServerVersion().isOlderThanOrEquals(ServerVersion.V_1_12_2) ?
61-
Bukkit.getMap((short) mapData.getMapId()) : ItemUtil.getMapById(mapData.getMapId());
62-
if (bukkitMapView != null && !bukkitMapView.isVirtual() && bukkitMapView.isUnlimitedTracking() != unlimitedTracking) {
63-
bukkitMapView.setUnlimitedTracking(unlimitedTracking);
65+
final int mapId = mapData.getMapId();
66+
67+
if (!MAP_VIEW_CACHE.get().contains(mapId)) {
68+
plugin.getServer().getScheduler().runTask(plugin, () -> {
69+
MapView bukkitMapView = Platform.getServerVersion().isOlderThanOrEquals(ServerVersion.V_1_12_2) ?
70+
Bukkit.getMap((short) mapId) : ItemUtil.getMapById(mapId);
71+
if (bukkitMapView != null && !bukkitMapView.isVirtual() && bukkitMapView.isUnlimitedTracking() != unlimitedTracking) {
72+
bukkitMapView.setUnlimitedTracking(unlimitedTracking);
73+
}
74+
});
6475
}
6576

6677
if (mapData.getDecorations() != null) {

0 commit comments

Comments
 (0)