Skip to content

Commit

Permalink
Update PGM switch to cloud command framework
Browse files Browse the repository at this point in the history
Signed-off-by: Pugzy <[email protected]>
  • Loading branch information
Pugzy authored and Pablete1234 committed Jan 31, 2023
1 parent cc3e489 commit 976ba89
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 168 deletions.
12 changes: 5 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<id>ashcon-repo</id>
<url>https://repo.ashcon.app/content/repositories/snapshots</url>
</repository>
<repository> <!-- Working repo for PGM and Sportpaper -->
<id>pgm.fyi</id>
<url>https://repo.pgm.fyi/snapshots</url>
</repository>
</repositories>
<dependencies>
<dependency>
Expand All @@ -23,13 +27,7 @@
<dependency>
<groupId>tc.oc.pgm</groupId>
<artifactId>core</artifactId>
<version>0.14-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>tc.oc.pgm</groupId>
<artifactId>util</artifactId>
<version>0.14-SNAPSHOT</version>
<version>0.15-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
71 changes: 10 additions & 61 deletions src/main/java/dev/pgm/events/EventsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import dev.pgm.events.api.teams.ConfigTeams;
import dev.pgm.events.api.teams.DefaultTeamRegistry;
import dev.pgm.events.api.teams.TournamentTeamRegistry;
import dev.pgm.events.commands.TournamentAdminCommands;
import dev.pgm.events.commands.TournamentUserCommands;
import dev.pgm.events.commands.VetoCommands;
import dev.pgm.events.commands.providers.TournamentProvider;
import dev.pgm.events.format.TournamentFormat;
import dev.pgm.events.commands.EventsCommandGraph;
import dev.pgm.events.listeners.MatchLoadListener;
import dev.pgm.events.listeners.PlayerJoinListen;
import dev.pgm.events.ready.ReadyCommands;
Expand All @@ -18,17 +14,9 @@
import dev.pgm.events.ready.ReadySystem;
import dev.pgm.events.team.DefaultTeamManager;
import dev.pgm.events.team.TournamentTeamManager;
import java.util.Collections;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import tc.oc.pgm.api.PGM;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.command.graph.CommandExecutor;
import tc.oc.pgm.command.graph.MatchPlayerProvider;
import tc.oc.pgm.command.graph.MatchProvider;
import tc.oc.pgm.lib.app.ashcon.intake.bukkit.graph.BasicBukkitCommandGraph;
import tc.oc.pgm.lib.app.ashcon.intake.fluent.DispatcherNode;
import tc.oc.pgm.lib.app.ashcon.intake.parametric.AbstractModule;

public class EventsPlugin extends JavaPlugin {

Expand All @@ -45,28 +33,24 @@ public void onEnable() {

teamManager = DefaultTeamManager.manager();
tournamentManager = new TournamentManager();
TournamentTeamRegistry teamRegistry = DefaultTeamRegistry.createRegistry(new ConfigTeams());
teamRegistry = DefaultTeamRegistry.createRegistry(new ConfigTeams());

ReadyManager readyManager = new ReadyManagerImpl(new ReadySystem(), new ReadyParties());
ReadyListener readyListener = new ReadyListener(readyManager);
ReadyCommands readyCommands = new ReadyCommands(readyManager);

BasicBukkitCommandGraph graph =
new BasicBukkitCommandGraph(
new CommandModule(tournamentManager, teamManager, teamRegistry));

DispatcherNode node = graph.getRootDispatcherNode();
node.registerCommands(new VetoCommands());
node.registerCommands(readyCommands);
EventsCommandGraph eventsCommandGraph;
try {
eventsCommandGraph = new EventsCommandGraph(this);
} catch (Exception e) {
throw new RuntimeException(e);
}

DispatcherNode subNode = node.registerNode("tourney", "tournament", "tm", "events");
subNode.registerCommands(new TournamentUserCommands());
subNode.registerCommands(new TournamentAdminCommands());
eventsCommandGraph.registerCommands(Collections.singletonList(readyCommands));

Bukkit.getPluginManager().registerEvents(new MatchLoadListener(teamManager), this);
Bukkit.getPluginManager().registerEvents(new PlayerJoinListen(teamManager), this);
Bukkit.getPluginManager().registerEvents(readyListener, this);
new CommandExecutor(this, graph).register();
}

@Override
Expand All @@ -93,39 +77,4 @@ public void setTeamRegistry(TournamentTeamRegistry teamRegistry) {
public static EventsPlugin get() {
return plugin;
}

private static class CommandModule extends AbstractModule {

private final TournamentManager tournamentManager;
private final TournamentTeamManager teamManager;
private final TournamentTeamRegistry teamRegistry;

public CommandModule(
TournamentManager tournamentManager,
TournamentTeamManager teamManager,
TournamentTeamRegistry teamRegistry) {
this.tournamentManager = tournamentManager;
this.teamManager = teamManager;
this.teamRegistry = teamRegistry;
}

@Override
protected void configure() {
configureInstances();
configureProviders();
}

private void configureInstances() {
bind(PGM.class).toInstance(PGM.get());
}

private void configureProviders() {
bind(MatchPlayer.class).toProvider(new MatchPlayerProvider());
bind(Match.class).toProvider(new MatchProvider());
bind(TournamentManager.class).toInstance(tournamentManager);
bind(TournamentTeamRegistry.class).toInstance(teamRegistry);
bind(TournamentTeamManager.class).toInstance(teamManager);
bind(TournamentFormat.class).toProvider(new TournamentProvider(tournamentManager));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public TournamentTeam findExact(String name) {
@Override
public TournamentTeam getTeam(String name) {
TournamentTeam found = findExact(name);
return found != null ? found : StringUtils.bestFuzzyMatch(name, teamMap, 0.9);
return found != null ? found : StringUtils.bestFuzzyMatch(name, teamMap);
}

@Override
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/dev/pgm/events/commands/CommandException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dev.pgm.events.commands;

import static net.kyori.adventure.text.Component.text;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.util.ComponentMessageThrowable;
import org.jetbrains.annotations.Nullable;

public class CommandException extends RuntimeException implements ComponentMessageThrowable {

private Component component;

public CommandException(String message) {
super(message);
}

public CommandException(Component component) {
this.component = component;
}

@Override
public @Nullable Component componentMessage() {
if (component != null) return component;
return text(this.getMessage());
}
}
69 changes: 69 additions & 0 deletions src/main/java/dev/pgm/events/commands/EventsCommandGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dev.pgm.events.commands;

import dev.pgm.events.EventsPlugin;
import dev.pgm.events.TournamentManager;
import dev.pgm.events.api.teams.TournamentTeamRegistry;
import dev.pgm.events.commands.providers.TournamentProvider;
import dev.pgm.events.format.TournamentFormat;
import dev.pgm.events.team.TournamentTeamManager;
import java.util.List;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import tc.oc.pgm.api.PGM;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.command.injectors.MatchPlayerProvider;
import tc.oc.pgm.command.injectors.MatchProvider;
import tc.oc.pgm.command.injectors.PlayerProvider;
import tc.oc.pgm.command.util.CommandGraph;
import tc.oc.pgm.lib.cloud.commandframework.extra.confirmation.CommandConfirmationManager;
import tc.oc.pgm.lib.cloud.commandframework.minecraft.extras.MinecraftHelp;
import tc.oc.pgm.util.Audience;

public class EventsCommandGraph extends CommandGraph<EventsPlugin> {

public EventsCommandGraph(EventsPlugin plugin) throws Exception {
super(plugin);
}

@Override
protected MinecraftHelp<CommandSender> createHelp() {
return new MinecraftHelp<>("/events help", Audience::get, manager);
}

@Override
protected CommandConfirmationManager<CommandSender> createConfirmationManager() {
return null;
}

@Override
protected void setupInjectors() {
// PGM Injectors
registerInjector(PGM.class, PGM::get);
registerInjector(Match.class, new MatchProvider());
registerInjector(MatchPlayer.class, new MatchPlayerProvider());
registerInjector(Player.class, new PlayerProvider());

// Events Injectors
registerInjector(TournamentManager.class, EventsPlugin::getTournamentManager);
registerInjector(TournamentTeamRegistry.class, EventsPlugin::getTeamRegistry);
registerInjector(TournamentTeamManager.class, EventsPlugin::getTeamManager);
registerInjector(TournamentFormat.class, new TournamentProvider(plugin.getTournamentManager()));
}

@Override
protected void setupParsers() {
// No custom parses used
}

@Override
public void registerCommands() {
register(new VetoCommands());
register(new TournamentUserCommands());
register(new TournamentAdminCommands());
}

public <T> void registerCommands(List<T> commands) {
commands.forEach(this::register);
}
}
73 changes: 41 additions & 32 deletions src/main/java/dev/pgm/events/commands/TournamentAdminCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,60 @@
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import tc.oc.pgm.api.PGM;
import tc.oc.pgm.api.integration.Integration;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.match.MatchManager;
import tc.oc.pgm.api.player.VanishManager;
import tc.oc.pgm.lib.app.ashcon.intake.Command;
import tc.oc.pgm.lib.app.ashcon.intake.parametric.annotation.Text;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.lib.cloud.commandframework.annotations.Argument;
import tc.oc.pgm.lib.cloud.commandframework.annotations.CommandDescription;
import tc.oc.pgm.lib.cloud.commandframework.annotations.CommandMethod;
import tc.oc.pgm.lib.cloud.commandframework.annotations.CommandPermission;
import tc.oc.pgm.lib.cloud.commandframework.annotations.specifier.Greedy;

@CommandMethod("tourney|tournament|tm|events")
public class TournamentAdminCommands {

@Command(
aliases = "create",
desc = "Creates a tournament",
usage = "<format>",
perms = "events.staff")
@CommandMethod("create <format>")
@CommandDescription("Creates a tournament")
@CommandPermission("events.staff")
public void tourney(
CommandSender sender, TournamentManager manager, Match match, @Text String pool) {
CommandSender sender,
TournamentManager manager,
Match match,
@Argument("format") @Greedy String pool) {
manager.createTournament(match, MapFormatXMLParser.parse(pool));
sender.sendMessage(ChatColor.GOLD + "Starting tournament.");
}

@Command(aliases = "register", desc = "Register a team", usage = "<team>", perms = "events.staff")
@CommandMethod("register <team>")
@CommandDescription("Register a team")
@CommandPermission("events.staff")
public void register(
CommandSender sender,
TournamentTeamRegistry teamRegistry,
TournamentTeamManager teamManager,
@Text String name) {
@Argument("team") @Greedy String name) {
TournamentTeam team = teamRegistry.getTeam(name);
if (team == null) { // TODO move to provider
sender.sendMessage(ChatColor.RED + "Team not found!");
return;
}
// TODO move to provider
if (team == null) throw new CommandException("Team not found!");

VanishManager vanishManager = PGM.get().getVanishManager();
MatchManager matchManager = PGM.get().getMatchManager();

for (TournamentPlayer player : team.getPlayers())
if (vanishManager.isVanished(player.getUUID()))
vanishManager.setVanished(
matchManager.getPlayer(Bukkit.getPlayer(player.getUUID())), false, false);
for (TournamentPlayer player : team.getPlayers()) {
Player bukkit = Bukkit.getPlayer(player.getUUID());
MatchPlayer mp = matchManager.getPlayer(bukkit);
if (Integration.isVanished(bukkit)) Integration.setVanished(mp, false, false);
}

teamManager.addTeam(team);
sender.sendMessage(ChatColor.YELLOW + "Added team " + team.getName() + "!");
}

@Command(aliases = "list", desc = "List all loaded teams", perms = "events.staff")
@CommandMethod("list")
@CommandDescription("List all loaded teams")
@CommandPermission("events.staff")
public void list(CommandSender sender, TournamentTeamRegistry registry) {
sender.sendMessage(
ChatColor.GOLD
Expand All @@ -68,17 +77,15 @@ public void list(CommandSender sender, TournamentTeamRegistry registry) {
sender.sendMessage(ChatColor.YELLOW + "Run /tourney info <team> to see player roster!");
}

@Command(
aliases = "info",
desc = "View information about a team",
usage = "<team>",
perms = "events.staff")
public void info(CommandSender sender, TournamentTeamRegistry registry, @Text String name) {
@CommandMethod("info <team>")
@CommandDescription("View information about a team")
@CommandPermission("events.staff")
public void info(
CommandSender sender,
TournamentTeamRegistry registry,
@Argument("team") @Greedy String name) {
TournamentTeam team = registry.getTeam(name);
if (team == null) {
sender.sendMessage(ChatColor.RED + "Team not found!");
return;
}
if (team == null) throw new CommandException("Team not found!");

sender.sendMessage(
ChatColor.GOLD
Expand All @@ -97,7 +104,9 @@ public void info(CommandSender sender, TournamentTeamRegistry registry, @Text St
}
}

@Command(aliases = "unregisterall", desc = "Clear all registered teams", perms = "events.staff")
@CommandMethod("unregisterall")
@CommandDescription("Clear all registered teams")
@CommandPermission("events.staff")
public void clear(CommandSender sender, TournamentTeamManager teamManager) {
teamManager.clear();
sender.sendMessage(ChatColor.YELLOW + "Unregistered all teams!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import tc.oc.pgm.lib.app.ashcon.intake.Command;
import tc.oc.pgm.lib.cloud.commandframework.annotations.CommandDescription;
import tc.oc.pgm.lib.cloud.commandframework.annotations.CommandMethod;

@CommandMethod("tourney|tournament|tm|events")
public class TournamentUserCommands {

@Command(aliases = "score", desc = "Shows the current score in the tournament")
@CommandMethod("score")
@CommandDescription("Shows the current score in the tournament")
public void currentScore(CommandSender sender, TournamentFormat format) {
if (format instanceof FormatTournamentImpl) {
String formatName = ((FormatTournamentImpl) format).getFormatRound().settings().name();
Expand All @@ -30,7 +33,8 @@ public void currentScore(CommandSender sender, TournamentFormat format) {
}
}

@Command(aliases = "rounds", desc = "Shows the rounds from this event")
@CommandMethod("rounds")
@CommandDescription("Shows the rounds from this event")
public void rounds(CommandSender sender, TournamentFormat format) {
String header = "Event Rounds";
if (format instanceof FormatTournamentImpl)
Expand Down
Loading

0 comments on commit 976ba89

Please sign in to comment.