From 3e578db3addb97ec0da4565e48778be5fa09321b Mon Sep 17 00:00:00 2001 From: fullwall Date: Thu, 4 May 2023 00:51:57 +0800 Subject: [PATCH] Fix paper backcompat with playerlist tracker change --- .../v1_11_R1/util/PlayerlistTrackerEntry.java | 74 ++++++++++++++----- .../v1_12_R1/util/PlayerlistTrackerEntry.java | 72 +++++++++++++----- .../v1_13_R2/util/PlayerlistTrackerEntry.java | 74 ++++++++++++++----- .../nms/v1_14_R1/util/PlayerlistTracker.java | 72 +++++++++++++----- .../nms/v1_15_R1/util/PlayerlistTracker.java | 71 +++++++++++++----- .../nms/v1_16_R3/util/PlayerlistTracker.java | 72 +++++++++++++----- 6 files changed, 331 insertions(+), 104 deletions(-) diff --git a/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/util/PlayerlistTrackerEntry.java b/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/util/PlayerlistTrackerEntry.java index c92236c42..0621d6793 100644 --- a/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/util/PlayerlistTrackerEntry.java +++ b/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/util/PlayerlistTrackerEntry.java @@ -2,12 +2,14 @@ package net.citizensnpcs.nms.v1_11_R1.util; import java.lang.invoke.MethodHandle; import java.lang.reflect.Field; +import java.util.Map; import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import com.google.common.collect.ForwardingMap; import com.google.common.collect.ForwardingSet; import net.citizensnpcs.Settings.Setting; @@ -27,25 +29,48 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { public PlayerlistTrackerEntry(Entity entity, int i, int j, int k, boolean flag) { super(entity, i, j, k, flag); tracker = getTracker(this); - try { - Set delegate = super.trackedPlayers; - TRACKING_SET_SETTER.invoke(this, new ForwardingSet() { - @Override - public boolean add(EntityPlayer player) { - boolean res = super.add(player); - if (res) { - updateLastPlayer(player); + if (TRACKING_MAP_SETTER != null) { + try { + Map delegate = (Map) TRACKING_MAP_GETTER.invoke(this); + TRACKING_MAP_SETTER.invoke(this, new ForwardingMap() { + @Override + protected Map delegate() { + return delegate; } - return res; - } - @Override - protected Set delegate() { - return delegate; - } - }); - } catch (Throwable e) { - e.printStackTrace(); + @Override + public Boolean put(EntityPlayer player, Boolean value) { + Boolean res = super.put(player, value); + if (res == null) { + updateLastPlayer(player); + } + return res; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } + } else { + try { + Set delegate = super.trackedPlayers; + TRACKING_SET_SETTER.invoke(this, new ForwardingSet() { + @Override + public boolean add(EntityPlayer player) { + boolean res = super.add(player); + if (res) { + updateLastPlayer(player); + } + return res; + } + + @Override + protected Set delegate() { + return delegate; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } } } @@ -141,10 +166,25 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { } private static Field E = NMS.getField(EntityTrackerEntry.class, "e"); + private static Field F = NMS.getField(EntityTrackerEntry.class, "f"); + private static Field G = NMS.getField(EntityTrackerEntry.class, "g"); + private static Field TRACKER = NMS.getField(EntityTrackerEntry.class, "tracker"); + private static MethodHandle TRACKING_MAP_GETTER; + private static MethodHandle TRACKING_MAP_SETTER; private static final MethodHandle TRACKING_SET_SETTER = NMS.getFirstFinalSetter(EntityTrackerEntry.class, Set.class); private static Field U = NMS.getField(EntityTrackerEntry.class, "u"); + static { + try { + // Old paper versions override the tracked player set to be a map + if (EntityTrackerEntry.class.getField("trackedPlayerMap") != null) { + TRACKING_MAP_SETTER = NMS.getFirstSetter(EntityTrackerEntry.class, Map.class); + TRACKING_MAP_GETTER = NMS.getFirstGetter(EntityTrackerEntry.class, Map.class); + } + } catch (Exception e) { + } + } } diff --git a/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/util/PlayerlistTrackerEntry.java b/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/util/PlayerlistTrackerEntry.java index c785dc964..93b34da3c 100644 --- a/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/util/PlayerlistTrackerEntry.java +++ b/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/util/PlayerlistTrackerEntry.java @@ -2,12 +2,14 @@ package net.citizensnpcs.nms.v1_12_R1.util; import java.lang.invoke.MethodHandle; import java.lang.reflect.Field; +import java.util.Map; import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import com.google.common.collect.ForwardingMap; import com.google.common.collect.ForwardingSet; import net.citizensnpcs.Settings.Setting; @@ -27,25 +29,48 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { public PlayerlistTrackerEntry(Entity entity, int i, int j, int k, boolean flag) { super(entity, i, j, k, flag); this.tracker = getTracker(this); - try { - Set delegate = super.trackedPlayers; - TRACKING_SET_SETTER.invoke(this, new ForwardingSet() { - @Override - public boolean add(EntityPlayer player) { - boolean res = super.add(player); - if (res) { - updateLastPlayer(player); + if (TRACKING_MAP_SETTER != null) { + try { + Map delegate = (Map) TRACKING_MAP_GETTER.invoke(this); + TRACKING_MAP_SETTER.invoke(this, new ForwardingMap() { + @Override + protected Map delegate() { + return delegate; } - return res; - } - @Override - protected Set delegate() { - return delegate; - } - }); - } catch (Throwable e) { - e.printStackTrace(); + @Override + public Boolean put(EntityPlayer player, Boolean value) { + Boolean res = super.put(player, value); + if (res == null) { + updateLastPlayer(player); + } + return res; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } + } else { + try { + Set delegate = super.trackedPlayers; + TRACKING_SET_SETTER.invoke(this, new ForwardingSet() { + @Override + public boolean add(EntityPlayer player) { + boolean res = super.add(player); + if (res) { + updateLastPlayer(player); + } + return res; + } + + @Override + protected Set delegate() { + return delegate; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } } } @@ -151,9 +176,22 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { private static Field F = NMS.getField(EntityTrackerEntry.class, "f"); private static Field G = NMS.getField(EntityTrackerEntry.class, "g"); private static Field TRACKER = NMS.getField(EntityTrackerEntry.class, "tracker"); + private static MethodHandle TRACKING_MAP_GETTER; + private static MethodHandle TRACKING_MAP_SETTER; private static final MethodHandle TRACKING_RANGE_SETTER = NMS.getFirstFinalSetter(EntityTrackerEntry.class, int.class); + private static final MethodHandle TRACKING_SET_SETTER = NMS.getFirstFinalSetter(EntityTrackerEntry.class, Set.class); private static Field U = NMS.getField(EntityTrackerEntry.class, "u"); + static { + try { + // Old paper versions override the tracked player set to be a map + if (EntityTrackerEntry.class.getField("trackedPlayerMap") != null) { + TRACKING_MAP_SETTER = NMS.getFirstSetter(EntityTrackerEntry.class, Map.class); + TRACKING_MAP_GETTER = NMS.getFirstGetter(EntityTrackerEntry.class, Map.class); + } + } catch (Exception e) { + } + } } diff --git a/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/util/PlayerlistTrackerEntry.java b/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/util/PlayerlistTrackerEntry.java index e1b62b0a7..512642d05 100644 --- a/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/util/PlayerlistTrackerEntry.java +++ b/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/util/PlayerlistTrackerEntry.java @@ -2,12 +2,14 @@ package net.citizensnpcs.nms.v1_13_R2.util; import java.lang.invoke.MethodHandle; import java.lang.reflect.Field; +import java.util.Map; import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import com.google.common.collect.ForwardingMap; import com.google.common.collect.ForwardingSet; import net.citizensnpcs.Settings.Setting; @@ -28,25 +30,48 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { public PlayerlistTrackerEntry(Entity entity, int i, int j, int k, boolean flag) { super(entity, i, j, k, flag); tracker = getTracker(this); - try { - Set delegate = super.trackedPlayers; - TRACKING_SET_SETTER.invoke(this, new ForwardingSet() { - @Override - public boolean add(EntityPlayer player) { - boolean res = super.add(player); - if (res) { - updateLastPlayer(player); + if (TRACKING_MAP_SETTER != null) { + try { + Map delegate = (Map) TRACKING_MAP_GETTER.invoke(this); + TRACKING_MAP_SETTER.invoke(this, new ForwardingMap() { + @Override + protected Map delegate() { + return delegate; } - return res; - } - @Override - protected Set delegate() { - return delegate; - } - }); - } catch (Throwable e) { - e.printStackTrace(); + @Override + public Boolean put(EntityPlayer player, Boolean value) { + Boolean res = super.put(player, value); + if (res == null) { + updateLastPlayer(player); + } + return res; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } + } else { + try { + Set delegate = super.trackedPlayers; + TRACKING_SET_SETTER.invoke(this, new ForwardingSet() { + @Override + public boolean add(EntityPlayer player) { + boolean res = super.add(player); + if (res) { + updateLastPlayer(player); + } + return res; + } + + @Override + protected Set delegate() { + return delegate; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } } } @@ -158,10 +183,25 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { private static Field F = NMS.getField(EntityTrackerEntry.class, "f"); private static Field G = NMS.getField(EntityTrackerEntry.class, "g"); + private static Field TRACKER = NMS.getField(EntityTrackerEntry.class, "tracker"); + + private static MethodHandle TRACKING_MAP_GETTER; + + private static MethodHandle TRACKING_MAP_SETTER; private static final MethodHandle TRACKING_RANGE_SETTER = NMS.getFirstFinalSetter(EntityTrackerEntry.class, int.class); private static final MethodHandle TRACKING_SET_SETTER = NMS.getFirstFinalSetter(EntityTrackerEntry.class, Set.class); private static Field U = NMS.getField(EntityTrackerEntry.class, "u"); + static { + try { + // Old paper versions override the tracked player set to be a map + if (EntityTrackerEntry.class.getField("trackedPlayerMap") != null) { + TRACKING_MAP_SETTER = NMS.getFirstSetter(EntityTrackerEntry.class, Map.class); + TRACKING_MAP_GETTER = NMS.getFirstGetter(EntityTrackerEntry.class, Map.class); + } + } catch (Exception e) { + } + } } diff --git a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/PlayerlistTracker.java b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/PlayerlistTracker.java index db4de429f..9137cf5e9 100644 --- a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/PlayerlistTracker.java +++ b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/PlayerlistTracker.java @@ -1,12 +1,14 @@ package net.citizensnpcs.nms.v1_14_R1.util; import java.lang.invoke.MethodHandle; +import java.util.Map; import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import com.google.common.collect.ForwardingMap; import com.google.common.collect.ForwardingSet; import net.citizensnpcs.Settings.Setting; @@ -29,25 +31,48 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { public PlayerlistTracker(PlayerChunkMap map, Entity entity, int i, int j, boolean flag) { map.super(entity, i, j, flag); this.tracker = entity; - try { - Set delegate = super.trackedPlayers; - TRACKING_SET_SETTER.invoke(this, new ForwardingSet() { - @Override - public boolean add(EntityPlayer player) { - boolean res = super.add(player); - if (res) { - updateLastPlayer(player); + if (TRACKING_MAP_SETTER != null) { + try { + Map delegate = (Map) TRACKING_MAP_GETTER.invoke(this); + TRACKING_MAP_SETTER.invoke(this, new ForwardingMap() { + @Override + protected Map delegate() { + return delegate; } - return res; - } - @Override - protected Set delegate() { - return delegate; - } - }); - } catch (Throwable e) { - e.printStackTrace(); + @Override + public Boolean put(EntityPlayer player, Boolean value) { + Boolean res = super.put(player, value); + if (res == null) { + updateLastPlayer(player); + } + return res; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } + } else { + try { + Set delegate = super.trackedPlayers; + TRACKING_SET_SETTER.invoke(this, new ForwardingSet() { + @Override + public boolean add(EntityPlayer player) { + boolean res = super.add(player); + if (res) { + updateLastPlayer(player); + } + return res; + } + + @Override + protected Set delegate() { + return delegate; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } } } @@ -135,11 +160,22 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { } private static final MethodHandle D = NMS.getGetter(EntityTrackerEntry.class, "d"); - private static final MethodHandle E = NMS.getGetter(EntityTrackerEntry.class, "e"); private static final MethodHandle TRACKER = NMS.getGetter(EntityTracker.class, "tracker"); private static final MethodHandle TRACKER_ENTRY = NMS.getGetter(EntityTracker.class, "trackerEntry"); + private static MethodHandle TRACKING_MAP_GETTER; + private static MethodHandle TRACKING_MAP_SETTER; private static final MethodHandle TRACKING_RANGE = NMS.getGetter(EntityTracker.class, "trackingDistance"); private static final MethodHandle TRACKING_RANGE_SETTER = NMS.getFirstFinalSetter(EntityTracker.class, int.class); private static final MethodHandle TRACKING_SET_SETTER = NMS.getFirstFinalSetter(EntityTracker.class, Set.class); + static { + try { + // Old paper versions override the tracked player set to be a map + if (EntityTracker.class.getField("trackedPlayerMap") != null) { + TRACKING_MAP_SETTER = NMS.getFirstSetter(EntityTracker.class, Map.class); + TRACKING_MAP_GETTER = NMS.getFirstGetter(EntityTracker.class, Map.class); + } + } catch (Exception e) { + } + } } diff --git a/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/util/PlayerlistTracker.java b/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/util/PlayerlistTracker.java index 623c2983a..897b94889 100644 --- a/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/util/PlayerlistTracker.java +++ b/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/util/PlayerlistTracker.java @@ -1,12 +1,14 @@ package net.citizensnpcs.nms.v1_15_R1.util; import java.lang.invoke.MethodHandle; +import java.util.Map; import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import com.google.common.collect.ForwardingMap; import com.google.common.collect.ForwardingSet; import net.citizensnpcs.Settings.Setting; @@ -29,25 +31,48 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { public PlayerlistTracker(PlayerChunkMap map, Entity entity, int i, int j, boolean flag) { map.super(entity, i, j, flag); this.tracker = entity; - try { - Set delegate = super.trackedPlayers; - TRACKING_SET_SETTER.invoke(this, new ForwardingSet() { - @Override - public boolean add(EntityPlayer player) { - boolean res = super.add(player); - if (res) { - updateLastPlayer(player); + if (TRACKING_MAP_SETTER != null) { + try { + Map delegate = (Map) TRACKING_MAP_GETTER.invoke(this); + TRACKING_MAP_SETTER.invoke(this, new ForwardingMap() { + @Override + protected Map delegate() { + return delegate; } - return res; - } - @Override - protected Set delegate() { - return delegate; - } - }); - } catch (Throwable e) { - e.printStackTrace(); + @Override + public Boolean put(EntityPlayer player, Boolean value) { + Boolean res = super.put(player, value); + if (res == null) { + updateLastPlayer(player); + } + return res; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } + } else { + try { + Set delegate = super.trackedPlayers; + TRACKING_SET_SETTER.invoke(this, new ForwardingSet() { + @Override + public boolean add(EntityPlayer player) { + boolean res = super.add(player); + if (res) { + updateLastPlayer(player); + } + return res; + } + + @Override + protected Set delegate() { + return delegate; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } } } @@ -138,7 +163,19 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { private static final MethodHandle E = NMS.getGetter(EntityTrackerEntry.class, "e"); private static final MethodHandle TRACKER = NMS.getGetter(EntityTracker.class, "tracker"); private static final MethodHandle TRACKER_ENTRY = NMS.getGetter(EntityTracker.class, "trackerEntry"); + private static MethodHandle TRACKING_MAP_GETTER; + private static MethodHandle TRACKING_MAP_SETTER; private static final MethodHandle TRACKING_RANGE = NMS.getGetter(EntityTracker.class, "trackingDistance"); private static final MethodHandle TRACKING_RANGE_SETTER = NMS.getFirstFinalSetter(EntityTracker.class, int.class); private static final MethodHandle TRACKING_SET_SETTER = NMS.getFirstFinalSetter(EntityTracker.class, Set.class); + static { + try { + // Old paper versions override the tracked player set to be a map + if (EntityTracker.class.getField("trackedPlayerMap") != null) { + TRACKING_MAP_SETTER = NMS.getFirstSetter(EntityTracker.class, Map.class); + TRACKING_MAP_GETTER = NMS.getFirstGetter(EntityTracker.class, Map.class); + } + } catch (Exception e) { + } + } } diff --git a/v1_16_R3/src/main/java/net/citizensnpcs/nms/v1_16_R3/util/PlayerlistTracker.java b/v1_16_R3/src/main/java/net/citizensnpcs/nms/v1_16_R3/util/PlayerlistTracker.java index ba4573205..48bd5ce1a 100644 --- a/v1_16_R3/src/main/java/net/citizensnpcs/nms/v1_16_R3/util/PlayerlistTracker.java +++ b/v1_16_R3/src/main/java/net/citizensnpcs/nms/v1_16_R3/util/PlayerlistTracker.java @@ -1,12 +1,14 @@ package net.citizensnpcs.nms.v1_16_R3.util; import java.lang.invoke.MethodHandle; +import java.util.Map; import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import com.google.common.collect.ForwardingMap; import com.google.common.collect.ForwardingSet; import net.citizensnpcs.Settings.Setting; @@ -29,25 +31,48 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { public PlayerlistTracker(PlayerChunkMap map, Entity entity, int i, int j, boolean flag) { map.super(entity, i, j, flag); this.tracker = entity; - try { - Set delegate = super.trackedPlayers; - TRACKING_SET_SETTER.invoke(this, new ForwardingSet() { - @Override - public boolean add(EntityPlayer player) { - boolean res = super.add(player); - if (res) { - updateLastPlayer(player); + if (TRACKING_MAP_SETTER != null) { + try { + Map delegate = (Map) TRACKING_MAP_GETTER.invoke(this); + TRACKING_MAP_SETTER.invoke(this, new ForwardingMap() { + @Override + protected Map delegate() { + return delegate; } - return res; - } - @Override - protected Set delegate() { - return delegate; - } - }); - } catch (Throwable e) { - e.printStackTrace(); + @Override + public Boolean put(EntityPlayer player, Boolean value) { + Boolean res = super.put(player, value); + if (res == null) { + updateLastPlayer(player); + } + return res; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } + } else { + try { + Set delegate = super.trackedPlayers; + TRACKING_SET_SETTER.invoke(this, new ForwardingSet() { + @Override + public boolean add(EntityPlayer player) { + boolean res = super.add(player); + if (res) { + updateLastPlayer(player); + } + return res; + } + + @Override + protected Set delegate() { + return delegate; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } } } @@ -135,11 +160,22 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { } private static final MethodHandle D = NMS.getGetter(EntityTrackerEntry.class, "d"); - private static final MethodHandle E = NMS.getGetter(EntityTrackerEntry.class, "e"); private static final MethodHandle TRACKER = NMS.getGetter(EntityTracker.class, "tracker"); private static final MethodHandle TRACKER_ENTRY = NMS.getGetter(EntityTracker.class, "trackerEntry"); + private static MethodHandle TRACKING_MAP_GETTER; + private static MethodHandle TRACKING_MAP_SETTER; private static final MethodHandle TRACKING_RANGE = NMS.getGetter(EntityTracker.class, "trackingDistance"); private static final MethodHandle TRACKING_RANGE_SETTER = NMS.getFirstFinalSetter(EntityTracker.class, int.class); private static final MethodHandle TRACKING_SET_SETTER = NMS.getFirstFinalSetter(EntityTracker.class, Set.class); + static { + try { + // Old paper versions override the tracked player set to be a map + if (EntityTracker.class.getField("trackedPlayerMap") != null) { + TRACKING_MAP_SETTER = NMS.getFirstSetter(EntityTracker.class, Map.class); + TRACKING_MAP_GETTER = NMS.getFirstGetter(EntityTracker.class, Map.class); + } + } catch (Exception e) { + } + } }