127 lines
5.0 KiB
Java
127 lines
5.0 KiB
Java
package games.dmg.spigottyrant;
|
|
|
|
import java.util.Locale;
|
|
import org.bukkit.ChatColor;
|
|
import org.bukkit.command.Command;
|
|
import org.bukkit.command.CommandExecutor;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.entity.Player;
|
|
|
|
public final class VigilanteCommand implements CommandExecutor {
|
|
private final TyrantStateManager stateManager;
|
|
private final FollowerService followers;
|
|
private final OnlinePlayerDirectory onlinePlayers;
|
|
private final VigilanteControlPanel controlPanel;
|
|
private final RoleControlItemService roleControlItems;
|
|
|
|
public VigilanteCommand(
|
|
TyrantStateManager stateManager,
|
|
FollowerService followers,
|
|
OnlinePlayerDirectory onlinePlayers
|
|
) {
|
|
this(
|
|
stateManager, followers, onlinePlayers,
|
|
player -> player.sendMessage(ChatColor.RED
|
|
+ "The Vigilante control panel is unavailable."),
|
|
null
|
|
);
|
|
}
|
|
|
|
public VigilanteCommand(
|
|
TyrantStateManager stateManager,
|
|
FollowerService followers,
|
|
OnlinePlayerDirectory onlinePlayers,
|
|
VigilanteControlPanel controlPanel
|
|
) {
|
|
this(stateManager, followers, onlinePlayers, controlPanel, null);
|
|
}
|
|
|
|
public VigilanteCommand(
|
|
TyrantStateManager stateManager,
|
|
FollowerService followers,
|
|
OnlinePlayerDirectory onlinePlayers,
|
|
VigilanteControlPanel controlPanel,
|
|
RoleControlItemService roleControlItems
|
|
) {
|
|
this.stateManager = stateManager;
|
|
this.followers = followers;
|
|
this.onlinePlayers = onlinePlayers;
|
|
this.controlPanel = controlPanel;
|
|
this.roleControlItems = roleControlItems;
|
|
}
|
|
|
|
@Override
|
|
public boolean onCommand(
|
|
CommandSender sender,
|
|
Command command,
|
|
String label,
|
|
String[] arguments
|
|
) {
|
|
if (!(sender instanceof Player player)) {
|
|
sender.sendMessage("This command must be used by a player.");
|
|
return true;
|
|
}
|
|
if (arguments.length == 0
|
|
|| arguments.length == 1 && arguments[0].equalsIgnoreCase("menu")) {
|
|
controlPanel.open(player);
|
|
return true;
|
|
}
|
|
if (arguments.length == 1 && arguments[0].equalsIgnoreCase("item")) {
|
|
if (roleControlItems == null) {
|
|
player.sendMessage(ChatColor.RED + "Role item recovery is unavailable.");
|
|
} else {
|
|
roleControlItems.recover(player, stateManager.game());
|
|
player.sendMessage(ChatColor.YELLOW
|
|
+ "Recovered the missing Vigilante control item if it could fit.");
|
|
}
|
|
return true;
|
|
}
|
|
FollowerResult result;
|
|
Player target = null;
|
|
if (arguments.length == 1 && arguments[0].equalsIgnoreCase("accept")) {
|
|
result = followers.accept(stateManager.game(), stateManager.players(),
|
|
player.getUniqueId());
|
|
} else if (arguments.length == 1 && arguments[0].equalsIgnoreCase("leave")) {
|
|
result = followers.leave(stateManager.game(), stateManager.players(),
|
|
player.getUniqueId());
|
|
} else if (arguments.length == 2
|
|
&& (arguments[0].equalsIgnoreCase("invite")
|
|
|| arguments[0].equalsIgnoreCase("dismiss"))) {
|
|
target = onlinePlayers.findByName(arguments[1]);
|
|
if (target == null) {
|
|
player.sendMessage(ChatColor.RED + "That player must be online.");
|
|
return true;
|
|
}
|
|
stateManager.player(target.getUniqueId(), target.getName());
|
|
result = arguments[0].equalsIgnoreCase("invite")
|
|
? followers.invite(
|
|
stateManager.game(), stateManager.players(), player.getUniqueId(),
|
|
target.getUniqueId()
|
|
)
|
|
: followers.dismiss(
|
|
stateManager.game(), stateManager.players(), player.getUniqueId(),
|
|
target.getUniqueId()
|
|
);
|
|
} else {
|
|
player.sendMessage(ChatColor.YELLOW
|
|
+ "Usage: /vigilante <menu|item|invite <player>|accept|dismiss <player>|leave>");
|
|
return true;
|
|
}
|
|
if (result.status() == FollowerStatus.JOINED
|
|
|| result.status() == FollowerStatus.DISMISSED
|
|
|| result.status() == FollowerStatus.LEFT) {
|
|
stateManager.replacePlayers(result.players());
|
|
stateManager.saveIfDirty();
|
|
}
|
|
player.sendMessage(ChatColor.YELLOW + "Follower action: "
|
|
+ result.status().name().toLowerCase(Locale.ROOT).replace('_', ' ') + ".");
|
|
if (target != null && result.status() == FollowerStatus.INVITED) {
|
|
target.sendMessage(ChatColor.YELLOW + player.getName()
|
|
+ " invited you to become a Follower. Use /vigilante accept.");
|
|
} else if (target != null && result.status() == FollowerStatus.DISMISSED) {
|
|
target.sendMessage(ChatColor.YELLOW + "You are no longer a Follower.");
|
|
}
|
|
return true;
|
|
}
|
|
}
|