Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.0.3 - New features #7

Merged
merged 13 commits into from
Jun 23, 2023
125 changes: 103 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,119 @@

This plugin allows you to control which plugins have to be enabled for the server continues running.

Inspired by [this](https:/PaperMC/Paper/issues/8859#issuecomment-1435905791) and [this](https:/PaperMC/Paper/pull/8108#issuecomment-1419304955) comment and myself having the same problem.
Inspired by [this](https:/PaperMC/Paper/issues/8859#issuecomment-1435905791)
and [this](https:/PaperMC/Paper/pull/8108#issuecomment-1419304955) comment and myself having the same
problem.

## Installation

1. Download the plugin from [here](https:/SrBedrock/PluginControl/releases/).
2. Put the plugin in the `plugins` folder of your server.
3. Restart the server.
4. Add plugins to the list with `/plugincontrol add <plugin>`.
5. Enable the plugin with `/plugincontrol enable`.
1. Download the PluginControl plugin from [here](https:/SrBedrock/PluginControl/releases/).
2. Place the downloaded plugin file in the `plugins` folder of your server.
3. Restart the server to load the PluginControl plugin.
4. Use the command `/plugincontrol action <action-type>` to set the action to be taken if any of the listed plugins are
not enabled. See the [Actions](#actions) section for available actions.
5. Add plugins to the list using the command `/plugincontrol add <plugin-name>`.
6. Enable the PluginControl plugin by running the command `/plugincontrol enable`.
7. Reload the PluginControl plugin configuration and language files with the command `/plugincontrol reload`.

Please note that the `<action-type>` and `<plugin-name>` placeholders should be replaced with the specific action and
plugin names as needed.

**Note: This plugin needs Java 17 to work.**

## Configuration

| Option | Description |
|-----------|-------------------------------------------------------|
| `enabled` | Whether the plugin is enabled or not. |
| `plugins` | List of plugins to be enabled when the server starts. |
| Option | Description |
|----------------|--------------------------------------------------------------------------------|
| `enabled` | Whether the plugin is enabled or not. |
| `action` | [Action to take if all listed plugins are not enabled.](#actions) |
| `kick-message` | Message sent to the player when the `disallow-player-login` action is enabled. |
| `plugins` | List of plugins to be enabled when the server starts. |

### Actions

| Action Type | Description |
|-------------------------|---------------------------------------------------------------------------------------|
| `log-to-console` | Sends a warning (`log-to-console` inside lang.yaml) in the console. |
| `disallow-player-login` | Block player from enter the server with a message (`kick-message` inside config.yml). |
| `shutdown-server` | Shutdown the server with a warning (`disabling-server` inside lang.yml). |

### Kick Message

Allows you to customize the message sent to the player when the `disallow-player-login` action is enabled.

Can be customized using `&#<hex>` code or legacy color codes (no MiniMessage support).

## Messages

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` |

### Default

```yaml
prefix: '<dark_gray>[<red>PluginControl<dark_gray>]'
console:
checking-plugins: '<prefix> <red>Checking plugins...'
disabling-server: '<prefix> <red>Disabling server because <yellow><plugins> <red>was not found or enabled successfully!'
finished-checking: '<prefix> <green>Plugins successfully verified!'
log-to-console: '<prefix> <red>Plugin <yellow><plugins> <red>not found or enabled successfully...'
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>'
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'
plugin-add-error: '<red>Usage: <yellow>/<command> add [plugin-name]'
plugin-added: '<prefix> <green>Plugin <yellow><plugin> <green>added successfully!'
plugin-already-added: '<prefix> <red>Plugin already added!'
plugin-disabled: '<prefix> <red>Deactivating plugin features...'
plugin-enabled: '<prefix> <green>Activating plugin features...'
plugin-list: '<prefix> <green>Plugins added: <yellow><plugins>'
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!'
```

## Commands

Main command aliases: `/pc` and `/pcontrol`.
Main Command `/plugincontrol` - Aliases: `/pc` and `/pcontrol`

| Command | Sub Command | Description |
|------------------|--------------------------|----------------------------------------------|
| `/plugincontrol` | `add <plugin-name>` | Add a plugin to the list. |
| `/plugincontrol` | `remove <plugin-name>` | Remove a plugin from the list. |
| `/plugincontrol` | `action` | Check the current action. |
| `/plugincontrol` | `action <action-type>` | [Set the action to take.](#actions) |
| `/plugincontrol` | `kick-message` | Check the current kick message. |
| `/plugincontrol` | `kick-message <message>` | Set the kick message. |
| `/plugincontrol` | `enable \| on` | Enable PluginControl. |
| `/plugincontrol` | `disable \| off` | Disable PluginControl. |
| `/plugincontrol` | `toggle` | Toggle PluginControl on or off. |
| `/plugincontrol` | `list` | List all plugins in the list. |
| `/plugincontrol` | `reload` | Reload the configuration and language files. |

| Command | Sub Command | Description |
|-------------------|-------------------|----------------------------------|
| `/plugincontrol` | `add <plugin>` | Add a plugin to the list. |
| `/plugincontrol` | `remove <plugin>` | Remove a plugin from the list. |
| `/plugincontrol` | `enable \| on` | Enable PluginControl. |
| `/plugincontrol` | `disable \| off` | Disable PluginControl. |
| `/plugincontrol` | `toggle` | Enable or disable PluginControl. |
| `/plugincontrol` | `list` | List all plugins in the list. |
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.

## Permissions

| Permissions | Description |
|----------------------|---------------------------------------------|
| `plugincontrol.use` | Permission to use all [commands](#commands) |
| Permissions | Description |
|------------------------|------------------------------------------------|
| `plugincontrol.use` | Permission to use all [commands](#commands) |
| `plugincontrol.bypass` | Bypass the `disallow-player-login` in `action` |
5 changes: 3 additions & 2 deletions 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.2"
version = "1.0.3"

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

java {
Expand Down
99 changes: 76 additions & 23 deletions src/main/java/com/armamc/plugincontrol/PluginControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,37 @@

import com.armamc.plugincontrol.commands.Command;
import com.armamc.plugincontrol.config.Config;
import com.armamc.plugincontrol.config.Lang;
import com.armamc.plugincontrol.listeners.PlayerListener;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
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;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.NotNull;

import java.text.MessageFormat;
import java.util.List;
import java.util.ArrayList;

public final class PluginControl extends JavaPlugin {
private static PluginControl instance;
private final ConsoleCommandSender sender = Bukkit.getConsoleSender();
private BukkitAudiences adventure;
private final MiniMessage miniMessage = MiniMessage.miniMessage();
private PlayerListener playerListener;
private Config config;
private Lang lang;
private static final String PREFIX = "prefix";

@Override
public void onEnable() {
instance = this;
this.adventure = BukkitAudiences.create(this);
registerConfig();
registerCommands();
saveDefaultConfig();
Config.load();
registerTask();
}

Expand All @@ -33,43 +44,73 @@ public void onDisable() {
}
}

public static PluginControl getInstance() {
return instance;
public Config getPluginConfig() {
return config;
}

public Lang getPluginLang() {
return lang;
}

public void unregisterListener() {
if (playerListener != null) {
PlayerLoginEvent.getHandlerList().unregister(this);
playerListener = null;
}
}

private void registerConfig() {
if (!getDataFolder().exists() && getDataFolder().mkdir()) {
getLogger().info("Creating the plugin folder!");
}
config = new Config(this);
lang = new Lang(this);
}

private void registerCommands() {
Command command = new Command();
PluginCommand pluginCommand = getCommand("plugincontrol");
var command = new Command(this);
var pluginCommand = getCommand("plugincontrol");
if (pluginCommand != null) {
pluginCommand.setExecutor(command);
pluginCommand.setTabCompleter(command);
}
}

private void registerTask() {
Bukkit.getScheduler().runTaskLater(this, () -> {
if (Config.isEnabled()) {
getLogger().info("Checking plugins...");
checkPlugins();
}
}, 20L);
Bukkit.getScheduler().runTaskLater(this, this::checkPlugins, 20L);
}

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

private void checkPlugins() {
List<String> plugins = Config.getPluginList();
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) {
getLogger().warning(() -> MessageFormat.format("Plugin {0} não encontrado!", plugin));
missingPlugins.add(plugin);
hasPlugins = true;
}
}
if (hasPlugins) {
getLogger().warning("One or more plugins required for the server to function properly were not found. Shutting down the server!");
getServer().shutdown();
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();
}
} else {
getLogger().info("Plugins successfully verified!");
send(sender, lang.message("console.finished-checking"), null);
}
}

Expand All @@ -80,4 +121,16 @@ private void checkPlugins() {
return this.adventure;
}

public void send(@NotNull CommandSender sender, @NotNull String message, @Nullable TagResolver tag) {
if (message.isEmpty() || message.isBlank()) {
return;
}
var prefix = Placeholder.parsed(PREFIX, lang.message(PREFIX));
if (tag == null) {
adventure().sender(sender).sendMessage(miniMessage.deserialize(message, prefix));
} else {
adventure().sender(sender).sendMessage(miniMessage.deserialize(message, prefix, tag));
}
}

}
Loading