From b6c804ae103efddb26179f47c5902c9ebdd38207 Mon Sep 17 00:00:00 2001 From: mathiascode Date: Tue, 17 Dec 2019 18:59:28 +0200 Subject: [PATCH] Enforce checkstyle --- .gitignore | 8 +- README.md | 23 +++- build.sh | 2 - pom.xml | 30 +++-- .../{Events.java => ControlPlayer.java} | 103 +++++++++--------- src/main/java/pw/kaboom/icontrolu/Main.java | 21 ++-- .../CommandIcu.java} | 47 ++++---- .../icontrolu/utilities/PlayerList.java | 38 +++++++ suppressions.xml | 13 +++ 9 files changed, 184 insertions(+), 101 deletions(-) delete mode 100755 build.sh rename src/main/java/pw/kaboom/icontrolu/{Events.java => ControlPlayer.java} (61%) rename src/main/java/pw/kaboom/icontrolu/{Commands.java => commands/CommandIcu.java} (74%) create mode 100644 src/main/java/pw/kaboom/icontrolu/utilities/PlayerList.java create mode 100644 suppressions.xml diff --git a/.gitignore b/.gitignore index d7eca3b..3f88fb7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ -.gradle/ -target/ \ No newline at end of file +.settings/ +bin/ +target/ +.checkstyle +.classpath +.project diff --git a/README.md b/README.md index dec7919..221b2f6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,22 @@ -# icontrolu +# iControlU -Source code for the iControlU plugin on the Kaboom server +Weapons is a Bukkit plugin that allows players to control other players. + +The plugin is created for the Kaboom server. + +## Commands + +| Command | Permission | Description | +| ------- | ---------- | ----------- | +|/icu | icu.command | Control another player's movements, inventory and chat| + +## Compiling + +Use [Maven](https://maven.apache.org/) to compile the plugin. +```bash +mvn package +``` +The generated .jar file will be located in the target/ folder. + +## License +[Unlicense](https://unlicense.org/) \ No newline at end of file diff --git a/build.sh b/build.sh deleted file mode 100755 index 628a9f5..0000000 --- a/build.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -mvn package \ No newline at end of file diff --git a/pom.xml b/pom.xml index 945b092..1ccd811 100644 --- a/pom.xml +++ b/pom.xml @@ -5,8 +5,10 @@ master - 1.7 - 1.7 + 1.0 + 1.0 + true + UTF-8 @@ -29,13 +31,23 @@ org.apache.maven.plugins - maven-compiler-plugin - 3.8.0 - - - -Xlint:all - - + maven-checkstyle-plugin + 3.1.0 + + + checkstyle + validate + + check + + + + suppressions.xml + + true + + + diff --git a/src/main/java/pw/kaboom/icontrolu/Events.java b/src/main/java/pw/kaboom/icontrolu/ControlPlayer.java similarity index 61% rename from src/main/java/pw/kaboom/icontrolu/Events.java rename to src/main/java/pw/kaboom/icontrolu/ControlPlayer.java index 96f56e6..76752da 100644 --- a/src/main/java/pw/kaboom/icontrolu/Events.java +++ b/src/main/java/pw/kaboom/icontrolu/ControlPlayer.java @@ -1,15 +1,13 @@ package pw.kaboom.icontrolu; import org.bukkit.Bukkit; - -import org.bukkit.entity.Player; +import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeInstance; import org.bukkit.entity.Entity; - +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; - import org.bukkit.event.entity.EntityDamageEvent; - import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerDropItemEvent; @@ -17,25 +15,20 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerRespawnEvent; - import org.bukkit.plugin.java.JavaPlugin; - import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; - import org.bukkit.scheduler.BukkitRunnable; - import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Team; -import org.bukkit.scoreboard.Team.Option; -import org.bukkit.scoreboard.Team.OptionStatus; -import org.bukkit.scheduler.BukkitRunnable; +import pw.kaboom.icontrolu.utilities.PlayerList; class Tick extends BukkitRunnable { + @Override public void run() { for (Player target: Bukkit.getOnlinePlayers()) { - final Player controller = Main.controllerFor.get(target.getUniqueId()); + final Player controller = PlayerList.getController(target.getUniqueId()); if (controller != null) { for (int i = 0; i < controller.getInventory().getSize(); i++) { @@ -52,11 +45,14 @@ class Tick extends BukkitRunnable { target.teleportAsync(controller.getLocation()); } + AttributeInstance controllerMaxHealth = target.getAttribute(Attribute.GENERIC_MAX_HEALTH); + AttributeInstance targetMaxHealth = target.getAttribute(Attribute.GENERIC_MAX_HEALTH); + targetMaxHealth.setBaseValue(controllerMaxHealth.getBaseValue()); + target.setAllowFlight(controller.getAllowFlight()); target.setExhaustion(controller.getExhaustion()); target.setFlying(controller.isFlying()); target.setFoodLevel(controller.getFoodLevel()); - target.setMaxHealth(controller.getMaxHealth()); target.setHealth(controller.getHealth()); target.setLevel(controller.getLevel()); target.setSneaking(controller.isSneaking()); @@ -65,7 +61,7 @@ class Tick extends BukkitRunnable { for (Player player: Bukkit.getOnlinePlayers()) { player.hidePlayer(JavaPlugin.getPlugin(Main.class), controller); } - + final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard(); Team team = scoreboard.getTeam("icuDisableCollision"); @@ -73,20 +69,25 @@ class Tick extends BukkitRunnable { team = scoreboard.registerNewTeam("icuDisableCollision"); } - team.setCanSeeFriendlyInvisibles​(false); + team.setCanSeeFriendlyInvisibles(false); team.setOption(Team.Option.COLLISION_RULE, Team.OptionStatus.NEVER); if (!team.hasEntry(controller.getName())) { team.addEntry(controller.getName()); } + final int duration = 99999; + final int amplifier = 0; + final boolean ambient = false; + final boolean particles = false; + controller.addPotionEffect( new PotionEffect( PotionEffectType.INVISIBILITY, - 99999, - 0, - false, - false + duration, + amplifier, + ambient, + particles ) ); } @@ -94,17 +95,17 @@ class Tick extends BukkitRunnable { } } -class Events implements Listener { +class ControlPlayer implements Listener { @EventHandler - void onAsyncPlayerChat(AsyncPlayerChatEvent event) { + private void onAsyncPlayerChat(final AsyncPlayerChatEvent event) { final Player player = event.getPlayer(); - if (Main.controllerFor.containsKey(player.getUniqueId())) { + if (PlayerList.getController(player.getUniqueId()) != null) { event.setCancelled(true); } - if (Main.targetFor.containsKey(player.getUniqueId())) { - final Player target = Main.targetFor.get(player.getUniqueId()); + if (PlayerList.getTarget(player.getUniqueId()) != null) { + final Player target = PlayerList.getTarget(player.getUniqueId()); target.chat(event.getMessage()); event.setCancelled(true); @@ -112,64 +113,66 @@ class Events implements Listener { } @EventHandler - void onEntityDamage(EntityDamageEvent event) { + private void onEntityDamage(final EntityDamageEvent event) { final Entity player = event.getEntity(); - if (Main.targetFor.containsKey(player.getUniqueId())) { + if (PlayerList.getTarget(player.getUniqueId()) != null) { event.setCancelled(true); } } @EventHandler - void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { + private void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event) { final Player player = event.getPlayer(); - if (Main.controllerFor.containsKey(player.getUniqueId())) { + if (PlayerList.getController(player.getUniqueId()) != null) { event.setCancelled(true); } } @EventHandler - void onPlayerDropItem(PlayerDropItemEvent event) { + private void onPlayerDropItem(final PlayerDropItemEvent event) { final Player player = event.getPlayer(); - if (Main.controllerFor.containsKey(player.getUniqueId())) { + if (PlayerList.getController(player.getUniqueId()) != null) { event.setCancelled(true); } } @EventHandler - void onPlayerInteract(PlayerInteractEvent event) { + private void onPlayerInteract(final PlayerInteractEvent event) { final Player player = event.getPlayer(); - if (Main.controllerFor.containsKey(player.getUniqueId())) { + if (PlayerList.getController(player.getUniqueId()) != null) { event.setCancelled(true); } } @EventHandler - void onPlayerMove(PlayerMoveEvent event) { + private void onPlayerMove(final PlayerMoveEvent event) { final Player player = event.getPlayer(); - if (Main.controllerFor.containsKey(player.getUniqueId())) { + if (PlayerList.getController(player.getUniqueId()) != null) { event.setCancelled(true); } } @EventHandler - void onPlayerQuit(PlayerQuitEvent event) { + private void onPlayerQuit(final PlayerQuitEvent event) { final Player player = event.getPlayer(); for (Player otherPlayer: Bukkit.getOnlinePlayers()) { /* Target disconnects */ - if (Main.controllerFor.containsKey(player.getUniqueId()) && - Main.controllerFor.get(player.getUniqueId()).equals(otherPlayer)) { - Main.targetFor.remove(otherPlayer.getUniqueId()); - Main.controllerFor.remove(player.getUniqueId()); + if (PlayerList.getController(player.getUniqueId()) != null + && PlayerList.getController(player.getUniqueId()).equals(otherPlayer)) { + PlayerList.removeTarget(otherPlayer.getUniqueId()); + PlayerList.removeController(player.getUniqueId()); final Player controller = otherPlayer; + final int tickDelay = 200; new BukkitRunnable() { + @Override public void run() { for (Player allPlayers: Bukkit.getOnlinePlayers()) { allPlayers.showPlayer(JavaPlugin.getPlugin(Main.class), controller); @@ -178,34 +181,34 @@ class Events implements Listener { final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard(); final Team team = scoreboard.getTeam("icuDisableCollision"); - if (team != null && - team.hasEntry(controller.getName()) == true) { + if (team != null + && team.hasEntry(controller.getName())) { team.removeEntry(controller.getName()); } controller.removePotionEffect(PotionEffectType.INVISIBILITY); controller.sendMessage("You are now visible"); } - }.runTaskLater(JavaPlugin.getPlugin(Main.class), 200); + }.runTaskLater(JavaPlugin.getPlugin(Main.class), tickDelay); otherPlayer.sendMessage("The player you were controlling has disconnected. You are invisible for 10 seconds."); } /* Controller disconnects */ - if (Main.targetFor.containsKey(player.getUniqueId()) && - Main.targetFor.get(player.getUniqueId()).equals(otherPlayer)) { - Main.targetFor.remove(player.getUniqueId()); - Main.controllerFor.remove(otherPlayer.getUniqueId()); + if (PlayerList.getTarget(player.getUniqueId()) != null + && PlayerList.getTarget(player.getUniqueId()).equals(otherPlayer)) { + PlayerList.removeTarget(player.getUniqueId()); + PlayerList.removeController(otherPlayer.getUniqueId()); } } } @EventHandler - void onPlayerRespawn(PlayerRespawnEvent event) { + private void onPlayerRespawn(final PlayerRespawnEvent event) { final Player player = event.getPlayer(); - if (Main.controllerFor.containsKey(player.getUniqueId())) { - final Player controller = Main.controllerFor.get(player.getUniqueId()); + if (PlayerList.getController(player.getUniqueId()) != null) { + final Player controller = PlayerList.getController(player.getUniqueId()); controller.teleportAsync(player.getLocation()); } diff --git a/src/main/java/pw/kaboom/icontrolu/Main.java b/src/main/java/pw/kaboom/icontrolu/Main.java index 427e173..b23f261 100644 --- a/src/main/java/pw/kaboom/icontrolu/Main.java +++ b/src/main/java/pw/kaboom/icontrolu/Main.java @@ -1,23 +1,17 @@ package pw.kaboom.icontrolu; -import java.util.HashMap; -import java.util.UUID; - import org.bukkit.Bukkit; - import org.bukkit.entity.Player; - -import org.bukkit.potion.PotionEffectType; - import org.bukkit.plugin.java.JavaPlugin; - +import org.bukkit.potion.PotionEffectType; import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Team; -public class Main extends JavaPlugin { - static HashMap controllerFor = new HashMap<>(); - static HashMap targetFor = new HashMap<>(); +import pw.kaboom.icontrolu.commands.CommandIcu; +import pw.kaboom.icontrolu.utilities.PlayerList; +public final class Main extends JavaPlugin { + @Override public void onEnable() { /* Setup scoreboard team to prevent player collisions */ final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard(); @@ -30,12 +24,13 @@ public class Main extends JavaPlugin { this.getCommand("icu").setExecutor(new CommandIcu()); new Tick().runTaskTimer(this, 0, 1); - this.getServer().getPluginManager().registerEvents(new Events(), this); + this.getServer().getPluginManager().registerEvents(new ControlPlayer(), this); } + @Override public void onDisable() { for (Player controller: Bukkit.getOnlinePlayers()) { - final Player target = Main.targetFor.get(controller.getUniqueId()); + final Player target = PlayerList.getTarget(controller.getUniqueId()); if (target != null) { for (Player player: Bukkit.getOnlinePlayers()) { diff --git a/src/main/java/pw/kaboom/icontrolu/Commands.java b/src/main/java/pw/kaboom/icontrolu/commands/CommandIcu.java similarity index 74% rename from src/main/java/pw/kaboom/icontrolu/Commands.java rename to src/main/java/pw/kaboom/icontrolu/commands/CommandIcu.java index d70a626..8c4703f 100644 --- a/src/main/java/pw/kaboom/icontrolu/Commands.java +++ b/src/main/java/pw/kaboom/icontrolu/commands/CommandIcu.java @@ -1,26 +1,24 @@ -package pw.kaboom.icontrolu; +package pw.kaboom.icontrolu.commands; import org.bukkit.Bukkit; import org.bukkit.ChatColor; - import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; - import org.bukkit.entity.Player; - import org.bukkit.plugin.java.JavaPlugin; - import org.bukkit.potion.PotionEffectType; - import org.bukkit.scheduler.BukkitRunnable; - import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Team; -class CommandIcu implements CommandExecutor { - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { +import pw.kaboom.icontrolu.Main; +import pw.kaboom.icontrolu.utilities.PlayerList; + +public final class CommandIcu implements CommandExecutor { + @Override + public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { if (sender instanceof ConsoleCommandSender) { sender.sendMessage("Command has to be run by a player"); } else { @@ -37,22 +35,22 @@ class CommandIcu implements CommandExecutor { if (target != null) { if (target == controller) { controller.sendMessage("You are already controlling yourself"); - } else if (Main.targetFor.containsKey(controller.getUniqueId())) { + } else if (PlayerList.getTarget(controller.getUniqueId()) != null) { controller.sendMessage("You are already controlling \"" + target.getName() + "\""); - } else if (Main.controllerFor.containsKey(target.getUniqueId())) { + } else if (PlayerList.getController(target.getUniqueId()) != null) { controller.sendMessage("Player \"" + target.getName() + "\" is already being controlled"); - } else if (Main.targetFor.containsKey(target.getUniqueId())) { + } else if (PlayerList.getTarget(target.getUniqueId()) != null) { controller.sendMessage("Player \"" + target.getName() + "\" is already controlling another player"); } else if (!controller.canSee(target)) { controller.sendMessage("You may not control this player"); } else { controller.teleportAsync(target.getLocation()); - + controller.getInventory().setContents(target.getInventory().getContents()); - Main.targetFor.put(controller.getUniqueId(), target); - Main.controllerFor.put(target.getUniqueId(), controller); - + PlayerList.setTarget(controller.getUniqueId(), target); + PlayerList.setController(target.getUniqueId(), controller); + controller.sendMessage("You are now controlling \"" + target.getName() + "\""); } } else { @@ -60,29 +58,32 @@ class CommandIcu implements CommandExecutor { } } } else if (args[0].equalsIgnoreCase("stop")) { - final Player target = Main.targetFor.get(controller.getUniqueId()); + final Player target = PlayerList.getTarget(controller.getUniqueId()); if (target != null) { - Main.targetFor.remove(controller.getUniqueId()); - Main.controllerFor.remove(target.getUniqueId()); + PlayerList.removeTarget(controller.getUniqueId()); + PlayerList.removeController(target.getUniqueId()); + + final int tickDelay = 200; new BukkitRunnable() { + @Override public void run() { for (Player player: Bukkit.getOnlinePlayers()) { player.showPlayer(JavaPlugin.getPlugin(Main.class), controller); } - + Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard(); Team team = scoreboard.getTeam("icuDisableCollision"); if (team != null && team.hasEntry(controller.getName())) { team.removeEntry(controller.getName()); } - + controller.removePotionEffect(PotionEffectType.INVISIBILITY); controller.sendMessage("You are now visible"); } - }.runTaskLater(JavaPlugin.getPlugin(Main.class), 200); - + }.runTaskLater(JavaPlugin.getPlugin(Main.class), tickDelay); + controller.sendMessage("You are no longer controlling \"" + target.getName() + "\". You are invisible for 10 seconds."); } else { controller.sendMessage("You are not controlling anyone at the moment"); diff --git a/src/main/java/pw/kaboom/icontrolu/utilities/PlayerList.java b/src/main/java/pw/kaboom/icontrolu/utilities/PlayerList.java new file mode 100644 index 0000000..c5302b9 --- /dev/null +++ b/src/main/java/pw/kaboom/icontrolu/utilities/PlayerList.java @@ -0,0 +1,38 @@ +package pw.kaboom.icontrolu.utilities; + +import java.util.HashMap; +import java.util.UUID; + +import org.bukkit.entity.Player; + +public final class PlayerList { + private PlayerList() { + } + + private static HashMap controllerFor = new HashMap(); + private static HashMap targetFor = new HashMap(); + + public static Player getController(final UUID playerUuid) { + return controllerFor.get(playerUuid); + } + + public static Player getTarget(final UUID playerUuid) { + return targetFor.get(playerUuid); + } + + public static void removeController(final UUID playerUuid) { + controllerFor.remove(playerUuid); + } + + public static void removeTarget(final UUID playerUuid) { + targetFor.remove(playerUuid); + } + + public static void setController(final UUID playerUuid, final Player player) { + controllerFor.put(playerUuid, player); + } + + public static void setTarget(final UUID playerUuid, final Player player) { + targetFor.put(playerUuid, player); + } +} diff --git a/suppressions.xml b/suppressions.xml new file mode 100644 index 0000000..3b75716 --- /dev/null +++ b/suppressions.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + \ No newline at end of file