Fix 1.7 and 1.8 compatibility

This commit is contained in:
sgdc3 2017-10-01 19:53:15 +02:00
parent d9399568a3
commit 843baa8e4a
3 changed files with 39 additions and 23 deletions

View File

@ -43,7 +43,6 @@ import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerShearEntityEvent; import org.bukkit.event.player.PlayerShearEntityEvent;
import org.spigotmc.event.player.PlayerSpawnLocationEvent;
import javax.inject.Inject; import javax.inject.Inject;
@ -80,8 +79,7 @@ public class PlayerListener implements Listener {
@Inject @Inject
private JoinMessageService joinMessageService; private JoinMessageService joinMessageService;
private boolean isAsyncPlayerPreLoginEventCalled = false; private static boolean IS_ASYNC_PLAYER_PRE_LOGIN_EVENT_CALLED = false;
private boolean isPlayerSpawnLocationEventCalled = false;
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
@ -189,26 +187,11 @@ public class PlayerListener implements Listener {
} }
} }
// Note: the following event is called since MC1.9, in older versions we have to fallback on the PlayerJoinEvent
@EventHandler(priority = EventPriority.HIGH)
public void onPlayerSpawn(PlayerSpawnLocationEvent event) {
isPlayerSpawnLocationEventCalled = true;
final Player player = event.getPlayer();
management.performJoin(player, event.getSpawnLocation());
Location customSpawnLocation = teleportationService.prepareOnJoinSpawnLocation(player);
if (customSpawnLocation != null) {
event.setSpawnLocation(customSpawnLocation);
}
}
@EventHandler(priority = EventPriority.NORMAL) @EventHandler(priority = EventPriority.NORMAL)
public void onPlayerJoin(PlayerJoinEvent event) { public void onPlayerJoin(PlayerJoinEvent event) {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
if (!isPlayerSpawnLocationEventCalled) { if (!PlayerListener19.isIsPlayerSpawnLocationEventCalled()) {
teleportationService.teleportOnJoin(player); teleportationService.teleportOnJoin(player);
management.performJoin(player, player.getLocation()); management.performJoin(player, player.getLocation());
} }
@ -239,7 +222,7 @@ public class PlayerListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
public void onAsyncPlayerPreLoginEvent(AsyncPlayerPreLoginEvent event) { public void onAsyncPlayerPreLoginEvent(AsyncPlayerPreLoginEvent event) {
isAsyncPlayerPreLoginEventCalled = true; IS_ASYNC_PLAYER_PRE_LOGIN_EVENT_CALLED = true;
final String name = event.getName(); final String name = event.getName();
@ -274,7 +257,7 @@ public class PlayerListener implements Listener {
return; return;
} }
if (!isAsyncPlayerPreLoginEventCalled) { if (!IS_ASYNC_PLAYER_PRE_LOGIN_EVENT_CALLED) {
try { try {
runOnJoinChecks(name, event.getAddress().getHostAddress()); runOnJoinChecks(name, event.getAddress().getHostAddress());
} catch (FailedVerificationException e) { } catch (FailedVerificationException e) {

View File

@ -2,19 +2,50 @@ package fr.xephi.authme.listener;
import javax.inject.Inject; import javax.inject.Inject;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.TeleportationService;
import org.bukkit.Location;
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.player.PlayerSwapHandItemsEvent; import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.spigotmc.event.player.PlayerSpawnLocationEvent;
/** /**
* Listener of player events for events introduced in Minecraft 1.9. * Listener of player events for events introduced in Minecraft 1.9.
*/ */
public class PlayerListener19 implements Listener { public class PlayerListener19 implements Listener {
@Inject
private Management management;
@Inject
private TeleportationService teleportationService;
@Inject @Inject
private ListenerService listenerService; private ListenerService listenerService;
private static boolean IS_PLAYER_SPAWN_LOCATION_EVENT_CALLED = false;
public static boolean isIsPlayerSpawnLocationEventCalled() {
return IS_PLAYER_SPAWN_LOCATION_EVENT_CALLED;
}
// Note: the following event is called since MC1.9, in older versions we have to fallback on the PlayerJoinEvent
@EventHandler(priority = EventPriority.HIGH)
public void onPlayerSpawn(PlayerSpawnLocationEvent event) {
PlayerListener19.IS_PLAYER_SPAWN_LOCATION_EVENT_CALLED = true;
final Player player = event.getPlayer();
management.performJoin(player, event.getSpawnLocation());
Location customSpawnLocation = teleportationService.prepareOnJoinSpawnLocation(player);
if (customSpawnLocation != null) {
event.setSpawnLocation(customSpawnLocation);
}
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) { public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) {
if (listenerService.shouldCancelEvent(event)) { if (listenerService.shouldCancelEvent(event)) {

View File

@ -32,7 +32,7 @@ public final class ListenerConsistencyTest {
"PlayerListener#onPlayerQuit", "ServerListener#onPluginDisable", "PlayerListener#onPlayerQuit", "ServerListener#onPluginDisable",
"ServerListener#onServerPing", "ServerListener#onPluginEnable", "ServerListener#onServerPing", "ServerListener#onPluginEnable",
"PlayerListener#onJoinMessage", "PlayerListener#onAsyncPlayerPreLoginEvent", "PlayerListener#onJoinMessage", "PlayerListener#onAsyncPlayerPreLoginEvent",
"PlayerListener#onPlayerSpawn"); "PlayerListener19#onPlayerSpawn");
@BeforeClass @BeforeClass
public static void collectListenerClasses() { public static void collectListenerClasses() {
@ -120,7 +120,9 @@ public final class ListenerConsistencyTest {
// Exclude any methods with "$" in it: jacoco creates a "$jacocoInit" method we want to ignore, and // Exclude any methods with "$" in it: jacoco creates a "$jacocoInit" method we want to ignore, and
// methods like "access$000" are created by the compiler when a private member is being accessed by an inner // methods like "access$000" are created by the compiler when a private member is being accessed by an inner
// class, which is not of interest for us // class, which is not of interest for us
if (Modifier.isPrivate(method.getModifiers()) || method.getName().contains("$")) { // Also exclude getters
String methodName = method.getName();
if (Modifier.isPrivate(method.getModifiers()) || methodName.contains("$") || methodName.startsWith("get") || methodName.startsWith("is")) {
return false; return false;
} }
// Skip reload() method (implementation of Reloadable interface) // Skip reload() method (implementation of Reloadable interface)