From 977bbbb70ca4f23aabd405aa6b628c5a17391bff Mon Sep 17 00:00:00 2001 From: Xephi59 Date: Thu, 11 Aug 2016 15:25:31 +0200 Subject: [PATCH] Prevent player for switching items in hands with shortkeys --- src/main/java/fr/xephi/authme/AuthMe.java | 64 +++++++++++-------- .../authme/listener/PlayerListener19.java | 25 ++++++++ .../authme/listener/PlayerListener19Test.java | 27 ++++++++ 3 files changed, 89 insertions(+), 27 deletions(-) create mode 100644 src/main/java/fr/xephi/authme/listener/PlayerListener19.java create mode 100644 src/test/java/fr/xephi/authme/listener/PlayerListener19Test.java diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index b4d85dbc8..952ed923a 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -1,8 +1,36 @@ package fr.xephi.authme; +import static fr.xephi.authme.settings.properties.EmailSettings.RECALL_PLAYERS; +import static fr.xephi.authme.util.BukkitService.TICKS_PER_MINUTE; + +import java.io.File; +import java.io.IOException; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.logging.log4j.LogManager; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Server; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.plugin.PluginDescriptionFile; +import org.bukkit.plugin.PluginLoader; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitScheduler; +import org.bukkit.scheduler.BukkitWorker; + +import com.google.common.annotations.VisibleForTesting; + import ch.jalu.injector.Injector; import ch.jalu.injector.InjectorBuilder; -import com.google.common.annotations.VisibleForTesting; import fr.xephi.authme.api.API; import fr.xephi.authme.api.NewAPI; import fr.xephi.authme.cache.auth.PlayerAuth; @@ -24,6 +52,7 @@ import fr.xephi.authme.listener.EntityListener; import fr.xephi.authme.listener.PlayerListener; import fr.xephi.authme.listener.PlayerListener16; import fr.xephi.authme.listener.PlayerListener18; +import fr.xephi.authme.listener.PlayerListener19; import fr.xephi.authme.listener.ServerListener; import fr.xephi.authme.output.ConsoleFilter; import fr.xephi.authme.output.Log4JFilter; @@ -52,32 +81,6 @@ import fr.xephi.authme.util.MigrationService; import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.Utils; import fr.xephi.authme.util.ValidationService; -import org.apache.logging.log4j.LogManager; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Server; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.plugin.PluginDescriptionFile; -import org.bukkit.plugin.PluginLoader; -import org.bukkit.plugin.PluginManager; -import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.scheduler.BukkitScheduler; -import org.bukkit.scheduler.BukkitWorker; - -import java.io.File; -import java.io.IOException; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; - -import static fr.xephi.authme.settings.properties.EmailSettings.RECALL_PLAYERS; -import static fr.xephi.authme.util.BukkitService.TICKS_PER_MINUTE; /** * The AuthMe main class. @@ -324,6 +327,13 @@ public class AuthMe extends JavaPlugin { pluginManager.registerEvents(injector.getSingleton(PlayerListener18.class), this); } catch (ClassNotFoundException ignore) { } + + // Try to register 1.9 player listeners + try { + Class.forName("org.bukkit.event.player.PlayerSwapHandItemsEvent"); + pluginManager.registerEvents(injector.getSingleton(PlayerListener19.class), this); + } catch (ClassNotFoundException ignore) { + } } /** diff --git a/src/main/java/fr/xephi/authme/listener/PlayerListener19.java b/src/main/java/fr/xephi/authme/listener/PlayerListener19.java new file mode 100644 index 000000000..ce2febe03 --- /dev/null +++ b/src/main/java/fr/xephi/authme/listener/PlayerListener19.java @@ -0,0 +1,25 @@ +package fr.xephi.authme.listener; + +import javax.inject.Inject; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerSwapHandItemsEvent; + +/** + * Listener of player events for events introduced in Minecraft 1.9. + */ +public class PlayerListener19 implements Listener { + + @Inject + private ListenerService listenerService; + + @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) + public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) { + if (listenerService.shouldCancelEvent(event)) { + event.setCancelled(true); + } + } + +} diff --git a/src/test/java/fr/xephi/authme/listener/PlayerListener19Test.java b/src/test/java/fr/xephi/authme/listener/PlayerListener19Test.java new file mode 100644 index 000000000..8fcd39b68 --- /dev/null +++ b/src/test/java/fr/xephi/authme/listener/PlayerListener19Test.java @@ -0,0 +1,27 @@ +package fr.xephi.authme.listener; + +import org.bukkit.event.player.PlayerSwapHandItemsEvent; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +/** + * Test for {@link PlayerListener19}. + */ +@RunWith(MockitoJUnitRunner.class) +public class PlayerListener19Test { + + @InjectMocks + private PlayerListener19 listener; + + @Mock + private ListenerService listenerService; + + @Test + public void shouldCancelEvent() { + ListenerTestUtils.checkEventIsCanceledForUnauthed(listener, listenerService, PlayerSwapHandItemsEvent.class); + } + +}