Fix paper backcompat with playerlist tracker change

This commit is contained in:
fullwall 2023-05-04 00:51:57 +08:00
parent 6b4ef37a46
commit 3e578db3ad
6 changed files with 331 additions and 104 deletions

View File

@ -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<EntityPlayer> delegate = super.trackedPlayers;
TRACKING_SET_SETTER.invoke(this, new ForwardingSet<EntityPlayer>() {
@Override
public boolean add(EntityPlayer player) {
boolean res = super.add(player);
if (res) {
updateLastPlayer(player);
if (TRACKING_MAP_SETTER != null) {
try {
Map<EntityPlayer, Boolean> delegate = (Map<EntityPlayer, Boolean>) TRACKING_MAP_GETTER.invoke(this);
TRACKING_MAP_SETTER.invoke(this, new ForwardingMap<EntityPlayer, Boolean>() {
@Override
protected Map<EntityPlayer, Boolean> delegate() {
return delegate;
}
return res;
}
@Override
protected Set<EntityPlayer> 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<EntityPlayer> delegate = super.trackedPlayers;
TRACKING_SET_SETTER.invoke(this, new ForwardingSet<EntityPlayer>() {
@Override
public boolean add(EntityPlayer player) {
boolean res = super.add(player);
if (res) {
updateLastPlayer(player);
}
return res;
}
@Override
protected Set<EntityPlayer> 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) {
}
}
}

View File

@ -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<EntityPlayer> delegate = super.trackedPlayers;
TRACKING_SET_SETTER.invoke(this, new ForwardingSet<EntityPlayer>() {
@Override
public boolean add(EntityPlayer player) {
boolean res = super.add(player);
if (res) {
updateLastPlayer(player);
if (TRACKING_MAP_SETTER != null) {
try {
Map<EntityPlayer, Boolean> delegate = (Map<EntityPlayer, Boolean>) TRACKING_MAP_GETTER.invoke(this);
TRACKING_MAP_SETTER.invoke(this, new ForwardingMap<EntityPlayer, Boolean>() {
@Override
protected Map<EntityPlayer, Boolean> delegate() {
return delegate;
}
return res;
}
@Override
protected Set<EntityPlayer> 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<EntityPlayer> delegate = super.trackedPlayers;
TRACKING_SET_SETTER.invoke(this, new ForwardingSet<EntityPlayer>() {
@Override
public boolean add(EntityPlayer player) {
boolean res = super.add(player);
if (res) {
updateLastPlayer(player);
}
return res;
}
@Override
protected Set<EntityPlayer> 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) {
}
}
}

View File

@ -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<EntityPlayer> delegate = super.trackedPlayers;
TRACKING_SET_SETTER.invoke(this, new ForwardingSet<EntityPlayer>() {
@Override
public boolean add(EntityPlayer player) {
boolean res = super.add(player);
if (res) {
updateLastPlayer(player);
if (TRACKING_MAP_SETTER != null) {
try {
Map<EntityPlayer, Boolean> delegate = (Map<EntityPlayer, Boolean>) TRACKING_MAP_GETTER.invoke(this);
TRACKING_MAP_SETTER.invoke(this, new ForwardingMap<EntityPlayer, Boolean>() {
@Override
protected Map<EntityPlayer, Boolean> delegate() {
return delegate;
}
return res;
}
@Override
protected Set<EntityPlayer> 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<EntityPlayer> delegate = super.trackedPlayers;
TRACKING_SET_SETTER.invoke(this, new ForwardingSet<EntityPlayer>() {
@Override
public boolean add(EntityPlayer player) {
boolean res = super.add(player);
if (res) {
updateLastPlayer(player);
}
return res;
}
@Override
protected Set<EntityPlayer> 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) {
}
}
}

View File

@ -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<EntityPlayer> delegate = super.trackedPlayers;
TRACKING_SET_SETTER.invoke(this, new ForwardingSet<EntityPlayer>() {
@Override
public boolean add(EntityPlayer player) {
boolean res = super.add(player);
if (res) {
updateLastPlayer(player);
if (TRACKING_MAP_SETTER != null) {
try {
Map<EntityPlayer, Boolean> delegate = (Map<EntityPlayer, Boolean>) TRACKING_MAP_GETTER.invoke(this);
TRACKING_MAP_SETTER.invoke(this, new ForwardingMap<EntityPlayer, Boolean>() {
@Override
protected Map<EntityPlayer, Boolean> delegate() {
return delegate;
}
return res;
}
@Override
protected Set<EntityPlayer> 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<EntityPlayer> delegate = super.trackedPlayers;
TRACKING_SET_SETTER.invoke(this, new ForwardingSet<EntityPlayer>() {
@Override
public boolean add(EntityPlayer player) {
boolean res = super.add(player);
if (res) {
updateLastPlayer(player);
}
return res;
}
@Override
protected Set<EntityPlayer> 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) {
}
}
}

View File

@ -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<EntityPlayer> delegate = super.trackedPlayers;
TRACKING_SET_SETTER.invoke(this, new ForwardingSet<EntityPlayer>() {
@Override
public boolean add(EntityPlayer player) {
boolean res = super.add(player);
if (res) {
updateLastPlayer(player);
if (TRACKING_MAP_SETTER != null) {
try {
Map<EntityPlayer, Boolean> delegate = (Map<EntityPlayer, Boolean>) TRACKING_MAP_GETTER.invoke(this);
TRACKING_MAP_SETTER.invoke(this, new ForwardingMap<EntityPlayer, Boolean>() {
@Override
protected Map<EntityPlayer, Boolean> delegate() {
return delegate;
}
return res;
}
@Override
protected Set<EntityPlayer> 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<EntityPlayer> delegate = super.trackedPlayers;
TRACKING_SET_SETTER.invoke(this, new ForwardingSet<EntityPlayer>() {
@Override
public boolean add(EntityPlayer player) {
boolean res = super.add(player);
if (res) {
updateLastPlayer(player);
}
return res;
}
@Override
protected Set<EntityPlayer> 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) {
}
}
}

View File

@ -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<EntityPlayer> delegate = super.trackedPlayers;
TRACKING_SET_SETTER.invoke(this, new ForwardingSet<EntityPlayer>() {
@Override
public boolean add(EntityPlayer player) {
boolean res = super.add(player);
if (res) {
updateLastPlayer(player);
if (TRACKING_MAP_SETTER != null) {
try {
Map<EntityPlayer, Boolean> delegate = (Map<EntityPlayer, Boolean>) TRACKING_MAP_GETTER.invoke(this);
TRACKING_MAP_SETTER.invoke(this, new ForwardingMap<EntityPlayer, Boolean>() {
@Override
protected Map<EntityPlayer, Boolean> delegate() {
return delegate;
}
return res;
}
@Override
protected Set<EntityPlayer> 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<EntityPlayer> delegate = super.trackedPlayers;
TRACKING_SET_SETTER.invoke(this, new ForwardingSet<EntityPlayer>() {
@Override
public boolean add(EntityPlayer player) {
boolean res = super.add(player);
if (res) {
updateLastPlayer(player);
}
return res;
}
@Override
protected Set<EntityPlayer> 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) {
}
}
}