From 6e0d1037bc3e4bf4cc73d46f3e26f0f033440fb9 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Fri, 1 Mar 2024 15:31:28 +0100 Subject: [PATCH] add config menu/command name & permission. also edit version number. --- README.md | 2 ++ build.gradle.kts | 2 +- .../cuanvil/gui/utils/GuiGlobalActions.java | 35 ++++++++++++++++--- src/main/kotlin/io/delilaheve/CustomAnvil.kt | 8 +++-- ...{TestExecutor.kt => EditConfigExecutor.kt} | 9 +++-- src/main/resources/plugin.yml | 12 +++++-- 6 files changed, 54 insertions(+), 14 deletions(-) rename src/main/kotlin/xyz/alexcrea/cuanvil/command/{TestExecutor.kt => EditConfigExecutor.kt} (61%) diff --git a/README.md b/README.md index 47941b8..4dde28b 100644 --- a/README.md +++ b/README.md @@ -30,10 +30,12 @@ ca.affected: Player with this permission will be affected by the plugin ca.bypass.fuse: Allow player to combine every enchantments to every item (no custom limit) ca.bypass.level: Allow player to bypass every level limit (no custom limit) ca.command.reload: Allow administrator to reload the plugin's configs +ca.config.edit: Allow administrator to reload the plugin's configs in game ``` ### Commands ```yml anvilconfigreload or carl: Reload every config of this plugin +customanvilconfig: open a menu for administrator to edit plugin's config in game ``` --- ### Default Configuration: diff --git a/build.gradle.kts b/build.gradle.kts index e0e1a4b..5f6dcb5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { } group = "xyz.alexcrea" -version = "1.3" +version = "1.3.0-alpha-1" repositories { mavenCentral() diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java b/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java index 69989ed..b641c50 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java @@ -1,6 +1,8 @@ package xyz.alexcrea.cuanvil.gui.utils; import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; +import io.delilaheve.CustomAnvil; +import org.bukkit.entity.HumanEntity; import org.bukkit.event.inventory.InventoryClickEvent; import org.jetbrains.annotations.NotNull; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; @@ -12,7 +14,8 @@ import java.util.function.Consumer; public class GuiGlobalActions { - public static Consumer stayInPlace = (event) -> event.setCancelled(true); + public final static String NO_EDIT_PERM = "§cYou do not have permission to edit the config"; + public final static Consumer stayInPlace = (event) -> event.setCancelled(true); public static @NotNull Consumer openGuiAction( @@ -21,11 +24,18 @@ public class GuiGlobalActions { @NotNull Object... args){ return event -> { event.setCancelled(true); + HumanEntity player = event.getWhoClicked(); + // Do not allow to open inventory if player do not have edit configuration permission + if(!player.hasPermission(CustomAnvil.editConfigPermission)) { + player.closeInventory(); + player.sendMessage(NO_EDIT_PERM); + return; + } try { Constructor constructor = clazz.getConstructor(argClass); // Assume constructor is accessible Gui gui = constructor.newInstance(args); - gui.show(event.getWhoClicked()); + gui.show(player); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) { @@ -49,8 +59,15 @@ public class GuiGlobalActions { public static @NotNull Consumer openGuiAction(@NotNull Gui goal) { return event -> { + HumanEntity player = event.getWhoClicked(); + // Do not allow to open inventory if player do not have edit configuration permission + if(!player.hasPermission(CustomAnvil.editConfigPermission)) { + player.closeInventory(); + player.sendMessage(NO_EDIT_PERM); + return; + } event.setCancelled(true); - goal.show(event.getWhoClicked()); + goal.show(player); }; } @@ -59,14 +76,22 @@ public class GuiGlobalActions { @NotNull ValueUpdatableGui goal) { return event -> { event.setCancelled(true); + HumanEntity player = event.getWhoClicked(); + // Do not allow to save configuration if player do not have edit configuration permission + if(!player.hasPermission(CustomAnvil.editConfigPermission)) { + player.closeInventory(); + player.sendMessage(NO_EDIT_PERM); + return; + } + // Save setting if(!setting.onSave()){ - event.getWhoClicked().sendMessage("\u00A7cSomething wrong happen while saving the change of value."); + player.sendMessage("\u00A7cSomething wrong happen while saving the change of value."); } // Update gui for the one who have it open goal.updateGuiValues(); // Then show - goal.show(event.getWhoClicked()); + goal.show(player); }; } } diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt index 9b6bdf5..4172392 100644 --- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt +++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt @@ -5,7 +5,7 @@ import org.bukkit.Bukkit import org.bukkit.configuration.file.YamlConfiguration import org.bukkit.plugin.java.JavaPlugin import xyz.alexcrea.cuanvil.command.ReloadExecutor -import xyz.alexcrea.cuanvil.command.TestExecutor +import xyz.alexcrea.cuanvil.command.EditConfigExecutor import xyz.alexcrea.cuanvil.config.ConfigHolder import xyz.alexcrea.cuanvil.util.Metrics import xyz.alexcrea.cuanvil.util.MetricsUtil @@ -30,11 +30,13 @@ class CustomAnvil : JavaPlugin() { const val bypassLevelPermission = "ca.bypass.level" // Permission string required to reload the config const val commandReloadPermission = "ca.command.reload" + // Permission string required to edit the plugin's config + const val editConfigPermission = "ca.config.edit" // Command Name to reload the config const val commandReloadName = "anvilconfigreload" // Test command name - const val commandTestName = "test" + const val commandTestName = "customanvilconfig" // Current plugin instance lateinit var instance: CustomAnvil @@ -113,7 +115,7 @@ class CustomAnvil : JavaPlugin() { command?.setExecutor(ReloadExecutor()) command = getCommand(commandTestName) - command?.setExecutor(TestExecutor()) + command?.setExecutor(EditConfigExecutor()) } } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/TestExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt similarity index 61% rename from src/main/kotlin/xyz/alexcrea/cuanvil/command/TestExecutor.kt rename to src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt index fbe1506..604911f 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/TestExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt @@ -1,15 +1,20 @@ package xyz.alexcrea.cuanvil.command +import io.delilaheve.CustomAnvil import org.bukkit.command.Command import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandSender import org.bukkit.entity.HumanEntity import xyz.alexcrea.cuanvil.gui.MainConfigGui +import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalActions -class TestExecutor : CommandExecutor { +class EditConfigExecutor : CommandExecutor { override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean { - + if(!sender.hasPermission(CustomAnvil.editConfigPermission)) { + sender.sendMessage(GuiGlobalActions.NO_EDIT_PERM) + return false + } if(sender !is HumanEntity) return false MainConfigGui.INSTANCE.show(sender) diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 773da9b..daa728f 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,7 +1,7 @@ main: io.delilaheve.CustomAnvil name: CustomAnvil prefix: "Custom Anvil" -version: 1.3 +version: 1.3.0-alpha-1 description: Allow to customise anvil mechanics api-version: 1.18 load: POSTWORLD @@ -18,8 +18,11 @@ commands: #- acreload # anvil config reload #- careload # custom anvil reload - carl # custom anvil reload - test: - description: test cmd + customanvilconfig: + description: open a menu for administrator to edit plugin's config in game + permission: ca.config.edit + aliases: + - configanvil permissions: ca.affected: @@ -34,6 +37,9 @@ permissions: ca.command.reload: default: op description: Allow administrator to reload the plugin's configs + ca.config.edit: + default: op + description: Allow administrator to reload the plugin's configs in game # soft depend on old name so I can disable it if it is on the same server # as it is the old name for this plugin