Enforce checkstyle

This commit is contained in:
mathiascode 2019-12-17 18:59:28 +02:00
parent 27c0caa435
commit b6c804ae10
9 changed files with 184 additions and 101 deletions

6
.gitignore vendored
View file

@ -1,2 +1,6 @@
.gradle/ .settings/
bin/
target/ target/
.checkstyle
.classpath
.project

View file

@ -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/)

View file

@ -1,2 +0,0 @@
#!/bin/bash
mvn package

30
pom.xml
View file

@ -5,8 +5,10 @@
<version>master</version> <version>master</version>
<properties> <properties>
<maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.source>1.0</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target> <maven.compiler.target>1.0</maven.compiler.target>
<maven.test.skip>true</maven.test.skip>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies> <dependencies>
@ -29,13 +31,23 @@
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-checkstyle-plugin</artifactId>
<version>3.8.0</version> <version>3.1.0</version>
<configuration> <executions>
<compilerArgs> <execution>
<arg>-Xlint:all</arg> <id>checkstyle</id>
</compilerArgs> <phase>validate</phase>
</configuration> <goals>
<goal>check</goal>
</goals>
<configuration>
<suppressionsLocation>
suppressions.xml
</suppressionsLocation>
<failOnViolation>true</failOnViolation>
</configuration>
</execution>
</executions>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>

View file

@ -1,15 +1,13 @@
package pw.kaboom.icontrolu; package pw.kaboom.icontrolu;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Player; import org.bukkit.attribute.AttributeInstance;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerDropItemEvent; 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.PlayerMoveEvent;
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.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team; 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 { class Tick extends BukkitRunnable {
@Override
public void run() { public void run() {
for (Player target: Bukkit.getOnlinePlayers()) { for (Player target: Bukkit.getOnlinePlayers()) {
final Player controller = Main.controllerFor.get(target.getUniqueId()); final Player controller = PlayerList.getController(target.getUniqueId());
if (controller != null) { if (controller != null) {
for (int i = 0; i < controller.getInventory().getSize(); i++) { for (int i = 0; i < controller.getInventory().getSize(); i++) {
@ -52,11 +45,14 @@ class Tick extends BukkitRunnable {
target.teleportAsync(controller.getLocation()); 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.setAllowFlight(controller.getAllowFlight());
target.setExhaustion(controller.getExhaustion()); target.setExhaustion(controller.getExhaustion());
target.setFlying(controller.isFlying()); target.setFlying(controller.isFlying());
target.setFoodLevel(controller.getFoodLevel()); target.setFoodLevel(controller.getFoodLevel());
target.setMaxHealth(controller.getMaxHealth());
target.setHealth(controller.getHealth()); target.setHealth(controller.getHealth());
target.setLevel(controller.getLevel()); target.setLevel(controller.getLevel());
target.setSneaking(controller.isSneaking()); target.setSneaking(controller.isSneaking());
@ -73,20 +69,25 @@ class Tick extends BukkitRunnable {
team = scoreboard.registerNewTeam("icuDisableCollision"); team = scoreboard.registerNewTeam("icuDisableCollision");
} }
team.setCanSeeFriendlyInvisibles(false); team.setCanSeeFriendlyInvisibles(false);
team.setOption(Team.Option.COLLISION_RULE, Team.OptionStatus.NEVER); team.setOption(Team.Option.COLLISION_RULE, Team.OptionStatus.NEVER);
if (!team.hasEntry(controller.getName())) { if (!team.hasEntry(controller.getName())) {
team.addEntry(controller.getName()); team.addEntry(controller.getName());
} }
final int duration = 99999;
final int amplifier = 0;
final boolean ambient = false;
final boolean particles = false;
controller.addPotionEffect( controller.addPotionEffect(
new PotionEffect( new PotionEffect(
PotionEffectType.INVISIBILITY, PotionEffectType.INVISIBILITY,
99999, duration,
0, amplifier,
false, ambient,
false particles
) )
); );
} }
@ -94,17 +95,17 @@ class Tick extends BukkitRunnable {
} }
} }
class Events implements Listener { class ControlPlayer implements Listener {
@EventHandler @EventHandler
void onAsyncPlayerChat(AsyncPlayerChatEvent event) { private void onAsyncPlayerChat(final AsyncPlayerChatEvent event) {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
if (Main.controllerFor.containsKey(player.getUniqueId())) { if (PlayerList.getController(player.getUniqueId()) != null) {
event.setCancelled(true); event.setCancelled(true);
} }
if (Main.targetFor.containsKey(player.getUniqueId())) { if (PlayerList.getTarget(player.getUniqueId()) != null) {
final Player target = Main.targetFor.get(player.getUniqueId()); final Player target = PlayerList.getTarget(player.getUniqueId());
target.chat(event.getMessage()); target.chat(event.getMessage());
event.setCancelled(true); event.setCancelled(true);
@ -112,64 +113,66 @@ class Events implements Listener {
} }
@EventHandler @EventHandler
void onEntityDamage(EntityDamageEvent event) { private void onEntityDamage(final EntityDamageEvent event) {
final Entity player = event.getEntity(); final Entity player = event.getEntity();
if (Main.targetFor.containsKey(player.getUniqueId())) { if (PlayerList.getTarget(player.getUniqueId()) != null) {
event.setCancelled(true); event.setCancelled(true);
} }
} }
@EventHandler @EventHandler
void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { private void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event) {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
if (Main.controllerFor.containsKey(player.getUniqueId())) { if (PlayerList.getController(player.getUniqueId()) != null) {
event.setCancelled(true); event.setCancelled(true);
} }
} }
@EventHandler @EventHandler
void onPlayerDropItem(PlayerDropItemEvent event) { private void onPlayerDropItem(final PlayerDropItemEvent event) {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
if (Main.controllerFor.containsKey(player.getUniqueId())) { if (PlayerList.getController(player.getUniqueId()) != null) {
event.setCancelled(true); event.setCancelled(true);
} }
} }
@EventHandler @EventHandler
void onPlayerInteract(PlayerInteractEvent event) { private void onPlayerInteract(final PlayerInteractEvent event) {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
if (Main.controllerFor.containsKey(player.getUniqueId())) { if (PlayerList.getController(player.getUniqueId()) != null) {
event.setCancelled(true); event.setCancelled(true);
} }
} }
@EventHandler @EventHandler
void onPlayerMove(PlayerMoveEvent event) { private void onPlayerMove(final PlayerMoveEvent event) {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
if (Main.controllerFor.containsKey(player.getUniqueId())) { if (PlayerList.getController(player.getUniqueId()) != null) {
event.setCancelled(true); event.setCancelled(true);
} }
} }
@EventHandler @EventHandler
void onPlayerQuit(PlayerQuitEvent event) { private void onPlayerQuit(final PlayerQuitEvent event) {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
for (Player otherPlayer: Bukkit.getOnlinePlayers()) { for (Player otherPlayer: Bukkit.getOnlinePlayers()) {
/* Target disconnects */ /* Target disconnects */
if (Main.controllerFor.containsKey(player.getUniqueId()) && if (PlayerList.getController(player.getUniqueId()) != null
Main.controllerFor.get(player.getUniqueId()).equals(otherPlayer)) { && PlayerList.getController(player.getUniqueId()).equals(otherPlayer)) {
Main.targetFor.remove(otherPlayer.getUniqueId()); PlayerList.removeTarget(otherPlayer.getUniqueId());
Main.controllerFor.remove(player.getUniqueId()); PlayerList.removeController(player.getUniqueId());
final Player controller = otherPlayer; final Player controller = otherPlayer;
final int tickDelay = 200;
new BukkitRunnable() { new BukkitRunnable() {
@Override
public void run() { public void run() {
for (Player allPlayers: Bukkit.getOnlinePlayers()) { for (Player allPlayers: Bukkit.getOnlinePlayers()) {
allPlayers.showPlayer(JavaPlugin.getPlugin(Main.class), controller); allPlayers.showPlayer(JavaPlugin.getPlugin(Main.class), controller);
@ -178,34 +181,34 @@ class Events implements Listener {
final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard(); final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
final Team team = scoreboard.getTeam("icuDisableCollision"); final Team team = scoreboard.getTeam("icuDisableCollision");
if (team != null && if (team != null
team.hasEntry(controller.getName()) == true) { && team.hasEntry(controller.getName())) {
team.removeEntry(controller.getName()); team.removeEntry(controller.getName());
} }
controller.removePotionEffect(PotionEffectType.INVISIBILITY); controller.removePotionEffect(PotionEffectType.INVISIBILITY);
controller.sendMessage("You are now visible"); 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."); otherPlayer.sendMessage("The player you were controlling has disconnected. You are invisible for 10 seconds.");
} }
/* Controller disconnects */ /* Controller disconnects */
if (Main.targetFor.containsKey(player.getUniqueId()) && if (PlayerList.getTarget(player.getUniqueId()) != null
Main.targetFor.get(player.getUniqueId()).equals(otherPlayer)) { && PlayerList.getTarget(player.getUniqueId()).equals(otherPlayer)) {
Main.targetFor.remove(player.getUniqueId()); PlayerList.removeTarget(player.getUniqueId());
Main.controllerFor.remove(otherPlayer.getUniqueId()); PlayerList.removeController(otherPlayer.getUniqueId());
} }
} }
} }
@EventHandler @EventHandler
void onPlayerRespawn(PlayerRespawnEvent event) { private void onPlayerRespawn(final PlayerRespawnEvent event) {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
if (Main.controllerFor.containsKey(player.getUniqueId())) { if (PlayerList.getController(player.getUniqueId()) != null) {
final Player controller = Main.controllerFor.get(player.getUniqueId()); final Player controller = PlayerList.getController(player.getUniqueId());
controller.teleportAsync(player.getLocation()); controller.teleportAsync(player.getLocation());
} }

View file

@ -1,23 +1,17 @@
package pw.kaboom.icontrolu; package pw.kaboom.icontrolu;
import java.util.HashMap;
import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
public class Main extends JavaPlugin { import pw.kaboom.icontrolu.commands.CommandIcu;
static HashMap<UUID, Player> controllerFor = new HashMap<>(); import pw.kaboom.icontrolu.utilities.PlayerList;
static HashMap<UUID, Player> targetFor = new HashMap<>();
public final class Main extends JavaPlugin {
@Override
public void onEnable() { public void onEnable() {
/* Setup scoreboard team to prevent player collisions */ /* Setup scoreboard team to prevent player collisions */
final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard(); final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
@ -30,12 +24,13 @@ public class Main extends JavaPlugin {
this.getCommand("icu").setExecutor(new CommandIcu()); this.getCommand("icu").setExecutor(new CommandIcu());
new Tick().runTaskTimer(this, 0, 1); new Tick().runTaskTimer(this, 0, 1);
this.getServer().getPluginManager().registerEvents(new Events(), this); this.getServer().getPluginManager().registerEvents(new ControlPlayer(), this);
} }
@Override
public void onDisable() { public void onDisable() {
for (Player controller: Bukkit.getOnlinePlayers()) { for (Player controller: Bukkit.getOnlinePlayers()) {
final Player target = Main.targetFor.get(controller.getUniqueId()); final Player target = PlayerList.getTarget(controller.getUniqueId());
if (target != null) { if (target != null) {
for (Player player: Bukkit.getOnlinePlayers()) { for (Player player: Bukkit.getOnlinePlayers()) {

View file

@ -1,26 +1,24 @@
package pw.kaboom.icontrolu; package pw.kaboom.icontrolu.commands;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
class CommandIcu implements CommandExecutor { import pw.kaboom.icontrolu.Main;
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { 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) { if (sender instanceof ConsoleCommandSender) {
sender.sendMessage("Command has to be run by a player"); sender.sendMessage("Command has to be run by a player");
} else { } else {
@ -37,11 +35,11 @@ class CommandIcu implements CommandExecutor {
if (target != null) { if (target != null) {
if (target == controller) { if (target == controller) {
controller.sendMessage("You are already controlling yourself"); 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() + "\""); 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"); 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"); controller.sendMessage("Player \"" + target.getName() + "\" is already controlling another player");
} else if (!controller.canSee(target)) { } else if (!controller.canSee(target)) {
controller.sendMessage("You may not control this player"); controller.sendMessage("You may not control this player");
@ -50,8 +48,8 @@ class CommandIcu implements CommandExecutor {
controller.getInventory().setContents(target.getInventory().getContents()); controller.getInventory().setContents(target.getInventory().getContents());
Main.targetFor.put(controller.getUniqueId(), target); PlayerList.setTarget(controller.getUniqueId(), target);
Main.controllerFor.put(target.getUniqueId(), controller); PlayerList.setController(target.getUniqueId(), controller);
controller.sendMessage("You are now controlling \"" + target.getName() + "\""); controller.sendMessage("You are now controlling \"" + target.getName() + "\"");
} }
@ -60,13 +58,16 @@ class CommandIcu implements CommandExecutor {
} }
} }
} else if (args[0].equalsIgnoreCase("stop")) { } else if (args[0].equalsIgnoreCase("stop")) {
final Player target = Main.targetFor.get(controller.getUniqueId()); final Player target = PlayerList.getTarget(controller.getUniqueId());
if (target != null) { if (target != null) {
Main.targetFor.remove(controller.getUniqueId()); PlayerList.removeTarget(controller.getUniqueId());
Main.controllerFor.remove(target.getUniqueId()); PlayerList.removeController(target.getUniqueId());
final int tickDelay = 200;
new BukkitRunnable() { new BukkitRunnable() {
@Override
public void run() { public void run() {
for (Player player: Bukkit.getOnlinePlayers()) { for (Player player: Bukkit.getOnlinePlayers()) {
player.showPlayer(JavaPlugin.getPlugin(Main.class), controller); player.showPlayer(JavaPlugin.getPlugin(Main.class), controller);
@ -81,7 +82,7 @@ class CommandIcu implements CommandExecutor {
controller.removePotionEffect(PotionEffectType.INVISIBILITY); controller.removePotionEffect(PotionEffectType.INVISIBILITY);
controller.sendMessage("You are now visible"); 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."); controller.sendMessage("You are no longer controlling \"" + target.getName() + "\". You are invisible for 10 seconds.");
} else { } else {

View file

@ -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<UUID, Player> controllerFor = new HashMap<UUID, Player>();
private static HashMap<UUID, Player> targetFor = new HashMap<UUID, Player>();
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);
}
}

13
suppressions.xml Normal file
View file

@ -0,0 +1,13 @@
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Puppy Crawl//DTD Suppressions 1.0//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_0.dtd">
<suppressions>
<suppress checks="AvoidStarImport" files="."/>
<suppress checks="FileTabCharacter" files="."/>
<suppress checks="Javadoc" files="."/>
<suppress checks="LineLength" files="."/>
<suppress checks="NewlineAtEndOfFile" files="."/>
</suppressions>