Merge remote-tracking branch 'upstream/2.x' into 2.x-upstream

This commit is contained in:
md678685 2017-08-04 12:09:03 +01:00
commit a27eb17684

View File

@ -1,25 +1,60 @@
package com.earth2me.essentials; package com.earth2me.essentials;
import net.ess3.api.IEssentials; import net.ess3.api.IEssentials;
import net.ess3.nms.refl.ReflUtil;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerListPingEvent; import org.bukkit.event.server.ServerListPingEvent;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
public class EssentialsServerListener implements Listener { public class EssentialsServerListener implements Listener {
private final transient IEssentials ess; private final transient IEssentials ess;
private boolean errorLogged = false; private boolean errorLogged = false;
private boolean isPaperSample;
private Method setSampleText;
private Method getSampleText;
public EssentialsServerListener(final IEssentials ess) { public EssentialsServerListener(final IEssentials ess) {
this.ess = ess; this.ess = ess;
setSampleText = ReflUtil.getMethodCached(ServerListPingEvent.class, "setSampleText", List.class);
getSampleText = ReflUtil.getMethodCached(ServerListPingEvent.class, "getSampleText");
if (setSampleText != null && getSampleText != null) {
ess.getLogger().info("Using Paper 1.12+ ServerListPingEvent methods");
isPaperSample = true;
} else {
ess.getLogger().info("Using Spigot 1.7.10+ ServerListPingEvent iterator");
isPaperSample = false;
}
} }
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
public void onServerListPing(final ServerListPingEvent event) { public void onServerListPing(final ServerListPingEvent event) {
if (isPaperSample) {
try {
List<String> playerNames = (List<String>) getSampleText.invoke(event, null);
Iterator<String> iterator = playerNames.iterator();
while (iterator.hasNext()) {
String player = iterator.next();
if (ess.getUser(player).isVanished()) {
iterator.remove();
}
}
setSampleText.invoke(event, playerNames);
} catch (IllegalAccessException | InvocationTargetException | ClassCastException e) {
if (!errorLogged) {
ess.getLogger().log(Level.WARNING, "Unable to hide players from server list ping using Paper 1.12+ method!", e);
errorLogged = true;
}
}
} else {
try { try {
Iterator<Player> iterator = event.iterator(); Iterator<Player> iterator = event.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
@ -36,4 +71,5 @@ public class EssentialsServerListener implements Listener {
} }
} }
} }
}
} }