Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ target/
.classpath
.project
*.iml
.theia/
run/
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version>
<version>3.3.0</version>
<executions>
<execution>
<id>checkstyle</id>
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/pw/kaboom/icontrolu/Main.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
package pw.kaboom.icontrolu;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;

import net.kyori.adventure.text.Component;

import pw.kaboom.icontrolu.commands.CommandIcu;
import pw.kaboom.icontrolu.PlayerControl;
import pw.kaboom.icontrolu.modules.PlayerControl;

public final class Main extends JavaPlugin {
private final PlayerControl controlModule = new PlayerControl();

@Override
public void onEnable() {
/* Commands */
this.getCommand("icu").setExecutor(new CommandIcu());
this.getCommand("icu").setExecutor(new CommandIcu(controlModule));

/* Modules */
PlayerControl.enable();
this.getServer().getPluginManager().registerEvents(new PlayerControl(), this);
controlModule.enable();
this.getServer().getPluginManager().registerEvents(controlModule, this);
}

@Override
public void onDisable() {
PlayerControl.disable();
controlModule.disable();
}
}
37 changes: 16 additions & 21 deletions src/main/java/pw/kaboom/icontrolu/commands/CommandIcu.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
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.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;

import pw.kaboom.icontrolu.Main;
import pw.kaboom.icontrolu.PlayerControl;
import pw.kaboom.icontrolu.modules.ControlManager;
import pw.kaboom.icontrolu.modules.PlayerControl;

public final class CommandIcu implements CommandExecutor {
private final PlayerControl controlModule;

public CommandIcu(final PlayerControl controlModule) {
this.controlModule = controlModule;
}

private void controlCommand(final Player controller, final String label, final String[] args) {
if (args.length == 1) {
controller.sendMessage(Component
Expand Down Expand Up @@ -47,16 +49,13 @@ private void controlCommand(final Player controller, final String label, final S
return;
}

if (PlayerControl.getTarget(controller.getUniqueId()) != null) {
controller.sendMessage(
Component.text("You are already controlling \"")
.append(Component.text(target.getName()))
.append(Component.text("\""))
);
final ControlManager manager = controlModule.manager;
if (manager.hasController(controller.getUniqueId())) {
controller.sendMessage(Component.text("You are already controlling someone"));
return;
}

if (PlayerControl.getController(target.getUniqueId()) != null) {
if (manager.hasTarget(target.getUniqueId())) {
controller.sendMessage(
Component.text("Player \"")
.append(Component.text(target.getName()))
Expand All @@ -65,7 +64,7 @@ private void controlCommand(final Player controller, final String label, final S
return;
}

if (PlayerControl.getTarget(target.getUniqueId()) != null) {
if (manager.hasController(target.getUniqueId())) {
controller.sendMessage(
Component.text("Player \"")
.append(Component.text(target.getName()))
Expand All @@ -82,9 +81,7 @@ private void controlCommand(final Player controller, final String label, final S
controller.teleportAsync(target.getLocation());
controller.getInventory().setContents(target.getInventory().getContents());

PlayerControl.setTarget(controller.getUniqueId(), target);
PlayerControl.setController(target.getUniqueId(), controller);

manager.control(controller, target);
controller.sendMessage(
Component.text("You are now controlling \"")
.append(Component.text(target.getName()))
Expand All @@ -93,16 +90,14 @@ private void controlCommand(final Player controller, final String label, final S
}

private void stopCommand(final Player controller) {
final Player target = PlayerControl.getTarget(controller.getUniqueId());
final Player target = controlModule.manager.removeController(controller.getUniqueId());

if (target == null) {
controller.sendMessage(Component.text("You are not controlling anyone at the moment"));
return;
}

PlayerControl.removeTarget(controller.getUniqueId());
PlayerControl.removeController(target.getUniqueId());
PlayerControl.scheduleVisibility(controller.getUniqueId());
controlModule.scheduleVisibility(controller.getUniqueId());

controller.sendMessage(
Component.text("You are no longer controlling \"")
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/pw/kaboom/icontrolu/modules/ControlManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package pw.kaboom.icontrolu.modules;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;
import java.util.function.BiConsumer;

public final class ControlManager {
// <controller, target>
private final BiMap<@NotNull UUID, @NotNull UUID> map = HashBiMap.create();

public void control(final Player controller, final Player target) {
map.put(controller.getUniqueId(), target.getUniqueId());
}

public boolean hasController(final UUID controllerUUID) {
return map.containsKey(controllerUUID);
}

public boolean hasTarget(final UUID targetUUID) {
return map.containsValue(targetUUID);
}

public Player getTarget(final UUID controllerUUID) {
final UUID targetUUID = map.get(controllerUUID);
if (targetUUID == null) return null;

final Player target = Bukkit.getPlayer(targetUUID);
if (target == null) map.remove(controllerUUID);
return target;
}

public Player getController(final UUID targetUUID) {
final UUID controllerUUID = map.inverse().get(targetUUID);
if (controllerUUID == null) return null;

final Player controller = Bukkit.getPlayer(controllerUUID);
if (controller == null) map.remove(controllerUUID);
return controller;
}

public void forEach(final BiConsumer<@NotNull Player, @NotNull Player> consumer) {
this.map.entrySet().removeIf(e -> {
final Player controller = Bukkit.getPlayer(e.getKey());
if (controller == null) return true;
final Player target = Bukkit.getPlayer(e.getValue());
if (target == null) return true;

consumer.accept(controller, target);
return false;
});
}

public Player removeController(final UUID controllerUUID) {
final UUID targetUUID = map.remove(controllerUUID);
if (targetUUID == null) return null;

return Bukkit.getPlayer(targetUUID);
}

public Player removeTarget(final UUID targetUUID) {
final UUID controlerUUID = map.inverse().remove(targetUUID);
if (controlerUUID == null) return null;

return Bukkit.getPlayer(controlerUUID);
}
}
Loading