Skip to content

Commit

Permalink
Finished command command type
Browse files Browse the repository at this point in the history
  • Loading branch information
Smudgge committed Oct 14, 2023
1 parent 9dc890d commit 97fd322
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 19 deletions.
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.github.smuddgge</groupId>
<artifactId>Leaf</artifactId>
<name>Leaf</name>
<version>4.1.0</version>
<version>4.2.0</version>
<description>A velocity utility plugin</description>
<build>
<resources>
Expand Down
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>com.github.smuddgge</groupId>
<artifactId>Leaf</artifactId>
<version>4.1.0</version>
<version>4.2.0</version>
<packaging>jar</packaging>

<name>Leaf</name>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/github/smuddgge/leaf/Leaf.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
Leaf.commandHandler.addType(new AlertRaw());
Leaf.commandHandler.addType(new AlertMessage());
Leaf.commandHandler.addType(new Chat());
Leaf.commandHandler.addType(new com.github.smuddgge.leaf.commands.types.Command());
Leaf.commandHandler.addType(new Find());
Leaf.commandHandler.addType(new Friend());
Leaf.commandHandler.addType(new History());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package com.github.smuddgge.leaf.commands;

import com.github.smuddgge.leaf.Leaf;
import com.github.smuddgge.leaf.configuration.ConfigMain;
import com.github.smuddgge.leaf.database.records.FriendRecord;
import com.github.smuddgge.leaf.database.records.PlayerRecord;
import com.github.smuddgge.leaf.database.tables.FriendTable;
import com.github.smuddgge.leaf.database.tables.PlayerTable;
import com.github.smuddgge.leaf.datatype.User;
import com.github.smuddgge.leaf.utility.PlayerUtility;
import com.github.smuddgge.squishyconfiguration.interfaces.ConfigurationSection;
import com.github.smuddgge.squishydatabase.Query;
import com.velocitypowered.api.proxy.Player;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public interface CommandType {
/**
* Executed when a command is registered.
*
* @param action The create command action to add options.
* @param section The command section.
* @param action The create command action to add options.
*/
default void onDiscordRegister(ConfigurationSection section, @NotNull CommandCreateAction action) {
}
Expand Down
39 changes: 35 additions & 4 deletions src/main/java/com/github/smuddgge/leaf/commands/types/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.github.smuddgge.leaf.commands.CommandStatus;
import com.github.smuddgge.leaf.commands.CommandSuggestions;
import com.github.smuddgge.leaf.datatype.User;
import com.github.smuddgge.leaf.discord.DiscordBotMessageAdapter;
import com.github.smuddgge.leaf.utility.CommandUtility;
import com.github.smuddgge.leaf.utility.PlayerUtility;
import com.github.smuddgge.squishyconfiguration.interfaces.ConfigurationSection;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.requests.restaction.CommandCreateAction;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -87,19 +89,48 @@ public CommandStatus onPlayerRun(ConfigurationSection section, String[] argument

@Override
public void onDiscordRegister(ConfigurationSection section, @NotNull CommandCreateAction action) {
action.addOption(OptionType.STRING, "player", "The players name.").complete();

// Get suggestion section.
ConfigurationSection argumentSection = section.getSection("discord_bot").getSection("arguments");

// Loop though suggestions.
for (String key : argumentSection.getKeys()) {
action.addOption(OptionType.STRING, key, argumentSection.getString(key));
}

action.complete();
}

@Override
public CommandStatus onDiscordRun(ConfigurationSection section, SlashCommandEvent event) {
return super.onDiscordRun(section, event);

// Loop though each command.
for (String command : section.getSection("discord_bot").getListString("commands")) {

for (OptionMapping optionMapping : event.getOptions()) {

// Replace argument placeholder.
command = command.replace("%" + optionMapping.getName() + "%", optionMapping.getAsString());
}

// Execute command.
CommandUtility.executeCommandInConsole(command);
}

// Respond message.
event.reply(new DiscordBotMessageAdapter(
section, "discord_bot.message",
"Ran commands."
).buildMessage()).queue();

return new CommandStatus();
}

/**
* Used to get the list of commands
* to execute.
*
* @param section The instance of the configuration section.
* @param section The instance of the configuration section.
* @param arguments The instance of the command arguments.
* @return The list of commands to execute.
*/
Expand All @@ -115,7 +146,7 @@ public CommandStatus onDiscordRun(ConfigurationSection section, SlashCommandEven
int argumentNumber = 1;
for (String argument : arguments) {
finalCommand = finalCommand.replace("%argument_" + argumentNumber + "%", argument);
argumentNumber ++;
argumentNumber++;
}

finalCommandList.add(finalCommand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ public void clearHeaderAndFooter() {
this.player.clearHeaderAndFooter();
}

@Override
public void clearPlayerListHeaderAndFooter() {
this.player.clearPlayerListHeaderAndFooter();
}

@Override
public Component getPlayerListHeader() {
return this.player.getPlayerListHeader();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.smuddgge.leaf.utility;

import com.github.smuddgge.leaf.Leaf;
import com.github.smuddgge.leaf.datatype.OpPlayerAdapter;
import com.velocitypowered.api.command.CommandManager;
import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.smuddgge.leaf.utility;

import com.github.smuddgge.leaf.Leaf;
import com.github.smuddgge.leaf.commands.CommandSuggestions;
import com.github.smuddgge.leaf.configuration.ConfigMain;
import com.github.smuddgge.leaf.database.records.FriendRecord;
import com.github.smuddgge.leaf.database.records.PlayerRecord;
Expand Down
11 changes: 9 additions & 2 deletions src/main/resources/commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,15 @@ command:
permissions:
- ADMINISTRATOR
no_permission: "You do not have permission to run this command."
not_found: "Player could not be found."
found: "Player was found on {server_formatted}"
# List of suggestions.
arguments:
arg_1: "The descrption."
# Commands to be run in console.
commands:
- "find %arg_1%"
- "lobby"
message: "Ran commands."



info:
Expand Down

0 comments on commit 97fd322

Please sign in to comment.