Skip to content

Commit

Permalink
v1.0.4 - Bug fixes and add help sub-command (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
SrBedrock authored Jul 2, 2023
1 parent 844113e commit 2cfcf7a
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 68 deletions.
6 changes: 6 additions & 0 deletions .github/renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:base"
]
}
39 changes: 39 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: "Build"

on:
push:
branches: [ 'main', 'dev' ]
pull_request:
branches: [ 'main', 'dev' ]
paths:
- 'gradle/**'
- 'src/**'
- 'build.gradle.kts'
- 'settings.gradle.kts'
- 'gradlew'
- 'gradlew.bat'
workflow_dispatch:
permissions:
contents: read

jobs:
build:
runs-on: 'ubuntu-latest'
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Run chmod to make gradlew executable
run: chmod +x ./gradlew
- name: Build with Gradle
uses: gradle/gradle-build-action@v2.4.2
with:
arguments: build
- name: Upload Plugin
uses: actions/upload-artifact@v3.1.2
with:
name: PluginControl
path: build/libs/*.jar
43 changes: 43 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: "CodeQL"

on:
push:
branches: [ 'main', 'dev' ]
pull_request:
branches: [ 'main', 'dev' ]
paths:
- 'gradle/**'
- 'src/**'
- 'build.gradle.kts'
- 'settings.gradle.kts'
- 'gradlew'
- 'gradlew.bat'
schedule:
- cron: '36 17 * * 3'

jobs:
analyze:
name: Analyze
runs-on: 'ubuntu-latest'
timeout-minutes: 360
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: [ 'java' ]
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
- name: Autobuild
uses: github/codeql-action/autobuild@v2
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{matrix.language}}"
38 changes: 28 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ Change the message formatting using [MiniMessage](https://webui.advntr.dev/)

### Placeholders

| Placeholder | Usage |
|------------------|-----------------------------------------------------------------------------------------------------------|
| `<prefix>` | Plugin prefix - all messages accept this placeholder |
| `<action>` | Plugin action used in `command.action-type` |
| `<actions>` | List of actions used in `command.action-list` |
| `<command>` | Command used in `command.command-not-found`, `command.plugin-add-error` and `command.plugin-remove-error` |
| `<kick-message>` | Kick message used in `command.kick-message` and `command.kick-message-set` |
| `<plugin>` | Plugin name used in `command.plugin-added`, `command.plugin-not-found` and `command.plugin-removed` |
| `<plugins>` | List of plugins used in `console.disabling-server` and `command.plugin-list` |
| Placeholder | Usage |
|------------------|-------------------------------------------------------------------------------------------|
| `<prefix>` | All messages accept this placeholder |
| `<action>` | `command.action-type` |
| `<actions>` | `command.action-list` |
| `<command>` | `command.command-not-found`, `command.plugin-add-error` and `command.plugin-remove-error` |
| `<kick-message>` | `command.kick-message` and `command.kick-message-set` |
| `<plugin>` | `command.plugin-added`, `command.plugin-not-found` and `command.plugin-removed` |
| `<plugins>` | `console.disabling-server` and `command.plugin-list` |

### Default

Expand All @@ -76,7 +76,7 @@ command:
action-list: '<prefix> <green>Actions available: <yellow><actions>'
action-set: '<prefix> <green>Action set to <yellow><action>'
action-type: '<prefix> <green>Action type: <yellow><action>'
command-not-found: '<red>Usage: <yellow>/<command> <add|remove|action|kick-message|toggle|on|off|list|reload>'
command-not-found: '<red>Use <yellow>/<command> help <red>to see the available commands'
kick-message: '<prefix> <green>Kick message: <yellow><kick-message>'
kick-message-set: '<prefix> <green>Kick message set to <yellow><kick-message>'
no-permission-error: '<prefix> <red>You do not have permission to use this command'
Expand All @@ -86,11 +86,28 @@ command:
plugin-disabled: '<prefix> <red>Deactivating plugin features...'
plugin-enabled: '<prefix> <green>Activating plugin features...'
plugin-list: '<prefix> <green>Plugins added: <yellow><plugins>'
plugin-list-separator: '<gray>, '
plugin-list-separator-last: '<gray> and '
plugin-list-empty: '<prefix> <red>No plugins added!'
plugin-not-found: '<prefix> <red>Plugin <yellow><plugin> <red>not found in the list!'
plugin-reload: '<prefix> <green>Config and Language reloaded!'
plugin-remove-error: '<red>Usage: <yellow>/<command> remove <plugin-name>'
plugin-removed: '<prefix> <green>Plugin <yellow><plugin> <green>removed!'
plugin-click-remove: '<red>Click to remove the plugin'
help:
- '<gradient:aqua:green>==== Plugin Control Help ====</gradient>'
- '<aqua>/<command> add <green><plugin-name> <yellow>- Add a plugin to the list'
- '<aqua>/<command> remove <green><plugin-name> <yellow>- Remove a plugin from the list'
- '<aqua>/<command> action <yellow>- List all actions available'
- '<aqua>/<command> kick-message <yellow>- Show the kick message'
- '<aqua>/<command> kick-message <message> <yellow>- Set the kick message'
- '<aqua>/<command> enable <yellow>- Enable the plugin'
- '<aqua>/<command> disable <yellow>- Disable the plugin'
- '<aqua>/<command> toggle <yellow>- Enable or disable the plugin'
- '<aqua>/<command> list <yellow>- List all plugins added'
- '<aqua>/<command> reload <yellow>- Reload the config and language'
- '<aqua>/<command> help <yellow>- Show this help'
- '<gradient:aqua:green>============================</gradient>'
```
## Commands
Expand All @@ -110,6 +127,7 @@ Main Command `/plugincontrol` - Aliases: `/pc` and `/pcontrol`
| `/plugincontrol` | `toggle` | Toggle PluginControl on or off. |
| `/plugincontrol` | `list` | List all plugins in the list. |
| `/plugincontrol` | `reload` | Reload the configuration and language files. |
| `/plugincontrol` | `help \| ?` | Show the list of commands. |

Please note that the `<plugin-name>`, `<action-type>` and `<message>` placeholders should be replaced with the specific
plugin name and kick message, respectively, as required.
Expand Down
7 changes: 6 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "com.armamc"
version = "1.0.3"
version = "1.0.4"

repositories {
mavenCentral()
Expand All @@ -17,6 +17,7 @@ dependencies {
implementation("net.kyori:adventure-api:4.14.0")
implementation("net.kyori:adventure-platform-bukkit:4.3.0")
implementation("net.kyori:adventure-text-minimessage:4.14.0")
implementation("net.kyori:adventure-text-serializer-legacy:4.14.0")
compileOnly(dependencyNotation = "org.spigotmc:spigot-api:1.19.4-R0.1-SNAPSHOT")
}

Expand All @@ -40,6 +41,10 @@ tasks {
options.compilerArgs.add("-parameters")
}

shadowJar {
minimize()
}

build {
dependsOn(shadowJar)
}
Expand Down
102 changes: 74 additions & 28 deletions src/main/java/com/armamc/plugincontrol/PluginControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import com.armamc.plugincontrol.config.Lang;
import com.armamc.plugincontrol.listeners.PlayerListener;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
Expand All @@ -18,11 +22,14 @@
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public final class PluginControl extends JavaPlugin {
private final ConsoleCommandSender sender = Bukkit.getConsoleSender();
private BukkitAudiences adventure;
private final ConsoleCommandSender consoleSender = Bukkit.getConsoleSender();
private final MiniMessage miniMessage = MiniMessage.miniMessage();
private BukkitAudiences adventure;
private PlayerListener playerListener;
private Config config;
private Lang lang;
Expand Down Expand Up @@ -81,36 +88,52 @@ private void registerTask() {
}

public void checkPlugins() {
if (!config.isEnabled()) return;

send(sender, lang.message("console.checking-plugins"), null);
var plugins = config.getPluginList();
var missingPlugins = new ArrayList<String>();
boolean hasPlugins = false;
for (String plugin : plugins) {
if (getServer().getPluginManager().getPlugin(plugin) == null) {
// If the plugin is disabled, don't check for plugins
if (!config.isEnabled()) {
return;
}

// Send a checking message to console
send(consoleSender, lang.message("console.checking-plugins"), null);

// Create a list of missing plugins
var missingPlugins = new HashSet<String>();

// Loop through the plugin list
for (String plugin : config.getPluginList()) {
// Check if the plugin is missing
if (!isPluginEnabled(plugin)) {
missingPlugins.add(plugin);
hasPlugins = true;
}
}
if (hasPlugins) {
var tag = Placeholder.parsed("plugins", String.join(", ", missingPlugins));
if (config.getAction().equals("disallow-player-login")) {
playerListener = new PlayerListener(this);
playerListener.init();
send(sender, lang.message("console.log-to-console"), tag);
return;
}
if (config.getAction().equals("log-to-console")) {
send(sender, lang.message("console.log-to-console"), tag);
return;
}
if (config.getAction().equals("shutdown-server")) {
send(sender, lang.message("console.disabling-server"), tag);
getServer().shutdown();
}

// If there are missing plugins, register the action
if (!missingPlugins.isEmpty()) {
registerAction(missingPlugins);
} else {
send(sender, lang.message("console.finished-checking"), null);
send(consoleSender, lang.message("console.finished-checking"), null);
}
}

private boolean isPluginEnabled(String pluginName) {
return getServer().getPluginManager().getPlugin(pluginName) != null;
}

private void registerAction(Set<String> missingPlugins) {
var tag = Placeholder.component("plugins", getPluginListComponent(new ArrayList<>(missingPlugins)));
if (config.getAction().equals("disallow-player-login")) {
playerListener = new PlayerListener(this);
playerListener.init();
send(consoleSender, lang.message("console.log-to-console"), tag);
return;
}
if (config.getAction().equals("log-to-console")) {
send(consoleSender, lang.message("console.log-to-console"), tag);
return;
}
if (config.getAction().equals("shutdown-server")) {
send(consoleSender, lang.message("console.disabling-server"), tag);
getServer().shutdown();
}
}

Expand All @@ -133,4 +156,27 @@ public void send(@NotNull CommandSender sender, @NotNull String message, @Nullab
}
}

public void send(@NotNull CommandSender sender, @NotNull List<String> message, @NotNull TagResolver tag) {
if (message.isEmpty()) {
return;
}
var prefix = Placeholder.parsed(PREFIX, lang.message(PREFIX));
message.forEach(line -> {
if (line.isEmpty()) return;
adventure().sender(sender).sendMessage(miniMessage.deserialize(line, prefix, tag));
});
}

public Component getPluginListComponent(@NotNull List<String> pluginList) {
var config = JoinConfiguration.builder()
.separator(miniMessage.deserialize(lang.message("command.plugin-list-separator")))
.lastSeparator(miniMessage.deserialize(lang.message("command.plugin-list-separator-last")))
.build();
var componentList = new ArrayList<Component>();
pluginList.forEach(pluginName -> componentList.add(Component.text(pluginName)
.hoverEvent(HoverEvent.showText(miniMessage.deserialize(lang.message("command.plugin-click-remove"))))
.clickEvent(ClickEvent.runCommand("/plugincontrol remove " + pluginName))));
return Component.join(config, componentList);
}

}
22 changes: 13 additions & 9 deletions src/main/java/com/armamc/plugincontrol/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ public boolean onCommand(@NotNull CommandSender sender, org.bukkit.command.@NotN
plugin.send(sender, lang.message("command.plugin-list-empty"), null);
} else {
plugin.send(sender, lang.message("command.plugin-list"),
Placeholder.parsed("plugins",
String.join(", ", config.getPluginList())));
Placeholder.component("plugins", plugin.getPluginListComponent(config.getPluginList())));
}
return true;
}
Expand All @@ -109,28 +108,33 @@ public boolean onCommand(@NotNull CommandSender sender, org.bukkit.command.@NotN
Placeholder.parsed("action", config.getAction().toLowerCase()));
return true;
}
List<String> actions = new ArrayList<>(List.of("log-to-console", "disallow-player-login", "shutdown-server"));
if (actions.contains(args[1])) {
config.setAction(args[1]);
var actions = new ArrayList<>(List.of("log-to-console", "disallow-player-login", "shutdown-server"));
if (actions.contains(args[1].toLowerCase())) {
config.setAction(args[1].toLowerCase());
plugin.send(sender, lang.message("command.action-set"),
Placeholder.parsed("action", config.getAction().toLowerCase()));
plugin.checkPlugins();
} else {
plugin.send(sender, lang.message("command.action-list"),
Placeholder.parsed("actions", String.join(", ", actions)));
}
return true;
}
case "kick-message" -> {
case "kick-message", "kickmessage" -> {
if (args.length < 2 || args[1].isBlank()) {
plugin.send(sender, lang.message("command.kick-message"),
Placeholder.parsed("kick-message", config.getKickMessage()));
Placeholder.component("kick-message", config.deserialize(config.getKickMessage())));
} else {
config.setKickMessage(String.join(" ", Arrays.copyOfRange(args, 1, args.length)));
plugin.send(sender, lang.message("command.kick-message-set"),
Placeholder.parsed("kick-message", config.getKickMessage()));
Placeholder.component("kick-message", config.deserialize(config.getKickMessage())));
}
return true;
}
case "help", "?" -> {
plugin.send(sender, lang.help(), Placeholder.parsed(COMMAND_TAG, label));
return true;
}
case "reload" -> {
reload();
plugin.send(sender, lang.message("command.plugin-reload"), null);
Expand All @@ -151,7 +155,7 @@ public boolean onCommand(@NotNull CommandSender sender, org.bukkit.command.@NotN

var completions = new ArrayList<String>();
if (args.length == 1) {
var subCommands = Arrays.asList("enable", "disable", "toggle", "add", "remove", "list", "action", "kick-message", "reload");
var subCommands = Arrays.asList("enable", "disable", "toggle", "add", "remove", "list", "action", "kick-message", "reload", "help");
StringUtil.copyPartialMatches(args[0], subCommands, completions);
return completions;
}
Expand Down
Loading

0 comments on commit 2cfcf7a

Please sign in to comment.