From 3b29ee5dc042b4009c6ffbebc6c859b6975bb396 Mon Sep 17 00:00:00 2001 From: mathiascode Date: Sun, 10 May 2020 16:23:33 +0300 Subject: [PATCH] Preserve pitch and yaw when teleporting --- pom.xml | 2 +- src/main/java/pw/kaboom/extras/Main.java | 12 +++++++ .../extras/commands/CommandUsername.java | 10 +++--- .../extras/modules/entity/EntityTeleport.java | 14 ++++----- .../modules/player/PlayerConnection.java | 28 ++--------------- .../extras/modules/player/PlayerRecipe.java | 20 ++++++++++++ .../extras/modules/server/ServerTick.java | 31 +++++++++++++++++++ 7 files changed, 77 insertions(+), 40 deletions(-) create mode 100644 src/main/java/pw/kaboom/extras/modules/player/PlayerRecipe.java create mode 100644 src/main/java/pw/kaboom/extras/modules/server/ServerTick.java diff --git a/pom.xml b/pom.xml index c1ad158..0db46a2 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ com.destroystokyo.paper paper-api - 1.13.2-R0.1-SNAPSHOT + 1.15.2-R0.1-SNAPSHOT com.comphenix.protocol diff --git a/src/main/java/pw/kaboom/extras/Main.java b/src/main/java/pw/kaboom/extras/Main.java index 06f408b..dff703d 100644 --- a/src/main/java/pw/kaboom/extras/Main.java +++ b/src/main/java/pw/kaboom/extras/Main.java @@ -2,6 +2,7 @@ package pw.kaboom.extras; import java.util.Collections; +import org.bukkit.Bukkit; import org.bukkit.block.BlockFace; import org.bukkit.plugin.java.JavaPlugin; @@ -37,9 +38,11 @@ import pw.kaboom.extras.modules.player.PlayerCommand; import pw.kaboom.extras.modules.player.PlayerConnection; import pw.kaboom.extras.modules.player.PlayerDamage; import pw.kaboom.extras.modules.player.PlayerInteract; +import pw.kaboom.extras.modules.player.PlayerRecipe; import pw.kaboom.extras.modules.player.PlayerTeleport; import pw.kaboom.extras.modules.server.ServerCommand; import pw.kaboom.extras.modules.server.ServerPing; +import pw.kaboom.extras.modules.server.ServerTick; public final class Main extends JavaPlugin { @Override @@ -105,10 +108,19 @@ public final class Main extends JavaPlugin { this.getServer().getPluginManager().registerEvents(new PlayerConnection(), this); this.getServer().getPluginManager().registerEvents(new PlayerDamage(), this); this.getServer().getPluginManager().registerEvents(new PlayerInteract(), this); + this.getServer().getPluginManager().registerEvents(new PlayerRecipe(), this); this.getServer().getPluginManager().registerEvents(new PlayerTeleport(), this); /* Server-related modules */ this.getServer().getPluginManager().registerEvents(new ServerCommand(), this); this.getServer().getPluginManager().registerEvents(new ServerPing(), this); + this.getServer().getPluginManager().registerEvents(new ServerTick(), this); + } + + @Override + public void onDisable() { + if (Bukkit.isStopping()) { + System.exit(1); + } } } diff --git a/src/main/java/pw/kaboom/extras/commands/CommandUsername.java b/src/main/java/pw/kaboom/extras/commands/CommandUsername.java index c746f77..c886e20 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandUsername.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandUsername.java @@ -32,11 +32,10 @@ public final class CommandUsername implements CommandExecutor { } else if (millisDifference <= 2000) { player.sendMessage("Please wait a few seconds before changing your username"); } else { - for (Player onlinePlayer : Bukkit.getOnlinePlayers()) { - if (name.equals(onlinePlayer.getName())) { - player.sendMessage("A player with that username is already logged in"); - return true; - } + if (Bukkit.getPlayer(name) != null + && Bukkit.getPlayer(name).isOnline()) { + player.sendMessage("A player with that username is already logged in"); + return true; } final PlayerProfile profile = player.getPlayerProfile(); @@ -44,7 +43,6 @@ public final class CommandUsername implements CommandExecutor { profile.setName(name); player.setPlayerProfile(profile); - player.setOp(true); millis = System.currentTimeMillis(); diff --git a/src/main/java/pw/kaboom/extras/modules/entity/EntityTeleport.java b/src/main/java/pw/kaboom/extras/modules/entity/EntityTeleport.java index bbf9869..37b6a22 100644 --- a/src/main/java/pw/kaboom/extras/modules/entity/EntityTeleport.java +++ b/src/main/java/pw/kaboom/extras/modules/entity/EntityTeleport.java @@ -15,25 +15,25 @@ public final class EntityTeleport implements Listener { final int minValue = -30000000; if (x > maxValue) { - x = maxValue; + location.setX(maxValue); } if (x < minValue) { - x = minValue; + location.setX(minValue); } if (y > maxValue) { - y = maxValue; + location.setY(maxValue); } if (y < minValue) { - y = minValue; + location.setY(minValue); } if (z > maxValue) { - z = maxValue; + location.setZ(maxValue); } if (z < minValue) { - z = minValue; + location.setZ(minValue); } - return new Location(location.getWorld(), x, y, z); + return location; } @EventHandler diff --git a/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java b/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java index b67e3fd..2ab3ab1 100644 --- a/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java +++ b/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java @@ -4,8 +4,6 @@ import java.util.HashSet; import java.util.UUID; import org.bukkit.Bukkit; -import org.bukkit.GameRule; -import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -15,7 +13,6 @@ import org.bukkit.event.player.PlayerKickEvent; import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerLoginEvent.Result; import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.event.player.PlayerStatisticIncrementEvent; import org.bukkit.plugin.java.JavaPlugin; import com.destroystokyo.paper.event.profile.PreLookupProfileEvent; @@ -27,7 +24,8 @@ import pw.kaboom.extras.Main; public final class PlayerConnection implements Listener { @EventHandler void onAsyncPlayerPreLogin(final AsyncPlayerPreLoginEvent event) { - if (Bukkit.getPlayer(event.getName()) != null) { + if (Bukkit.getPlayer(event.getName()) != null + && Bukkit.getPlayer(event.getName()).isOnline()) { event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, "A player with that username is already logged in"); } @@ -109,28 +107,6 @@ public final class PlayerConnection implements Listener { //PlayerInteract.interactMillisList.remove(event.getPlayer().getUniqueId()); } - @EventHandler - void onPlayerStatisticIncrement(final PlayerStatisticIncrementEvent event) { - final World world = event.getPlayer().getWorld(); - final Integer randomTickSpeed = world.getGameRuleValue(GameRule.RANDOM_TICK_SPEED); - - if (randomTickSpeed > 6) { - world.setGameRule(GameRule.RANDOM_TICK_SPEED, 6); - } - - final Integer spawnRadius = world.getGameRuleValue(GameRule.SPAWN_RADIUS); - - if (spawnRadius > 100) { - world.setGameRule(GameRule.SPAWN_RADIUS, 100); - } - - if (!world.isAutoSave()) { - world.setAutoSave(true); - } - - event.setCancelled(true); - } - @EventHandler void onPreLookupProfile(final PreLookupProfileEvent event) { // Disable Mojang API calls, we don't need them diff --git a/src/main/java/pw/kaboom/extras/modules/player/PlayerRecipe.java b/src/main/java/pw/kaboom/extras/modules/player/PlayerRecipe.java new file mode 100644 index 0000000..8116b8e --- /dev/null +++ b/src/main/java/pw/kaboom/extras/modules/player/PlayerRecipe.java @@ -0,0 +1,20 @@ +package pw.kaboom.extras.modules.player; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerRecipeDiscoverEvent; + +public final class PlayerRecipe implements Listener { + private long recipeMillis; + + @EventHandler + void onPlayerRecipeDiscover(final PlayerRecipeDiscoverEvent event) { + final long millisDifference = System.currentTimeMillis() - recipeMillis; + + if (millisDifference < 75) { + event.setCancelled(true); + } + + recipeMillis = System.currentTimeMillis(); + } +} diff --git a/src/main/java/pw/kaboom/extras/modules/server/ServerTick.java b/src/main/java/pw/kaboom/extras/modules/server/ServerTick.java new file mode 100644 index 0000000..00dacd5 --- /dev/null +++ b/src/main/java/pw/kaboom/extras/modules/server/ServerTick.java @@ -0,0 +1,31 @@ +package pw.kaboom.extras.modules.server; + +import org.bukkit.GameRule; +import org.bukkit.World; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerStatisticIncrementEvent; + +public final class ServerTick implements Listener { + @EventHandler + void onPlayerStatisticIncrement(final PlayerStatisticIncrementEvent event) { + final World world = event.getPlayer().getWorld(); + final Integer randomTickSpeed = world.getGameRuleValue(GameRule.RANDOM_TICK_SPEED); + + if (randomTickSpeed > 6) { + world.setGameRule(GameRule.RANDOM_TICK_SPEED, 6); + } + + final Integer spawnRadius = world.getGameRuleValue(GameRule.SPAWN_RADIUS); + + if (spawnRadius > 100) { + world.setGameRule(GameRule.SPAWN_RADIUS, 100); + } + + if (!world.isAutoSave()) { + world.setAutoSave(true); + } + + event.setCancelled(true); + } +}