Skip to content

Commit

Permalink
Adding functionality that Console can use commands
Browse files Browse the repository at this point in the history
Fully working version just not all features i wanna implement.
- changed all usage of Player to CommandSource inside of PteroCommand.
- updated Github workflow
- updated version

Took 10 minutes
  • Loading branch information
TubYoub committed Jul 17, 2024
1 parent 4fda6f8 commit 2e875a3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 36 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,4 @@ jobs:
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml

# Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
- name: Update dependency graph
uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6
run: mvn -B package --file pom.xml
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.tubyoub</groupId>
<artifactId>VelocityPteroPower</artifactId>
<version>0.9.1</version>
<version>0.9.2</version>
<packaging>jar</packaging>

<name>VelocityPteroPower</name>
Expand Down
59 changes: 29 additions & 30 deletions src/main/java/de/tubyoub/velocitypteropower/PteroCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,93 +75,92 @@ public void execute(Invocation invocation) {
CommandSource sender = invocation.source();
String[] args = invocation.arguments();

Player player = (Player) sender;

if (args.length == 0) {
displayHelp(player);
displayHelp(sender);
return;
}

String subCommand = args[0].toLowerCase();

switch (subCommand) {
case "start":
if (player.hasPermission("ptero.start")) {
startServer(player, args);
if (sender.hasPermission("ptero.start")) {
startServer(invocation.source(), args);
} else {
player.sendMessage(getSPPPrefix().append(Component.text("You do not have permission to use this command.",TextColor.color(255,0,0))));
sender.sendMessage(getSPPPrefix().append(Component.text("You do not have permission to use this command.",TextColor.color(255,0,0))));
}
break;
case "stop":
if (player.hasPermission("ptero.stop")) {
stopServer(player, args);
if (sender.hasPermission("ptero.stop")) {
stopServer(sender, args);
} else {
player.sendMessage(getSPPPrefix().append(Component.text("You do not have permission to use this command.",TextColor.color(255,0,0))));
sender.sendMessage(getSPPPrefix().append(Component.text("You do not have permission to use this command.",TextColor.color(255,0,0))));
}
break;
case "reload":
if (player.hasPermission("ptero.reload")) {
reloadConfig(player);
if (sender.hasPermission("ptero.reload")) {
reloadConfig(sender);
} else {
player.sendMessage(getSPPPrefix().append(Component.text("You do not have permission to use this command.",TextColor.color(255,0,0))));
sender.sendMessage(getSPPPrefix().append(Component.text("You do not have permission to use this command.",TextColor.color(255,0,0))));
}
break;
default:
player.sendMessage(getSPPPrefix().append(Component.text("Unknown subcommand: " + subCommand)));
displayHelp(player);
sender.sendMessage(getSPPPrefix().append(Component.text("Unknown subcommand: " + subCommand)));
displayHelp(sender);
}
}

/**
* This method is called to start a server.
*
* @param player the player who executed the command
* @param sender the player who executed the command
* @param args the command arguments
*/
private void startServer(Player player, String[] args) {
private void startServer(CommandSource sender, String[] args) {
if (args.length < 2) {
player.sendMessage(getSPPPrefix().append(Component.text("Usage: /ptero start <serverName>", NamedTextColor.RED)));
sender.sendMessage(getSPPPrefix().append(Component.text("Usage: /ptero start <serverName>", NamedTextColor.RED)));
return;
}
String serverName = args[1];
Map<String, PteroServerInfo> serverInfoMap = plugin.getServerInfoMap();
if (serverInfoMap.containsKey(serverName)) {
PteroServerInfo serverInfo = serverInfoMap.get(serverName);
apiClient.powerServer(serverInfo.getServerId(), "start");
player.sendMessage(getSPPPrefix().append(Component.text("The server: "+ serverName + " is starting")));
sender.sendMessage(getSPPPrefix().append(Component.text("The server: "+ serverName + " is starting")));
} else {
}
}

/**
* This method is called to stop a server.
*
* @param player the player who executed the command
* @param sender the player who executed the command
* @param args the command arguments
*/
private void stopServer(Player player, String[] args) {
private void stopServer(CommandSource sender, String[] args) {
if (args.length < 2) {
player.sendMessage(getSPPPrefix().append(Component.text("Usage: /ptero stop <serverName>", TextColor.color(66,135,245))));
sender.sendMessage(getSPPPrefix().append(Component.text("Usage: /ptero stop <serverName>", TextColor.color(66,135,245))));
return;
}
String serverName = args[1];
Map<String, PteroServerInfo> serverInfoMap = plugin.getServerInfoMap();
if (serverInfoMap.containsKey(serverName)) {
PteroServerInfo serverInfo = serverInfoMap.get(serverName);
apiClient.powerServer(serverInfo.getServerId(), "stop");
player.sendMessage(getSPPPrefix().append(Component.text("The server: "+ serverName + " is stopping")));
sender.sendMessage(getSPPPrefix().append(Component.text("The server: "+ serverName + " is stopping")));
} else {
}
}

/**
* This method is called to reload the configuration.
*
* @param player the player who executed the command
* @param sender the player who executed the command
*/
private void reloadConfig(Player player) {
private void reloadConfig(CommandSource sender) {
plugin.reloadConfig();
player.sendMessage(getSPPPrefix().append(Component.text("Configuration reloaded.",TextColor.color(0,255,0))));
sender.sendMessage(getSPPPrefix().append(Component.text("Configuration reloaded.",TextColor.color(0,255,0))));
}

/**
Expand Down Expand Up @@ -195,12 +194,12 @@ public List<String> suggest(Invocation invocation) {
return null;
}

private void displayHelp(Player player) {
player.sendMessage(getSPPPrefix().append(Component.text("Available commands:", NamedTextColor.GREEN)));
player.sendMessage(getSPPPrefix().append(Component.text("/ptero start <serverName>", TextColor.color(66,135,245))));
player.sendMessage(getSPPPrefix().append(Component.text("/ptero stop <serverName>", TextColor.color(66,135,245))));
player.sendMessage(getSPPPrefix().append(Component.text("/ptero reload", TextColor.color(66,135,245))));
player.sendMessage(getSPPPrefix().append(Component.text("/ptero help", TextColor.color(66,135,245))));
private void displayHelp(CommandSource sender) {
sender.sendMessage(getSPPPrefix().append(Component.text("Available commands:", NamedTextColor.GREEN)));
sender.sendMessage(getSPPPrefix().append(Component.text("/ptero start <serverName>", TextColor.color(66,135,245))));
sender.sendMessage(getSPPPrefix().append(Component.text("/ptero stop <serverName>", TextColor.color(66,135,245))));
sender.sendMessage(getSPPPrefix().append(Component.text("/ptero reload", TextColor.color(66,135,245))));
sender.sendMessage(getSPPPrefix().append(Component.text("/ptero help", TextColor.color(66,135,245))));
}

private Component getSPPPrefix() {
Expand Down

0 comments on commit 2e875a3

Please sign in to comment.