mirror of
https://github.com/kaboomserver/icontrolu.git
synced 2025-04-23 20:33:36 -04:00
Refactor player control module
This commit is contained in:
parent
1e60f14b3d
commit
f3fbd1b633
4 changed files with 110 additions and 131 deletions
src/main/java/pw/kaboom/icontrolu
|
@ -10,48 +10,21 @@ import org.bukkit.scoreboard.Team;
|
|||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import pw.kaboom.icontrolu.commands.CommandIcu;
|
||||
import pw.kaboom.icontrolu.utilities.PlayerList;
|
||||
import pw.kaboom.icontrolu.PlayerControl;
|
||||
|
||||
public final class Main extends JavaPlugin {
|
||||
@Override
|
||||
public void onEnable() {
|
||||
/* Setup scoreboard team to prevent player collisions */
|
||||
final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
|
||||
final Team team = scoreboard.getTeam("icuCollision");
|
||||
if (team != null) {
|
||||
team.unregister();
|
||||
}
|
||||
|
||||
/* Commands */
|
||||
this.getCommand("icu").setExecutor(new CommandIcu());
|
||||
|
||||
this.getServer().getPluginManager().registerEvents(new ControlPlayer(), this);
|
||||
/* Modules */
|
||||
PlayerControl.enable();
|
||||
this.getServer().getPluginManager().registerEvents(new PlayerControl(), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
for (Player controller: Bukkit.getOnlinePlayers()) {
|
||||
final Player target = PlayerList.getTarget(controller.getUniqueId());
|
||||
|
||||
if (target != null) {
|
||||
for (Player player: Bukkit.getOnlinePlayers()) {
|
||||
player.showPlayer(this, controller);
|
||||
}
|
||||
|
||||
final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
|
||||
final Team team = scoreboard.getTeam("icuCollision");
|
||||
|
||||
if (team != null) {
|
||||
team.unregister();
|
||||
}
|
||||
|
||||
controller.removePotionEffect(PotionEffectType.INVISIBILITY);
|
||||
controller.sendMessage(
|
||||
Component.text("You are no longer controlling \"")
|
||||
.append(Component.text(target.getName()))
|
||||
.append(Component.text("\" due to server reload"))
|
||||
);
|
||||
}
|
||||
}
|
||||
PlayerControl.disable();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ import net.kyori.adventure.text.Component;
|
|||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
|
||||
import pw.kaboom.icontrolu.Main;
|
||||
import pw.kaboom.icontrolu.utilities.PlayerList;
|
||||
import pw.kaboom.icontrolu.PlayerControl;
|
||||
|
||||
public final class CommandIcu implements CommandExecutor {
|
||||
private void controlCommand(final Player controller, final String label, final String[] args) {
|
||||
|
@ -34,19 +34,19 @@ public final class CommandIcu implements CommandExecutor {
|
|||
if (target != null) {
|
||||
if (target == controller) {
|
||||
controller.sendMessage(Component.text("You are already controlling yourself"));
|
||||
} else if (PlayerList.getTarget(controller.getUniqueId()) != null) {
|
||||
} else if (PlayerControl.getTarget(controller.getUniqueId()) != null) {
|
||||
controller.sendMessage(
|
||||
Component.text("You are already controlling \"")
|
||||
.append(Component.text(target.getName()))
|
||||
.append(Component.text("\""))
|
||||
);
|
||||
} else if (PlayerList.getController(target.getUniqueId()) != null) {
|
||||
} else if (PlayerControl.getController(target.getUniqueId()) != null) {
|
||||
controller.sendMessage(
|
||||
Component.text("Player \"")
|
||||
.append(Component.text(target.getName()))
|
||||
.append(Component.text("\" is already being controlled"))
|
||||
);
|
||||
} else if (PlayerList.getTarget(target.getUniqueId()) != null) {
|
||||
} else if (PlayerControl.getTarget(target.getUniqueId()) != null) {
|
||||
controller.sendMessage(
|
||||
Component.text("Player \"")
|
||||
.append(Component.text(target.getName()))
|
||||
|
@ -59,8 +59,8 @@ public final class CommandIcu implements CommandExecutor {
|
|||
|
||||
controller.getInventory().setContents(target.getInventory().getContents());
|
||||
|
||||
PlayerList.setTarget(controller.getUniqueId(), target);
|
||||
PlayerList.setController(target.getUniqueId(), controller);
|
||||
PlayerControl.setTarget(controller.getUniqueId(), target);
|
||||
PlayerControl.setController(target.getUniqueId(), controller);
|
||||
|
||||
controller.sendMessage(
|
||||
Component.text("You are now controlling \"")
|
||||
|
@ -79,12 +79,12 @@ public final class CommandIcu implements CommandExecutor {
|
|||
}
|
||||
|
||||
private void stopCommand(final Player controller) {
|
||||
final Player target = PlayerList.getTarget(controller.getUniqueId());
|
||||
final Player target = PlayerControl.getTarget(controller.getUniqueId());
|
||||
|
||||
if (target != null) {
|
||||
PlayerList.removeTarget(controller.getUniqueId());
|
||||
PlayerList.removeController(target.getUniqueId());
|
||||
PlayerList.scheduleVisibility(controller.getUniqueId());
|
||||
PlayerControl.removeTarget(controller.getUniqueId());
|
||||
PlayerControl.removeController(target.getUniqueId());
|
||||
PlayerControl.scheduleVisibility(controller.getUniqueId());
|
||||
|
||||
final int seconds = 10;
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package pw.kaboom.icontrolu;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.UUID;
|
||||
|
||||
|
@ -31,19 +33,85 @@ import com.destroystokyo.paper.event.server.ServerTickStartEvent;
|
|||
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import pw.kaboom.icontrolu.utilities.PlayerList;
|
||||
public final class PlayerControl implements Listener {
|
||||
|
||||
class ControlPlayer implements Listener {
|
||||
private static String CHAT_PREFIX = "\ud800iControlUChat\ud800";
|
||||
private static final String CHAT_PREFIX = "\ud800iControlUChat\ud800";
|
||||
private static final int VISIBILITY_DELAY_MS = 10000;
|
||||
|
||||
private static Map<UUID, Player> controllers = new HashMap<>();
|
||||
private static Map<UUID, Player> targets = new HashMap<>();
|
||||
private static Map<UUID, Long> scheduledVisibilities = new HashMap<>();
|
||||
|
||||
public static void enable() {
|
||||
/* Setup scoreboard team to prevent player collisions */
|
||||
final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
|
||||
final Team team = scoreboard.getTeam("icuCollision");
|
||||
if (team != null) {
|
||||
team.unregister();
|
||||
}
|
||||
}
|
||||
|
||||
public static void disable() {
|
||||
for (UUID controllerUUID : targets.keySet()) {
|
||||
final Player controller = Bukkit.getPlayer(controllerUUID);
|
||||
final Player target = getTarget(controller.getUniqueId());
|
||||
|
||||
for (Player player: Bukkit.getOnlinePlayers()) {
|
||||
player.showPlayer(JavaPlugin.getPlugin(Main.class), controller);
|
||||
}
|
||||
|
||||
final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
|
||||
final Team team = scoreboard.getTeam("icuCollision");
|
||||
|
||||
if (team != null) {
|
||||
team.unregister();
|
||||
}
|
||||
|
||||
controller.removePotionEffect(PotionEffectType.INVISIBILITY);
|
||||
controller.sendMessage(
|
||||
Component.text("You are no longer controlling \"")
|
||||
.append(Component.text(target.getName()))
|
||||
.append(Component.text("\" due to server reload"))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static Player getController(final UUID playerUUID) {
|
||||
return controllers.get(playerUUID);
|
||||
}
|
||||
|
||||
public static Player getTarget(final UUID playerUUID) {
|
||||
return targets.get(playerUUID);
|
||||
}
|
||||
|
||||
public static void removeController(final UUID playerUUID) {
|
||||
controllers.remove(playerUUID);
|
||||
}
|
||||
|
||||
public static void removeTarget(final UUID playerUUID) {
|
||||
targets.remove(playerUUID);
|
||||
}
|
||||
|
||||
public static void setController(final UUID playerUUID, final Player player) {
|
||||
controllers.put(playerUUID, player);
|
||||
}
|
||||
|
||||
public static void setTarget(final UUID playerUUID, final Player player) {
|
||||
targets.put(playerUUID, player);
|
||||
}
|
||||
|
||||
public static void scheduleVisibility(final UUID playerUUID) {
|
||||
scheduledVisibilities.put(playerUUID, System.currentTimeMillis() + VISIBILITY_DELAY_MS);
|
||||
}
|
||||
|
||||
private void controlPlayers() {
|
||||
if (!PlayerList.hasControllers()) {
|
||||
if (targets.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (UUID controllerUUID : PlayerList.getControllers()) {
|
||||
for (UUID controllerUUID : targets.keySet()) {
|
||||
final Player controller = Bukkit.getPlayer(controllerUUID);
|
||||
final Player target = PlayerList.getTarget(controllerUUID);
|
||||
final Player target = getTarget(controllerUUID);
|
||||
|
||||
for (int i = 0; i < controller.getInventory().getSize(); i++) {
|
||||
if (controller.getInventory().getItem(i) != null) {
|
||||
|
@ -110,12 +178,11 @@ class ControlPlayer implements Listener {
|
|||
}
|
||||
|
||||
private void checkVisibility() {
|
||||
if (!PlayerList.hasScheduledVisibilities()) {
|
||||
if (scheduledVisibilities.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Iterator<Entry<UUID, Long>> iterator = PlayerList.getScheduledVisibilities().entrySet()
|
||||
.iterator();
|
||||
Iterator<Entry<UUID, Long>> iterator = scheduledVisibilities.entrySet().iterator();
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
final Entry<UUID, Long> entry = iterator.next();
|
||||
|
@ -149,7 +216,7 @@ class ControlPlayer implements Listener {
|
|||
private void onEntityDamage(final EntityDamageEvent event) {
|
||||
final Entity player = event.getEntity();
|
||||
|
||||
if (PlayerList.getTarget(player.getUniqueId()) != null) {
|
||||
if (getTarget(player.getUniqueId()) != null) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
@ -158,7 +225,7 @@ class ControlPlayer implements Listener {
|
|||
private void onPlayerAnimation(final PlayerAnimationEvent event) {
|
||||
final Player player = event.getPlayer();
|
||||
|
||||
if (PlayerList.getController(player.getUniqueId()) != null) {
|
||||
if (getController(player.getUniqueId()) != null) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
@ -168,7 +235,7 @@ class ControlPlayer implements Listener {
|
|||
final Player player = event.getPlayer();
|
||||
final UUID playerUUID = player.getUniqueId();
|
||||
|
||||
if (PlayerList.getController(playerUUID) != null) {
|
||||
if (getController(playerUUID) != null) {
|
||||
if (event.getMessage().startsWith(CHAT_PREFIX)) {
|
||||
event.setMessage(event.getMessage().substring(CHAT_PREFIX.length()));
|
||||
return;
|
||||
|
@ -177,7 +244,7 @@ class ControlPlayer implements Listener {
|
|||
return;
|
||||
}
|
||||
|
||||
final Player target = PlayerList.getTarget(playerUUID);
|
||||
final Player target = getTarget(playerUUID);
|
||||
|
||||
if (target != null) {
|
||||
target.chat(CHAT_PREFIX + event.getMessage());
|
||||
|
@ -189,7 +256,7 @@ class ControlPlayer implements Listener {
|
|||
private void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event) {
|
||||
final Player player = event.getPlayer();
|
||||
|
||||
if (PlayerList.getController(player.getUniqueId()) != null) {
|
||||
if (getController(player.getUniqueId()) != null) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
@ -198,7 +265,7 @@ class ControlPlayer implements Listener {
|
|||
private void onPlayerDropItem(final PlayerDropItemEvent event) {
|
||||
final Player player = event.getPlayer();
|
||||
|
||||
if (PlayerList.getController(player.getUniqueId()) != null) {
|
||||
if (getController(player.getUniqueId()) != null) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
@ -207,13 +274,13 @@ class ControlPlayer implements Listener {
|
|||
private void onPlayerInteract(final PlayerInteractEvent event) {
|
||||
final Player player = event.getPlayer();
|
||||
|
||||
if (PlayerList.getController(player.getUniqueId()) != null) {
|
||||
if (getController(player.getUniqueId()) != null) {
|
||||
event.setCancelled(true);
|
||||
|
||||
} else if ((event.getAction() == Action.LEFT_CLICK_AIR
|
||||
|| event.getAction() == Action.LEFT_CLICK_BLOCK)
|
||||
&& PlayerList.getTarget(player.getUniqueId()) != null) {
|
||||
final Player target = PlayerList.getTarget(player.getUniqueId());
|
||||
&& getTarget(player.getUniqueId()) != null) {
|
||||
final Player target = getTarget(player.getUniqueId());
|
||||
|
||||
if (event.getHand() == EquipmentSlot.HAND) {
|
||||
target.swingMainHand();
|
||||
|
@ -227,7 +294,7 @@ class ControlPlayer implements Listener {
|
|||
private void onPlayerMove(final PlayerMoveEvent event) {
|
||||
final Player player = event.getPlayer();
|
||||
|
||||
if (PlayerList.getController(player.getUniqueId()) != null) {
|
||||
if (getController(player.getUniqueId()) != null) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
@ -236,39 +303,38 @@ class ControlPlayer implements Listener {
|
|||
private void onPlayerQuit(final PlayerQuitEvent event) {
|
||||
final Player player = event.getPlayer();
|
||||
final UUID playerUUID = player.getUniqueId();
|
||||
final Player controller = PlayerList.getController(playerUUID);
|
||||
final Player controller = getController(playerUUID);
|
||||
|
||||
if (controller != null) {
|
||||
/*
|
||||
Target disconnects
|
||||
*/
|
||||
PlayerList.removeTarget(controller.getUniqueId());
|
||||
PlayerList.removeController(playerUUID);
|
||||
PlayerList.scheduleVisibility(controller.getUniqueId());
|
||||
removeTarget(controller.getUniqueId());
|
||||
removeController(playerUUID);
|
||||
scheduleVisibility(controller.getUniqueId());
|
||||
|
||||
controller.sendMessage(Component.text("The player you were controlling has "
|
||||
+ "disconnected. You are invisible for 10 seconds."));
|
||||
return;
|
||||
}
|
||||
|
||||
final Player target = PlayerList.getTarget(playerUUID);
|
||||
final Player target = getTarget(playerUUID);
|
||||
|
||||
if (target != null) {
|
||||
/*
|
||||
Controller disconnects
|
||||
*/
|
||||
PlayerList.removeTarget(playerUUID);
|
||||
PlayerList.removeController(target.getUniqueId());
|
||||
removeTarget(playerUUID);
|
||||
removeController(target.getUniqueId());
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
private void onPlayerRespawn(final PlayerRespawnEvent event) {
|
||||
final Player player = event.getPlayer();
|
||||
final Player controller = getController(player.getUniqueId());
|
||||
|
||||
if (PlayerList.getController(player.getUniqueId()) != null) {
|
||||
final Player controller = PlayerList.getController(player.getUniqueId());
|
||||
|
||||
if (controller != null) {
|
||||
controller.teleportAsync(player.getLocation());
|
||||
}
|
||||
}
|
|
@ -1,60 +0,0 @@
|
|||
package pw.kaboom.icontrolu.utilities;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public final class PlayerList {
|
||||
|
||||
private static final int VISIBILITY_DELAY_MS = 10000;
|
||||
private static Map<UUID, Player> controllerFor = new HashMap<>();
|
||||
private static Map<UUID, Player> targetFor = new HashMap<>();
|
||||
private static Map<UUID, Long> scheduledVisibilities = new HashMap<>();
|
||||
|
||||
public static Player getController(final UUID playerUUID) {
|
||||
return controllerFor.get(playerUUID);
|
||||
}
|
||||
|
||||
public static Set<UUID> getControllers() {
|
||||
return targetFor.keySet();
|
||||
}
|
||||
|
||||
public static Player getTarget(final UUID playerUUID) {
|
||||
return targetFor.get(playerUUID);
|
||||
}
|
||||
|
||||
public static Map getScheduledVisibilities() {
|
||||
return scheduledVisibilities;
|
||||
}
|
||||
|
||||
public static boolean hasControllers() {
|
||||
return !targetFor.isEmpty();
|
||||
}
|
||||
|
||||
public static boolean hasScheduledVisibilities() {
|
||||
return !scheduledVisibilities.isEmpty();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public static void scheduleVisibility(final UUID playerUUID) {
|
||||
scheduledVisibilities.put(playerUUID, System.currentTimeMillis() + VISIBILITY_DELAY_MS);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue