From f0d77685b3310e3ee8ac1f973bb6d0cb038f45a1 Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Tue, 10 Dec 2024 12:59:36 +0000 Subject: [PATCH 01/20] Refactor ReloadCommand to use PluginManager for reloading and update success messages --- .../xodium/vanillaplus/commands/ReloadCommand.java | 12 ++++++++---- .../org/xodium/vanillaplus/interfaces/PERMS.java | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java index dfeaf58..97bb50a 100644 --- a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java +++ b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java @@ -4,6 +4,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.plugin.PluginManager; import org.xodium.vanillaplus.VanillaPlus; import org.xodium.vanillaplus.interfaces.MSG; import org.xodium.vanillaplus.interfaces.PERMS; @@ -13,12 +14,14 @@ import net.kyori.adventure.text.minimessage.MiniMessage; public class ReloadCommand implements MSG { - private static final String RELOAD_SUCC_LOG_MSG = "Configuration reloaded successfully."; - private static final String RELOAD_SUCC_MSG = PREFIX + "Configuration reloaded successfully."; - private static final String PERM_ERR_MSG = PREFIX + "You do not have permission to use this command!"; private final static VanillaPlus vp = VanillaPlus.getInstance(); + private final static PluginManager pm = vp.getServer().getPluginManager(); private final static MiniMessage mm = MiniMessage.miniMessage(); + private static final String RELOAD_SUCC_LOG_MSG = "Reloaded successfully."; + private static final String RELOAD_SUCC_MSG = PREFIX + "Reloaded successfully."; + private static final String PERM_ERR_MSG = PREFIX + "You do not have permission to use this command!"; + { vp.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, e -> { e.registrar().register( @@ -29,7 +32,8 @@ public class ReloadCommand implements MSG { p.sendMessage(mm.deserialize(PERM_ERR_MSG)); return 0; } - vp.reloadConfig(); + pm.disablePlugin(vp); + pm.enablePlugin(vp); cs.sendMessage(mm.deserialize(RELOAD_SUCC_MSG)); vp.getLogger().info(RELOAD_SUCC_LOG_MSG); return Command.SINGLE_SUCCESS; diff --git a/src/main/java/org/xodium/vanillaplus/interfaces/PERMS.java b/src/main/java/org/xodium/vanillaplus/interfaces/PERMS.java index f647009..a92d148 100644 --- a/src/main/java/org/xodium/vanillaplus/interfaces/PERMS.java +++ b/src/main/java/org/xodium/vanillaplus/interfaces/PERMS.java @@ -1,5 +1,6 @@ package org.xodium.vanillaplus.interfaces; +// TODO: refactor to each module for modularity. public interface PERMS { String VANILLAPLUS = "vanillaplus."; String RELOAD = VANILLAPLUS + "reload"; From c26abb80050602591c61859d58e4c4431cd2d7bf Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Tue, 10 Dec 2024 13:14:55 +0000 Subject: [PATCH 02/20] added support for 1.21.1 - 1.21.3 - 1.21.4 --- build.gradle.kts | 2 ++ src/main/java/org/xodium/vanillaplus/VanillaPlus.java | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index cee8048..a156496 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -15,7 +15,9 @@ repositories { } dependencies { + compileOnly("io.papermc.paper:paper-api:1.21.1-R0.1-SNAPSHOT") compileOnly("io.papermc.paper:paper-api:1.21.3-R0.1-SNAPSHOT") + compileOnly("io.papermc.paper:paper-api:1.21.4-R0.1-SNAPSHOT") implementation("net.kyori:adventure-api:4.17.0") } diff --git a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java index f44aa2f..e59b438 100644 --- a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java +++ b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java @@ -6,7 +6,7 @@ public class VanillaPlus extends JavaPlugin { - private static final String[] V = { "1.21.3" }; + private static final String[] V = { "1.21.1", "1.21.3", "1.21.4" }; private static final String[] PAPER = { "Paper" }; private static final String IS_PAPER_MSG = "This plugin is not compatible with non-Paper servers."; private static final String IS_SUPPORTED_VERSION_MSG = "This plugin requires Paper version: " + String.join(", ", V); From f57ed346e4609b8f9a250e86e183785a870ab5a7 Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Tue, 10 Dec 2024 13:38:45 +0000 Subject: [PATCH 03/20] Refactor ModuleManager and VanillaPlus to centralize message handling and improve logging format --- src/main/java/org/xodium/vanillaplus/ModuleManager.java | 6 +++--- src/main/java/org/xodium/vanillaplus/VanillaPlus.java | 7 ++++++- .../org/xodium/vanillaplus/commands/ReloadCommand.java | 2 +- src/main/java/org/xodium/vanillaplus/interfaces/MSG.java | 6 ------ 4 files changed, 10 insertions(+), 11 deletions(-) delete mode 100644 src/main/java/org/xodium/vanillaplus/interfaces/MSG.java diff --git a/src/main/java/org/xodium/vanillaplus/ModuleManager.java b/src/main/java/org/xodium/vanillaplus/ModuleManager.java index a1247a7..969b76b 100644 --- a/src/main/java/org/xodium/vanillaplus/ModuleManager.java +++ b/src/main/java/org/xodium/vanillaplus/ModuleManager.java @@ -1,12 +1,12 @@ package org.xodium.vanillaplus; -import org.xodium.vanillaplus.interfaces.MSG; +import org.xodium.vanillaplus.VanillaPlus.MSG; import org.xodium.vanillaplus.interfaces.Modular; import org.xodium.vanillaplus.modules.DoorsModule; import java.util.List; -public class ModuleManager { +public class ModuleManager implements MSG { private final VanillaPlus vp = VanillaPlus.getInstance(); { @@ -16,7 +16,7 @@ public class ModuleManager { .filter(Modular::isEnabled) .forEach(module -> { vp.getServer().getPluginManager().registerEvents(module, vp); - vp.getLogger().info(MSG.MODULE_LOADED + module.getClass().getSimpleName()); + vp.getLogger().info(PREFIX + "Loaded: " + module.getClass().getSimpleName()); }); } } \ No newline at end of file diff --git a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java index e59b438..7d8f9db 100644 --- a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java +++ b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java @@ -9,7 +9,12 @@ public class VanillaPlus extends JavaPlugin { private static final String[] V = { "1.21.1", "1.21.3", "1.21.4" }; private static final String[] PAPER = { "Paper" }; private static final String IS_PAPER_MSG = "This plugin is not compatible with non-Paper servers."; - private static final String IS_SUPPORTED_VERSION_MSG = "This plugin requires Paper version: " + String.join(", ", V); + private static final String IS_SUPPORTED_VERSION_MSG = "This plugin requires Paper version(s): " + + String.join(", ", V); + + public interface MSG { + String PREFIX = "[VanillaPlus] "; + } public static VanillaPlus getInstance() { return getPlugin(VanillaPlus.class); diff --git a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java index 97bb50a..b3cf54d 100644 --- a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java +++ b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java @@ -6,7 +6,7 @@ import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.xodium.vanillaplus.VanillaPlus; -import org.xodium.vanillaplus.interfaces.MSG; +import org.xodium.vanillaplus.VanillaPlus.MSG; import org.xodium.vanillaplus.interfaces.PERMS; import com.mojang.brigadier.Command; import io.papermc.paper.command.brigadier.Commands; diff --git a/src/main/java/org/xodium/vanillaplus/interfaces/MSG.java b/src/main/java/org/xodium/vanillaplus/interfaces/MSG.java deleted file mode 100644 index 317406c..0000000 --- a/src/main/java/org/xodium/vanillaplus/interfaces/MSG.java +++ /dev/null @@ -1,6 +0,0 @@ -package org.xodium.vanillaplus.interfaces; - -public interface MSG { - String PREFIX = "[VanillaPlus] "; - String MODULE_LOADED = "Loaded: "; -} From 223ac4e42489be399c11f562c03aab6430128304 Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Tue, 10 Dec 2024 14:27:36 +0000 Subject: [PATCH 04/20] Refactor Modular interface and remove PERMS interface for improved modularity and clarity --- .../org/xodium/vanillaplus/ModuleManager.java | 7 +- .../org/xodium/vanillaplus/VanillaPlus.java | 4 +- .../vanillaplus/commands/ReloadCommand.java | 30 +++-- .../vanillaplus/interfaces/Modular.java | 6 +- .../xodium/vanillaplus/interfaces/PERMS.java | 13 -- .../vanillaplus/modules/DoorsModule.java | 127 ++++++++++-------- .../vanillaplus/modules/ElevatorModule.java | 9 +- src/main/resources/paper-plugin.yml | 6 +- 8 files changed, 102 insertions(+), 100 deletions(-) delete mode 100644 src/main/java/org/xodium/vanillaplus/interfaces/PERMS.java diff --git a/src/main/java/org/xodium/vanillaplus/ModuleManager.java b/src/main/java/org/xodium/vanillaplus/ModuleManager.java index 969b76b..dabf524 100644 --- a/src/main/java/org/xodium/vanillaplus/ModuleManager.java +++ b/src/main/java/org/xodium/vanillaplus/ModuleManager.java @@ -1,22 +1,21 @@ package org.xodium.vanillaplus; -import org.xodium.vanillaplus.VanillaPlus.MSG; import org.xodium.vanillaplus.interfaces.Modular; import org.xodium.vanillaplus.modules.DoorsModule; import java.util.List; -public class ModuleManager implements MSG { +public class ModuleManager { private final VanillaPlus vp = VanillaPlus.getInstance(); { List.of(new DoorsModule()) .stream() .peek(Modular::config) - .filter(Modular::isEnabled) + .filter(Modular::enabled) .forEach(module -> { vp.getServer().getPluginManager().registerEvents(module, vp); - vp.getLogger().info(PREFIX + "Loaded: " + module.getClass().getSimpleName()); + vp.getLogger().info(VanillaPlus.PREFIX + "Loaded: " + module.getClass().getSimpleName()); }); } } \ No newline at end of file diff --git a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java index 7d8f9db..d91544d 100644 --- a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java +++ b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java @@ -12,9 +12,7 @@ public class VanillaPlus extends JavaPlugin { private static final String IS_SUPPORTED_VERSION_MSG = "This plugin requires Paper version(s): " + String.join(", ", V); - public interface MSG { - String PREFIX = "[VanillaPlus] "; - } + public final static String PREFIX = "[VanillaPlus] "; public static VanillaPlus getInstance() { return getPlugin(VanillaPlus.class); diff --git a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java index b3cf54d..8e1ca51 100644 --- a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java +++ b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java @@ -6,21 +6,27 @@ import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.xodium.vanillaplus.VanillaPlus; -import org.xodium.vanillaplus.VanillaPlus.MSG; -import org.xodium.vanillaplus.interfaces.PERMS; import com.mojang.brigadier.Command; import io.papermc.paper.command.brigadier.Commands; import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; import net.kyori.adventure.text.minimessage.MiniMessage; -public class ReloadCommand implements MSG { - private final static VanillaPlus vp = VanillaPlus.getInstance(); - private final static PluginManager pm = vp.getServer().getPluginManager(); - private final static MiniMessage mm = MiniMessage.miniMessage(); +public class ReloadCommand { + private static final VanillaPlus vp = VanillaPlus.getInstance(); + private static final PluginManager pm = vp.getServer().getPluginManager(); + private static final String vpcn = vp.getClass().getSimpleName(); + private static final MiniMessage mm = MiniMessage.miniMessage(); - private static final String RELOAD_SUCC_LOG_MSG = "Reloaded successfully."; - private static final String RELOAD_SUCC_MSG = PREFIX + "Reloaded successfully."; - private static final String PERM_ERR_MSG = PREFIX + "You do not have permission to use this command!"; + private interface MSG { + String RELOAD_SUCC_LOG = "Reloaded successfully."; + String RELOAD_SUCC = VanillaPlus.PREFIX + "Reloaded successfully."; + String PERM_ERR = VanillaPlus.PREFIX + + "You do not have permission to use this command!"; + } + + private interface PERMS { + String RELOAD = vpcn + ".reload"; + } { vp.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, e -> { @@ -29,13 +35,13 @@ public class ReloadCommand implements MSG { .executes(ctx -> { CommandSender cs = ctx.getSource().getSender(); if (cs instanceof Player p && !p.hasPermission(PERMS.RELOAD)) { - p.sendMessage(mm.deserialize(PERM_ERR_MSG)); + p.sendMessage(mm.deserialize(MSG.PERM_ERR)); return 0; } pm.disablePlugin(vp); pm.enablePlugin(vp); - cs.sendMessage(mm.deserialize(RELOAD_SUCC_MSG)); - vp.getLogger().info(RELOAD_SUCC_LOG_MSG); + cs.sendMessage(mm.deserialize(MSG.RELOAD_SUCC)); + vp.getLogger().info(MSG.RELOAD_SUCC_LOG); return Command.SINGLE_SUCCESS; }) .build(), diff --git a/src/main/java/org/xodium/vanillaplus/interfaces/Modular.java b/src/main/java/org/xodium/vanillaplus/interfaces/Modular.java index 27a441a..fdca9f7 100644 --- a/src/main/java/org/xodium/vanillaplus/interfaces/Modular.java +++ b/src/main/java/org/xodium/vanillaplus/interfaces/Modular.java @@ -3,9 +3,11 @@ import org.bukkit.event.Listener; public interface Modular extends Listener { - String ENABLE = ".enable"; + interface CONFIG { + String ENABLE = ".enable"; + } - boolean isEnabled(); + boolean enabled(); void config(); } \ No newline at end of file diff --git a/src/main/java/org/xodium/vanillaplus/interfaces/PERMS.java b/src/main/java/org/xodium/vanillaplus/interfaces/PERMS.java deleted file mode 100644 index a92d148..0000000 --- a/src/main/java/org/xodium/vanillaplus/interfaces/PERMS.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.xodium.vanillaplus.interfaces; - -// TODO: refactor to each module for modularity. -public interface PERMS { - String VANILLAPLUS = "vanillaplus."; - String RELOAD = VANILLAPLUS + "reload"; - - interface DOORSMODULE { - String USE = VANILLAPLUS + "doubledoors"; - String KNOCK = VANILLAPLUS + "knock"; - String AUTOCLOSE = VANILLAPLUS + "autoclose"; - } -} diff --git a/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java b/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java index 641feb0..cd82f1e 100644 --- a/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java +++ b/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java @@ -33,38 +33,45 @@ import org.xodium.vanillaplus.Database; import org.xodium.vanillaplus.VanillaPlus; import org.xodium.vanillaplus.interfaces.Modular; -import org.xodium.vanillaplus.interfaces.PERMS; import org.xodium.vanillaplus.records.AdjacentBlockRecord; import com.google.common.base.Enums; // TODO: refactor. public class DoorsModule implements Modular { - private final String className = getClass().getSimpleName(); - private final VanillaPlus vp = VanillaPlus.getInstance(); - private final Database db = new Database(); - - // Sound settings - public static final String SOUND_KNOCK_CATEGORY = ".sound_knock_category"; - public static final String SOUND_KNOCK_PITCH = ".sound_knock_pitch"; - public static final String SOUND_KNOCK_VOLUME = ".sound_knock_volume"; - public static final String SOUND_KNOCK_WOOD = ".sound_knock_wood"; - - // Behavior settings - public static final String ALLOW_AUTOCLOSE = ".allow_autoclose"; - public static final String ALLOW_DOUBLEDOORS = ".allow_doubledoors"; - public static final String ALLOW_KNOCKING = ".allow_knocking"; - public static final String ALLOW_KNOCKING_GATES = ".allow_knocking_gates"; - public static final String ALLOW_KNOCKING_TRAPDOORS = ".allow_knocking_trapdoors"; - public static final String KNOCKING_REQUIRES_EMPTY_HAND = ".knocking_requires_empty_hand"; - public static final String KNOCKING_REQUIRES_SHIFT = ".knocking_requires_shift"; - - // Auto-close settings - public static final String AUTOCLOSE_DELAY = ".autoclose_delay"; - - // Others + private final String cn = getClass().getSimpleName(); + private static final VanillaPlus vp = VanillaPlus.getInstance(); + private static final String vpcn = vp.getClass().getSimpleName(); + private static final Database db = new Database(); + + private interface CONFIG extends Modular.CONFIG { + // Sound settings + String SOUND_KNOCK_CATEGORY = ".sound_knock_category"; + String SOUND_KNOCK_PITCH = ".sound_knock_pitch"; + String SOUND_KNOCK_VOLUME = ".sound_knock_volume"; + String SOUND_KNOCK_WOOD = ".sound_knock_wood"; + + // Behavior settings + String ALLOW_AUTOCLOSE = ".allow_autoclose"; + String ALLOW_DOUBLEDOORS = ".allow_doubledoors"; + String ALLOW_KNOCKING = ".allow_knocking"; + String ALLOW_KNOCKING_GATES = ".allow_knocking_gates"; + String ALLOW_KNOCKING_TRAPDOORS = ".allow_knocking_trapdoors"; + String KNOCKING_REQUIRES_EMPTY_HAND = ".knocking_requires_empty_hand"; + String KNOCKING_REQUIRES_SHIFT = ".knocking_requires_shift"; + + // Auto-close settings + String AUTOCLOSE_DELAY = ".autoclose_delay"; + } + + private interface PERMS { + String USE = vpcn + ".doubledoors"; + String KNOCK = vpcn + ".knock"; + String AUTOCLOSE = vpcn + ".autoclose"; + } + private final HashMap autoClose = new HashMap<>(); - private final static AdjacentBlockRecord[] POSSIBLE_NEIGHBOURS = { + private static final AdjacentBlockRecord[] POSSIBLE_NEIGHBOURS = { new AdjacentBlockRecord(0, -1, Door.Hinge.RIGHT, BlockFace.EAST), new AdjacentBlockRecord(0, 1, Door.Hinge.LEFT, BlockFace.EAST), @@ -119,14 +126,14 @@ public void onRightClick(PlayerInteractEvent e) { || e.getAction() != Action.RIGHT_CLICK_BLOCK || e.useInteractedBlock() == Event.Result.DENY || e.useItemInHand() == Event.Result.DENY - || !e.getPlayer().hasPermission(PERMS.DOORSMODULE.USE) + || !e.getPlayer().hasPermission(PERMS.USE) || !(blockData instanceof Door || blockData instanceof Gate)) return; - if (!db.getData(className + ALLOW_DOUBLEDOORS, Boolean.class)) { + if (!db.getData(cn + CONFIG.ALLOW_DOUBLEDOORS, Boolean.class)) { vp.getLogger() .warning("Double doors are disabled. ALLOW_DOUBLEDOORS value: " - + db.getData(className + ALLOW_DOUBLEDOORS, Boolean.class)); + + db.getData(cn + CONFIG.ALLOW_DOUBLEDOORS, Boolean.class)); return; } @@ -136,13 +143,17 @@ public void onRightClick(PlayerInteractEvent e) { if (otherDoorBlock != null && otherDoorBlock.getBlockData() instanceof Door) { Door otherDoor = (Door) otherDoorBlock.getBlockData(); this.toggleOtherDoor(clickedBlock, otherDoorBlock, !otherDoor.isOpen()); - autoClose.put(otherDoorBlock, - System.currentTimeMillis() - + db.getData(className + AUTOCLOSE_DELAY, Long.class) * 1000); + if (e.getPlayer().hasPermission(PERMS.AUTOCLOSE)) { + autoClose.put(otherDoorBlock, + System.currentTimeMillis() + + db.getData(cn + CONFIG.AUTOCLOSE_DELAY, Long.class) * 1000); + } } } - autoClose.put(clickedBlock, - System.currentTimeMillis() + db.getData(className + AUTOCLOSE_DELAY, Long.class) * 1000); + if (e.getPlayer().hasPermission(PERMS.AUTOCLOSE)) { + autoClose.put(clickedBlock, + System.currentTimeMillis() + db.getData(cn + CONFIG.AUTOCLOSE_DELAY, Long.class) * 1000); + } } @EventHandler @@ -153,14 +164,14 @@ public void onKnock(PlayerInteractEvent e) { if (gm == GameMode.CREATIVE || gm == GameMode.SPECTATOR) return; - if (!p.hasPermission(PERMS.DOORSMODULE.KNOCK) || e.getAction() != Action.LEFT_CLICK_BLOCK + if (!p.hasPermission(PERMS.KNOCK) || e.getAction() != Action.LEFT_CLICK_BLOCK || e.getHand() != EquipmentSlot.HAND) return; - if (db.getData(className + KNOCKING_REQUIRES_SHIFT, Boolean.class) && !p.isSneaking()) + if (db.getData(cn + CONFIG.KNOCKING_REQUIRES_SHIFT, Boolean.class) && !p.isSneaking()) return; - if (db.getData(className + KNOCKING_REQUIRES_EMPTY_HAND, Boolean.class) + if (db.getData(cn + CONFIG.KNOCKING_REQUIRES_EMPTY_HAND, Boolean.class) && p.getInventory().getItemInMainHand().getType() != Material.AIR) return; @@ -170,9 +181,9 @@ public void onKnock(PlayerInteractEvent e) { Block block = e.getClickedBlock(); BlockData blockData = block.getBlockData(); - if ((blockData instanceof Door && db.getData(className + ALLOW_KNOCKING, Boolean.class)) - || (blockData instanceof TrapDoor && db.getData(className + ALLOW_KNOCKING_TRAPDOORS, Boolean.class)) - || (blockData instanceof Gate && db.getData(className + ALLOW_KNOCKING_GATES, Boolean.class))) { + if ((blockData instanceof Door && db.getData(cn + CONFIG.ALLOW_KNOCKING, Boolean.class)) + || (blockData instanceof TrapDoor && db.getData(cn + CONFIG.ALLOW_KNOCKING_TRAPDOORS, Boolean.class)) + || (blockData instanceof Gate && db.getData(cn + CONFIG.ALLOW_KNOCKING_GATES, Boolean.class))) { this.playKnockSound(block); } } @@ -184,14 +195,14 @@ public void playKnockSound(Block block) { .ofNullable( Registry.SOUNDS .get(NamespacedKey.minecraft( - db.getData(className + SOUND_KNOCK_WOOD, String.class).toLowerCase()))) + db.getData(cn + CONFIG.SOUND_KNOCK_WOOD, String.class).toLowerCase()))) .orElse(Sound.ITEM_SHIELD_BLOCK); SoundCategory category = Enums .getIfPresent(SoundCategory.class, - db.getData(className + SOUND_KNOCK_CATEGORY, String.class).toUpperCase()) + db.getData(cn + CONFIG.SOUND_KNOCK_CATEGORY, String.class).toUpperCase()) .or(SoundCategory.BLOCKS); - float volume = db.getData(className + SOUND_KNOCK_VOLUME, Float.class); - float pitch = db.getData(className + SOUND_KNOCK_PITCH, Float.class); + float volume = db.getData(cn + CONFIG.SOUND_KNOCK_VOLUME, Float.class); + float pitch = db.getData(cn + CONFIG.SOUND_KNOCK_PITCH, Float.class); world.playSound(loc, sound, category, volume, pitch); } @@ -250,24 +261,24 @@ public void run() { } @Override - public boolean isEnabled() { - return db.getData(className + ENABLE, Boolean.class); + public boolean enabled() { + return db.getData(cn + CONFIG.ENABLE, Boolean.class); } @Override public void config() { - db.setData(className + ENABLE, true); - db.setData(className + SOUND_KNOCK_CATEGORY, "BLOCKS"); - db.setData(className + SOUND_KNOCK_PITCH, 1.0); - db.setData(className + SOUND_KNOCK_VOLUME, 1.0); - db.setData(className + SOUND_KNOCK_WOOD, "entity_zombie_attack_wooden_door"); - db.setData(className + ALLOW_AUTOCLOSE, true); - db.setData(className + ALLOW_DOUBLEDOORS, true); - db.setData(className + ALLOW_KNOCKING, true); - db.setData(className + ALLOW_KNOCKING_GATES, true); - db.setData(className + ALLOW_KNOCKING_TRAPDOORS, true); - db.setData(className + KNOCKING_REQUIRES_EMPTY_HAND, true); - db.setData(className + KNOCKING_REQUIRES_SHIFT, false); - db.setData(className + AUTOCLOSE_DELAY, 6); + db.setData(cn + CONFIG.ENABLE, true); + db.setData(cn + CONFIG.SOUND_KNOCK_CATEGORY, "BLOCKS"); + db.setData(cn + CONFIG.SOUND_KNOCK_PITCH, 1.0); + db.setData(cn + CONFIG.SOUND_KNOCK_VOLUME, 1.0); + db.setData(cn + CONFIG.SOUND_KNOCK_WOOD, "entity_zombie_attack_wooden_door"); + db.setData(cn + CONFIG.ALLOW_AUTOCLOSE, true); + db.setData(cn + CONFIG.ALLOW_DOUBLEDOORS, true); + db.setData(cn + CONFIG.ALLOW_KNOCKING, true); + db.setData(cn + CONFIG.ALLOW_KNOCKING_GATES, true); + db.setData(cn + CONFIG.ALLOW_KNOCKING_TRAPDOORS, true); + db.setData(cn + CONFIG.KNOCKING_REQUIRES_EMPTY_HAND, true); + db.setData(cn + CONFIG.KNOCKING_REQUIRES_SHIFT, false); + db.setData(cn + CONFIG.AUTOCLOSE_DELAY, 6); } } diff --git a/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java b/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java index 6668728..b666bdf 100644 --- a/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java +++ b/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java @@ -4,17 +4,16 @@ import org.xodium.vanillaplus.interfaces.Modular; public class ElevatorModule implements Modular { - private final String className = getClass().getSimpleName(); + private final String cn = getClass().getSimpleName(); private final Database db = new Database(); @Override - public boolean isEnabled() { - return db.getData(className + ENABLE, Boolean.class); + public boolean enabled() { + return db.getData(cn + CONFIG.ENABLE, Boolean.class); } @Override public void config() { - db.setData(className + ENABLE, true); + db.setData(cn + CONFIG.ENABLE, true); } - } diff --git a/src/main/resources/paper-plugin.yml b/src/main/resources/paper-plugin.yml index 3c0d960..71f34c8 100644 --- a/src/main/resources/paper-plugin.yml +++ b/src/main/resources/paper-plugin.yml @@ -6,12 +6,12 @@ api-version: 1.21.3 authors: - XodiumSoftware permissions: - vanillaplus.doubledoors: + VanillaPlus.doubledoors: description: Allows to open double doors with one click default: true - vanillaplus.knock: + VanillaPlus.knock: description: Allows to knock on doors using left-click default: true - vanillaplus.autoclose: + VanillaPlus.autoclose: description: Allows players to have their doors close automatically default: true From 4f84438985ad99095e7e7e8f93ec2e7adcb42e74 Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Tue, 10 Dec 2024 14:41:33 +0000 Subject: [PATCH 05/20] Refactor DoorsModule to improve permission handling and simplify condition checks --- .../vanillaplus/modules/DoorsModule.java | 42 +++++++------------ 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java b/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java index cd82f1e..082a54c 100644 --- a/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java +++ b/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java @@ -41,7 +41,6 @@ public class DoorsModule implements Modular { private final String cn = getClass().getSimpleName(); private static final VanillaPlus vp = VanillaPlus.getInstance(); - private static final String vpcn = vp.getClass().getSimpleName(); private static final Database db = new Database(); private interface CONFIG extends Modular.CONFIG { @@ -65,9 +64,9 @@ private interface CONFIG extends Modular.CONFIG { } private interface PERMS { - String USE = vpcn + ".doubledoors"; - String KNOCK = vpcn + ".knock"; - String AUTOCLOSE = vpcn + ".autoclose"; + String USE = vp.getClass().getSimpleName() + ".doubledoors"; + String KNOCK = vp.getClass().getSimpleName() + ".knock"; + String AUTOCLOSE = vp.getClass().getSimpleName() + ".autoclose"; } private final HashMap autoClose = new HashMap<>(); @@ -127,16 +126,10 @@ public void onRightClick(PlayerInteractEvent e) { || e.useInteractedBlock() == Event.Result.DENY || e.useItemInHand() == Event.Result.DENY || !e.getPlayer().hasPermission(PERMS.USE) - || !(blockData instanceof Door || blockData instanceof Gate)) + || !(blockData instanceof Door || blockData instanceof Gate) + || !db.getData(cn + CONFIG.ALLOW_DOUBLEDOORS, Boolean.class)) return; - if (!db.getData(cn + CONFIG.ALLOW_DOUBLEDOORS, Boolean.class)) { - vp.getLogger() - .warning("Double doors are disabled. ALLOW_DOUBLEDOORS value: " - + db.getData(cn + CONFIG.ALLOW_DOUBLEDOORS, Boolean.class)); - return; - } - if (blockData instanceof Door) { Door door = this.getBottomDoor((Door) blockData, clickedBlock); Block otherDoorBlock = this.getOtherPart(door, clickedBlock); @@ -161,22 +154,17 @@ public void onKnock(PlayerInteractEvent e) { Player p = e.getPlayer(); GameMode gm = p.getGameMode(); - if (gm == GameMode.CREATIVE || gm == GameMode.SPECTATOR) - return; - - if (!p.hasPermission(PERMS.KNOCK) || e.getAction() != Action.LEFT_CLICK_BLOCK - || e.getHand() != EquipmentSlot.HAND) - return; - - if (db.getData(cn + CONFIG.KNOCKING_REQUIRES_SHIFT, Boolean.class) && !p.isSneaking()) - return; - - if (db.getData(cn + CONFIG.KNOCKING_REQUIRES_EMPTY_HAND, Boolean.class) - && p.getInventory().getItemInMainHand().getType() != Material.AIR) - return; - - if (e.getClickedBlock() == null) + if ((gm == GameMode.CREATIVE + || gm == GameMode.SPECTATOR) + || (!p.hasPermission(PERMS.KNOCK) + || e.getAction() != Action.LEFT_CLICK_BLOCK + || e.getHand() != EquipmentSlot.HAND) + || (db.getData(cn + CONFIG.KNOCKING_REQUIRES_SHIFT, Boolean.class) && !p.isSneaking()) + || (db.getData(cn + CONFIG.KNOCKING_REQUIRES_EMPTY_HAND, Boolean.class) + && p.getInventory().getItemInMainHand().getType() != Material.AIR) + || (e.getClickedBlock() == null)) { return; + } Block block = e.getClickedBlock(); BlockData blockData = block.getBlockData(); From e766afbe994aa4b51edbfa7c114dde0354d4a6c5 Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Tue, 10 Dec 2024 15:38:45 +0000 Subject: [PATCH 06/20] Bump version to 1.2.0 and refactor interfaces for improved modularity --- build.gradle.kts | 2 +- .../org/xodium/vanillaplus/ModuleManager.java | 8 +-- .../vanillaplus/commands/ReloadCommand.java | 38 +++++++------ .../xodium/vanillaplus/gui/SettingsGUI.java | 57 +++++++++++++++++++ .../{Modular.java => ModuleInterface.java} | 2 +- .../vanillaplus/modules/DoorsModule.java | 6 +- .../vanillaplus/modules/ElevatorModule.java | 4 +- src/main/resources/paper-plugin.yml | 4 +- 8 files changed, 91 insertions(+), 30 deletions(-) create mode 100644 src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java rename src/main/java/org/xodium/vanillaplus/interfaces/{Modular.java => ModuleInterface.java} (78%) diff --git a/build.gradle.kts b/build.gradle.kts index a156496..dedee55 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,7 +3,7 @@ plugins { } group = "org.xodium.vanillaplus" -version = "1.1.4" +version = "1.2.0" description = "Minecraft plugin that enhances the base gameplay." repositories { diff --git a/src/main/java/org/xodium/vanillaplus/ModuleManager.java b/src/main/java/org/xodium/vanillaplus/ModuleManager.java index dabf524..85ca2f1 100644 --- a/src/main/java/org/xodium/vanillaplus/ModuleManager.java +++ b/src/main/java/org/xodium/vanillaplus/ModuleManager.java @@ -1,6 +1,6 @@ package org.xodium.vanillaplus; -import org.xodium.vanillaplus.interfaces.Modular; +import org.xodium.vanillaplus.interfaces.ModuleInterface; import org.xodium.vanillaplus.modules.DoorsModule; import java.util.List; @@ -11,11 +11,11 @@ public class ModuleManager { { List.of(new DoorsModule()) .stream() - .peek(Modular::config) - .filter(Modular::enabled) + .peek(ModuleInterface::config) + .filter(ModuleInterface::enabled) .forEach(module -> { vp.getServer().getPluginManager().registerEvents(module, vp); - vp.getLogger().info(VanillaPlus.PREFIX + "Loaded: " + module.getClass().getSimpleName()); + vp.getLogger().info("Loaded: " + module.getClass().getSimpleName()); }); } } \ No newline at end of file diff --git a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java index 8e1ca51..28caa89 100644 --- a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java +++ b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java @@ -4,28 +4,31 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.bukkit.plugin.PluginManager; +import org.jetbrains.annotations.NotNull; import org.xodium.vanillaplus.VanillaPlus; +import org.xodium.vanillaplus.gui.SettingsGUI; + import com.mojang.brigadier.Command; import io.papermc.paper.command.brigadier.Commands; import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; +import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; public class ReloadCommand { private static final VanillaPlus vp = VanillaPlus.getInstance(); - private static final PluginManager pm = vp.getServer().getPluginManager(); - private static final String vpcn = vp.getClass().getSimpleName(); private static final MiniMessage mm = MiniMessage.miniMessage(); private interface MSG { - String RELOAD_SUCC_LOG = "Reloaded successfully."; - String RELOAD_SUCC = VanillaPlus.PREFIX + "Reloaded successfully."; - String PERM_ERR = VanillaPlus.PREFIX - + "You do not have permission to use this command!"; + @NotNull + Component PERM_ERR = mm.deserialize(VanillaPlus.PREFIX + + "You do not have permission to use this command!"); + @NotNull + Component PLAYER_ONLY_CMD = mm + .deserialize("This command can only be run by a player."); } private interface PERMS { - String RELOAD = vpcn + ".reload"; + String RELOAD = vp.getClass().getSimpleName() + ".reload"; } { @@ -34,18 +37,19 @@ private interface PERMS { Commands.literal("vanillaplus") .executes(ctx -> { CommandSender cs = ctx.getSource().getSender(); - if (cs instanceof Player p && !p.hasPermission(PERMS.RELOAD)) { - p.sendMessage(mm.deserialize(MSG.PERM_ERR)); - return 0; + if (cs instanceof Player p) { + if (!p.hasPermission(PERMS.RELOAD)) { + p.sendMessage(MSG.PERM_ERR); + return 0; + } + SettingsGUI.openInventory(p); + return Command.SINGLE_SUCCESS; } - pm.disablePlugin(vp); - pm.enablePlugin(vp); - cs.sendMessage(mm.deserialize(MSG.RELOAD_SUCC)); - vp.getLogger().info(MSG.RELOAD_SUCC_LOG); - return Command.SINGLE_SUCCESS; + cs.sendMessage(MSG.PLAYER_ONLY_CMD); + return 0; }) .build(), - "Reloads VanillaPlus", + "Opens the GUI", List.of("vp")); }); } diff --git a/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java b/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java new file mode 100644 index 0000000..b819c6a --- /dev/null +++ b/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java @@ -0,0 +1,57 @@ +package org.xodium.vanillaplus.gui; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.Listener; +import org.bukkit.event.EventHandler; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.Material; +import org.bukkit.inventory.meta.ItemMeta; +import net.kyori.adventure.text.Component; + +public class SettingsGUI implements Listener { + + private static Inventory inv; + + public SettingsGUI() { + inv = Bukkit.createInventory(null, 9, Component.text("Settings")); + initializeItems(); + } + + private void initializeItems() { + inv.setItem(0, createGuiItem(Material.COMPASS, "Option 1")); + inv.setItem(1, createGuiItem(Material.REDSTONE, "Option 2")); + // Add more items as needed + } + + private ItemStack createGuiItem(Material m, String name) { + ItemStack is = new ItemStack(m, 1); + ItemMeta im = is.getItemMeta(); + im.displayName(Component.text(name)); + is.setItemMeta(im); + return is; + } + + public static void openInventory(Player p) { + p.openInventory(inv); + } + + @EventHandler + public void onInventoryClick(InventoryClickEvent e) { + if (!e.getInventory().equals(inv)) + return; + e.setCancelled(true); + ItemStack is = e.getCurrentItem(); + if (is == null || is.getType() == Material.AIR) + return; + + Component name = is.getItemMeta().displayName(); + if (name.equals("Option 1")) { + // Handle Option 1 + } else if (name.equals("Option 2")) { + // Handle Option 2 + } + } +} \ No newline at end of file diff --git a/src/main/java/org/xodium/vanillaplus/interfaces/Modular.java b/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java similarity index 78% rename from src/main/java/org/xodium/vanillaplus/interfaces/Modular.java rename to src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java index fdca9f7..61a68d9 100644 --- a/src/main/java/org/xodium/vanillaplus/interfaces/Modular.java +++ b/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java @@ -2,7 +2,7 @@ import org.bukkit.event.Listener; -public interface Modular extends Listener { +public interface ModuleInterface extends Listener { interface CONFIG { String ENABLE = ".enable"; } diff --git a/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java b/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java index 082a54c..af46f26 100644 --- a/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java +++ b/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java @@ -32,18 +32,18 @@ import org.bukkit.scheduler.BukkitRunnable; import org.xodium.vanillaplus.Database; import org.xodium.vanillaplus.VanillaPlus; -import org.xodium.vanillaplus.interfaces.Modular; +import org.xodium.vanillaplus.interfaces.ModuleInterface; import org.xodium.vanillaplus.records.AdjacentBlockRecord; import com.google.common.base.Enums; // TODO: refactor. -public class DoorsModule implements Modular { +public class DoorsModule implements ModuleInterface { private final String cn = getClass().getSimpleName(); private static final VanillaPlus vp = VanillaPlus.getInstance(); private static final Database db = new Database(); - private interface CONFIG extends Modular.CONFIG { + private interface CONFIG extends ModuleInterface.CONFIG { // Sound settings String SOUND_KNOCK_CATEGORY = ".sound_knock_category"; String SOUND_KNOCK_PITCH = ".sound_knock_pitch"; diff --git a/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java b/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java index b666bdf..ea601d6 100644 --- a/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java +++ b/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java @@ -1,9 +1,9 @@ package org.xodium.vanillaplus.modules; import org.xodium.vanillaplus.Database; -import org.xodium.vanillaplus.interfaces.Modular; +import org.xodium.vanillaplus.interfaces.ModuleInterface; -public class ElevatorModule implements Modular { +public class ElevatorModule implements ModuleInterface { private final String cn = getClass().getSimpleName(); private final Database db = new Database(); diff --git a/src/main/resources/paper-plugin.yml b/src/main/resources/paper-plugin.yml index 71f34c8..fafb1ab 100644 --- a/src/main/resources/paper-plugin.yml +++ b/src/main/resources/paper-plugin.yml @@ -1,8 +1,8 @@ name: VanillaPlus -version: 1.1.4 +version: 1.2.0 main: org.xodium.vanillaplus.VanillaPlus description: Minecraft plugin that enhances the base gameplay. -api-version: 1.21.3 +api-version: 1.21.4 authors: - XodiumSoftware permissions: From fd5a7b6767180464eb7adb52502fe083c03b6edd Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Tue, 10 Dec 2024 15:46:58 +0000 Subject: [PATCH 07/20] Refactor SettingsGUI to improve inventory initialization and item handling --- .../xodium/vanillaplus/gui/SettingsGUI.java | 92 +++++++++++++++---- 1 file changed, 72 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java b/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java index b819c6a..dc1b1f0 100644 --- a/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java +++ b/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java @@ -11,47 +11,99 @@ import org.bukkit.inventory.meta.ItemMeta; import net.kyori.adventure.text.Component; +/** + * A simple GUI for plugin settings. + */ public class SettingsGUI implements Listener { + private static final String GUI_TITLE = "Settings"; + private static final Component OPTION_1_NAME = Component.text("Option 1"); + private static final Component OPTION_2_NAME = Component.text("Option 2"); + private static Inventory inv; - public SettingsGUI() { - inv = Bukkit.createInventory(null, 9, Component.text("Settings")); - initializeItems(); + /** + * Opens the settings inventory for a player. + * + * @param p the player. + */ + public static void openInventory(Player p) { + if (inv == null) { + initInventory(); + } + p.openInventory(inv); + } + + /** + * Initializes the inventory if it hasn't been created yet. + */ + private static void initInventory() { + inv = Bukkit.createInventory(null, 9, Component.text(GUI_TITLE)); + initItems(); } - private void initializeItems() { - inv.setItem(0, createGuiItem(Material.COMPASS, "Option 1")); - inv.setItem(1, createGuiItem(Material.REDSTONE, "Option 2")); - // Add more items as needed + /** + * Populates the inventory with items. + */ + private static void initItems() { + inv.setItem(0, createItem(Material.COMPASS, OPTION_1_NAME)); + inv.setItem(1, createItem(Material.REDSTONE, OPTION_2_NAME)); } - private ItemStack createGuiItem(Material m, String name) { + /** + * Creates a GUI item. + * + * @param m the material. + * @param name the display name. + * @return the ItemStack. + */ + private static ItemStack createItem(Material m, Component name) { ItemStack is = new ItemStack(m, 1); ItemMeta im = is.getItemMeta(); - im.displayName(Component.text(name)); + im.displayName(name); is.setItemMeta(im); return is; } - public static void openInventory(Player p) { - p.openInventory(inv); - } - + /** + * Handles clicks in the settings inventory. + * + * @param e the click event. + */ @EventHandler public void onInventoryClick(InventoryClickEvent e) { - if (!e.getInventory().equals(inv)) + if (!isSettingsInventory(e)) return; + e.setCancelled(true); + ItemStack is = e.getCurrentItem(); if (is == null || is.getType() == Material.AIR) return; - Component name = is.getItemMeta().displayName(); - if (name.equals("Option 1")) { - // Handle Option 1 - } else if (name.equals("Option 2")) { - // Handle Option 2 + handleClick(is.getItemMeta().displayName()); + } + + /** + * Checks if the inventory is the settings inventory. + * + * @param e the click event. + * @return true if it is; false otherwise. + */ + private static boolean isSettingsInventory(InventoryClickEvent e) { + return e.getInventory().equals(inv); + } + + /** + * Handles item click logic. + * + * @param name the clicked item's display name. + */ + private static void handleClick(Component name) { + if (OPTION_1_NAME.equals(name)) { + // TODO: Implement Option 1 logic. + } else if (OPTION_2_NAME.equals(name)) { + // TODO: Implement Option 2 logic. } } -} \ No newline at end of file +} From 94e079bb3fcdc9d63524405b6aafde0371fb27a3 Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Wed, 11 Dec 2024 11:35:01 +0000 Subject: [PATCH 08/20] Refactor DoorsModule to simplify permission checks and improve code readability --- README.md | 63 +++++++++++++++++++ .../vanillaplus/modules/DoorsModule.java | 14 ++--- 2 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..7226cfb --- /dev/null +++ b/README.md @@ -0,0 +1,63 @@ + + +

+
+ + VanillaPlus Logo + +

+ VanillaPlus +
+
+

+ +

Minecraft plugin that enhances the base gameplay.


+ +
+ +[![Contributors][contributors_shield_url]][contributors_url] +[![Issues][issues_shield_url]][issues_url] +[![Roadmap][roadmap_shield_url]][roadmap_url]
+ +## Table of Contents + +- [About The Project](#about-the-project) +- [Code of Conduct][code_of_conduct_url] +- [Contributing][contributing_url] +- [License][license_url] + +## About The Project + + + +

Built With

+ +[![Built With][built_with_shield_url]][built_with_url]
+ +

+ +[built_with_shield_url]: https://skillicons.dev/icons?i=vscode,docker,java,gradle,sqlite,github,githubactions +[built_with_url]: https://skillicons.dev +[code_of_conduct_url]: https://github.com/XodiumSoftware/xCLOUD?tab=coc-ov-file +[contributing_url]: https://github.com/XodiumSoftware/xCLOUD/blob/main/CONTRIBUTING.md +[contributors_shield_url]: https://img.shields.io/github/contributors/XodiumSoftware/xCLOUD?style=for-the-badge&color=blue +[contributors_url]: https://github.com/XodiumSoftware/xCLOUD/graphs/contributors +[issues_shield_url]: https://img.shields.io/github/issues/XodiumSoftware/xCLOUD?style=for-the-badge&color=yellow +[issues_url]: https://github.com/XodiumSoftware/xCLOUD/issues +[license_url]: https://github.com/XodiumSoftware/xCLOUD?tab=AGPL-3.0-1-ov-file +[roadmap_shield_url]: https://img.shields.io/badge/Roadmap-Click%20Me!-purple.svg?style=for-the-badge +[roadmap_url]: https://github.com/orgs/XodiumSoftware/projects/4 diff --git a/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java b/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java index af46f26..7b8458e 100644 --- a/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java +++ b/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java @@ -152,19 +152,15 @@ public void onRightClick(PlayerInteractEvent e) { @EventHandler public void onKnock(PlayerInteractEvent e) { Player p = e.getPlayer(); - GameMode gm = p.getGameMode(); - if ((gm == GameMode.CREATIVE - || gm == GameMode.SPECTATOR) - || (!p.hasPermission(PERMS.KNOCK) - || e.getAction() != Action.LEFT_CLICK_BLOCK - || e.getHand() != EquipmentSlot.HAND) + if (p.getGameMode() == GameMode.CREATIVE || p.getGameMode() == GameMode.SPECTATOR + || !p.hasPermission(PERMS.KNOCK) || e.getAction() != Action.LEFT_CLICK_BLOCK + || e.getHand() != EquipmentSlot.HAND || (db.getData(cn + CONFIG.KNOCKING_REQUIRES_SHIFT, Boolean.class) && !p.isSneaking()) || (db.getData(cn + CONFIG.KNOCKING_REQUIRES_EMPTY_HAND, Boolean.class) && p.getInventory().getItemInMainHand().getType() != Material.AIR) - || (e.getClickedBlock() == null)) { + || e.getClickedBlock() == null) return; - } Block block = e.getClickedBlock(); BlockData blockData = block.getBlockData(); @@ -172,7 +168,7 @@ public void onKnock(PlayerInteractEvent e) { if ((blockData instanceof Door && db.getData(cn + CONFIG.ALLOW_KNOCKING, Boolean.class)) || (blockData instanceof TrapDoor && db.getData(cn + CONFIG.ALLOW_KNOCKING_TRAPDOORS, Boolean.class)) || (blockData instanceof Gate && db.getData(cn + CONFIG.ALLOW_KNOCKING_GATES, Boolean.class))) { - this.playKnockSound(block); + playKnockSound(block); } } From cf856e3ad022371dc90562706efcf96786aba8e6 Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Wed, 11 Dec 2024 13:07:50 +0000 Subject: [PATCH 09/20] Refactor code to improve consistency and readability by changing instance variables to static final where appropriate and updating inventory handling in SettingsGUI --- .../java/org/xodium/vanillaplus/Database.java | 8 +- .../org/xodium/vanillaplus/ModuleManager.java | 10 +- .../org/xodium/vanillaplus/VanillaPlus.java | 3 +- .../vanillaplus/commands/ReloadCommand.java | 18 ++-- .../xodium/vanillaplus/gui/SettingsGUI.java | 91 +++++++++---------- .../interfaces/ModuleInterface.java | 2 +- .../vanillaplus/modules/DoorsModule.java | 70 +++++++------- .../vanillaplus/modules/ElevatorModule.java | 6 +- 8 files changed, 101 insertions(+), 107 deletions(-) diff --git a/src/main/java/org/xodium/vanillaplus/Database.java b/src/main/java/org/xodium/vanillaplus/Database.java index 3406d74..0b5dd38 100644 --- a/src/main/java/org/xodium/vanillaplus/Database.java +++ b/src/main/java/org/xodium/vanillaplus/Database.java @@ -14,7 +14,7 @@ public class Database { private Connection conn; - private final VanillaPlus vp = VanillaPlus.getInstance(); + private static final VanillaPlus VP = VanillaPlus.getInstance(); private static final String DB_URL_PREFIX = "jdbc:sqlite:"; private static final String DB_FILE = "vanillaplus.db"; @@ -39,11 +39,11 @@ public class Database { } }; - public Database() { + { try { - vp.getDataFolder().mkdirs(); + VP.getDataFolder().mkdirs(); conn = DriverManager - .getConnection(DB_URL_PREFIX + new File(vp.getDataFolder(), DB_FILE).getAbsolutePath()); + .getConnection(DB_URL_PREFIX + new File(VP.getDataFolder(), DB_FILE).getAbsolutePath()); initTables(); } catch (SQLException err) { err.printStackTrace(); diff --git a/src/main/java/org/xodium/vanillaplus/ModuleManager.java b/src/main/java/org/xodium/vanillaplus/ModuleManager.java index 85ca2f1..d73e089 100644 --- a/src/main/java/org/xodium/vanillaplus/ModuleManager.java +++ b/src/main/java/org/xodium/vanillaplus/ModuleManager.java @@ -6,16 +6,16 @@ import java.util.List; public class ModuleManager { - private final VanillaPlus vp = VanillaPlus.getInstance(); + private static final VanillaPlus VP = VanillaPlus.getInstance(); - { + static { List.of(new DoorsModule()) .stream() .peek(ModuleInterface::config) .filter(ModuleInterface::enabled) - .forEach(module -> { - vp.getServer().getPluginManager().registerEvents(module, vp); - vp.getLogger().info("Loaded: " + module.getClass().getSimpleName()); + .forEach(mod -> { + VP.getServer().getPluginManager().registerEvents(mod, VP); + VP.getLogger().info("Loaded: " + mod.getClass().getSimpleName()); }); } } \ No newline at end of file diff --git a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java index d91544d..ec37839 100644 --- a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java +++ b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java @@ -12,7 +12,8 @@ public class VanillaPlus extends JavaPlugin { private static final String IS_SUPPORTED_VERSION_MSG = "This plugin requires Paper version(s): " + String.join(", ", V); - public final static String PREFIX = "[VanillaPlus] "; + public static final String PREFIX = "[VanillaPlus] "; + public static final String MM_HEX_PREFIX = "<#CB2D3E>"; public static VanillaPlus getInstance() { return getPlugin(VanillaPlus.class); diff --git a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java index 28caa89..bf41f58 100644 --- a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java +++ b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java @@ -15,24 +15,24 @@ import net.kyori.adventure.text.minimessage.MiniMessage; public class ReloadCommand { - private static final VanillaPlus vp = VanillaPlus.getInstance(); - private static final MiniMessage mm = MiniMessage.miniMessage(); + private static final VanillaPlus VP = VanillaPlus.getInstance(); + private static final MiniMessage MM = MiniMessage.miniMessage(); - private interface MSG { + private static interface MSG { @NotNull - Component PERM_ERR = mm.deserialize(VanillaPlus.PREFIX + Component PERM_ERR = MM.deserialize(VanillaPlus.PREFIX + "You do not have permission to use this command!"); @NotNull - Component PLAYER_ONLY_CMD = mm + Component PLAYER_ONLY_CMD = MM .deserialize("This command can only be run by a player."); } - private interface PERMS { - String RELOAD = vp.getClass().getSimpleName() + ".reload"; + private static interface PERMS { + String RELOAD = VP.getClass().getSimpleName() + ".reload"; } - { - vp.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, e -> { + static { + VP.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, e -> { e.registrar().register( Commands.literal("vanillaplus") .executes(ctx -> { diff --git a/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java b/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java index dc1b1f0..bc79143 100644 --- a/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java +++ b/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java @@ -1,5 +1,7 @@ package org.xodium.vanillaplus.gui; +import java.util.Map; + import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.Listener; @@ -9,45 +11,59 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.Material; import org.bukkit.inventory.meta.ItemMeta; +import org.xodium.vanillaplus.Database; + import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; /** * A simple GUI for plugin settings. */ public class SettingsGUI implements Listener { + private static final Database DB = new Database(); + private static final MiniMessage MM = MiniMessage.miniMessage(); + private static final Component GUI_TITLE = MM.deserialize("Settings"); + private static final Inventory INV = Bukkit.createInventory(null, 9, GUI_TITLE); - private static final String GUI_TITLE = "Settings"; - private static final Component OPTION_1_NAME = Component.text("Option 1"); - private static final Component OPTION_2_NAME = Component.text("Option 2"); - - private static Inventory inv; + static { + populateInventory(); + } /** - * Opens the settings inventory for a player. - * - * @param p the player. + * Loads settings from the SQLite database and populates the inventory. */ - public static void openInventory(Player p) { - if (inv == null) { - initInventory(); + private static void populateInventory() { + Map settings = DB.getAllSettings(); + int slot = 0; + for (Map.Entry entry : settings.entrySet()) { + if (slot >= INV.getSize()) + break; + String name = entry.getKey(); + String material = entry.getValue(); + Material mat = Material.matchMaterial(material); + if (mat != null) { + INV.setItem(slot++, createItem(mat, MM.deserialize(name))); + } } - p.openInventory(inv); } /** - * Initializes the inventory if it hasn't been created yet. + * Updates a setting in the SQLite database. + * + * @param name the setting name. + * @param material the material associated with the setting. */ - private static void initInventory() { - inv = Bukkit.createInventory(null, 9, Component.text(GUI_TITLE)); - initItems(); + private static void updateSettingInDatabase(String name, String material) { + DB.setData(name, material, false); } /** - * Populates the inventory with items. + * Opens the settings inventory for a player. + * + * @param p the player. */ - private static void initItems() { - inv.setItem(0, createItem(Material.COMPASS, OPTION_1_NAME)); - inv.setItem(1, createItem(Material.REDSTONE, OPTION_2_NAME)); + public static void openInventory(Player p) { + p.openInventory(INV); } /** @@ -68,42 +84,19 @@ private static ItemStack createItem(Material m, Component name) { /** * Handles clicks in the settings inventory. * - * @param e the click event. + * @param event the click event. */ @EventHandler public void onInventoryClick(InventoryClickEvent e) { - if (!isSettingsInventory(e)) + if (!e.getInventory().equals(INV)) return; e.setCancelled(true); - - ItemStack is = e.getCurrentItem(); - if (is == null || is.getType() == Material.AIR) + ItemStack clickedItem = e.getCurrentItem(); + if (clickedItem == null || clickedItem.getType() == Material.AIR) return; - handleClick(is.getItemMeta().displayName()); - } - - /** - * Checks if the inventory is the settings inventory. - * - * @param e the click event. - * @return true if it is; false otherwise. - */ - private static boolean isSettingsInventory(InventoryClickEvent e) { - return e.getInventory().equals(inv); - } - - /** - * Handles item click logic. - * - * @param name the clicked item's display name. - */ - private static void handleClick(Component name) { - if (OPTION_1_NAME.equals(name)) { - // TODO: Implement Option 1 logic. - } else if (OPTION_2_NAME.equals(name)) { - // TODO: Implement Option 2 logic. - } + updateSettingInDatabase(MM.serialize(clickedItem.getItemMeta().displayName()), clickedItem.getType().name()); + // TODO: Provide feedback to the player, e.g., via chat or title. } } diff --git a/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java b/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java index 61a68d9..0483b53 100644 --- a/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java +++ b/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java @@ -3,7 +3,7 @@ import org.bukkit.event.Listener; public interface ModuleInterface extends Listener { - interface CONFIG { + static interface CONFIG { String ENABLE = ".enable"; } diff --git a/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java b/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java index 7b8458e..f6fefab 100644 --- a/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java +++ b/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java @@ -40,10 +40,10 @@ // TODO: refactor. public class DoorsModule implements ModuleInterface { private final String cn = getClass().getSimpleName(); - private static final VanillaPlus vp = VanillaPlus.getInstance(); - private static final Database db = new Database(); + private static final VanillaPlus VP = VanillaPlus.getInstance(); + private static final Database DB = new Database(); - private interface CONFIG extends ModuleInterface.CONFIG { + private static interface CONFIG extends ModuleInterface.CONFIG { // Sound settings String SOUND_KNOCK_CATEGORY = ".sound_knock_category"; String SOUND_KNOCK_PITCH = ".sound_knock_pitch"; @@ -63,10 +63,10 @@ private interface CONFIG extends ModuleInterface.CONFIG { String AUTOCLOSE_DELAY = ".autoclose_delay"; } - private interface PERMS { - String USE = vp.getClass().getSimpleName() + ".doubledoors"; - String KNOCK = vp.getClass().getSimpleName() + ".knock"; - String AUTOCLOSE = vp.getClass().getSimpleName() + ".autoclose"; + private static interface PERMS { + String USE = VP.getClass().getSimpleName() + ".doubledoors"; + String KNOCK = VP.getClass().getSimpleName() + ".knock"; + String AUTOCLOSE = VP.getClass().getSimpleName() + ".autoclose"; } private final HashMap autoClose = new HashMap<>(); @@ -85,7 +85,7 @@ private interface PERMS { }; { - Bukkit.getScheduler().runTaskTimer(vp, () -> { + Bukkit.getScheduler().runTaskTimer(VP, () -> { Iterator> it = autoClose.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = it.next(); @@ -127,7 +127,7 @@ public void onRightClick(PlayerInteractEvent e) { || e.useItemInHand() == Event.Result.DENY || !e.getPlayer().hasPermission(PERMS.USE) || !(blockData instanceof Door || blockData instanceof Gate) - || !db.getData(cn + CONFIG.ALLOW_DOUBLEDOORS, Boolean.class)) + || !DB.getData(cn + CONFIG.ALLOW_DOUBLEDOORS, Boolean.class)) return; if (blockData instanceof Door) { @@ -139,13 +139,13 @@ public void onRightClick(PlayerInteractEvent e) { if (e.getPlayer().hasPermission(PERMS.AUTOCLOSE)) { autoClose.put(otherDoorBlock, System.currentTimeMillis() - + db.getData(cn + CONFIG.AUTOCLOSE_DELAY, Long.class) * 1000); + + DB.getData(cn + CONFIG.AUTOCLOSE_DELAY, Long.class) * 1000); } } } if (e.getPlayer().hasPermission(PERMS.AUTOCLOSE)) { autoClose.put(clickedBlock, - System.currentTimeMillis() + db.getData(cn + CONFIG.AUTOCLOSE_DELAY, Long.class) * 1000); + System.currentTimeMillis() + DB.getData(cn + CONFIG.AUTOCLOSE_DELAY, Long.class) * 1000); } } @@ -156,8 +156,8 @@ public void onKnock(PlayerInteractEvent e) { if (p.getGameMode() == GameMode.CREATIVE || p.getGameMode() == GameMode.SPECTATOR || !p.hasPermission(PERMS.KNOCK) || e.getAction() != Action.LEFT_CLICK_BLOCK || e.getHand() != EquipmentSlot.HAND - || (db.getData(cn + CONFIG.KNOCKING_REQUIRES_SHIFT, Boolean.class) && !p.isSneaking()) - || (db.getData(cn + CONFIG.KNOCKING_REQUIRES_EMPTY_HAND, Boolean.class) + || (DB.getData(cn + CONFIG.KNOCKING_REQUIRES_SHIFT, Boolean.class) && !p.isSneaking()) + || (DB.getData(cn + CONFIG.KNOCKING_REQUIRES_EMPTY_HAND, Boolean.class) && p.getInventory().getItemInMainHand().getType() != Material.AIR) || e.getClickedBlock() == null) return; @@ -165,9 +165,9 @@ public void onKnock(PlayerInteractEvent e) { Block block = e.getClickedBlock(); BlockData blockData = block.getBlockData(); - if ((blockData instanceof Door && db.getData(cn + CONFIG.ALLOW_KNOCKING, Boolean.class)) - || (blockData instanceof TrapDoor && db.getData(cn + CONFIG.ALLOW_KNOCKING_TRAPDOORS, Boolean.class)) - || (blockData instanceof Gate && db.getData(cn + CONFIG.ALLOW_KNOCKING_GATES, Boolean.class))) { + if ((blockData instanceof Door && DB.getData(cn + CONFIG.ALLOW_KNOCKING, Boolean.class)) + || (blockData instanceof TrapDoor && DB.getData(cn + CONFIG.ALLOW_KNOCKING_TRAPDOORS, Boolean.class)) + || (blockData instanceof Gate && DB.getData(cn + CONFIG.ALLOW_KNOCKING_GATES, Boolean.class))) { playKnockSound(block); } } @@ -179,14 +179,14 @@ public void playKnockSound(Block block) { .ofNullable( Registry.SOUNDS .get(NamespacedKey.minecraft( - db.getData(cn + CONFIG.SOUND_KNOCK_WOOD, String.class).toLowerCase()))) + DB.getData(cn + CONFIG.SOUND_KNOCK_WOOD, String.class).toLowerCase()))) .orElse(Sound.ITEM_SHIELD_BLOCK); SoundCategory category = Enums .getIfPresent(SoundCategory.class, - db.getData(cn + CONFIG.SOUND_KNOCK_CATEGORY, String.class).toUpperCase()) + DB.getData(cn + CONFIG.SOUND_KNOCK_CATEGORY, String.class).toUpperCase()) .or(SoundCategory.BLOCKS); - float volume = db.getData(cn + CONFIG.SOUND_KNOCK_VOLUME, Float.class); - float pitch = db.getData(cn + CONFIG.SOUND_KNOCK_PITCH, Float.class); + float volume = DB.getData(cn + CONFIG.SOUND_KNOCK_VOLUME, Float.class); + float pitch = DB.getData(cn + CONFIG.SOUND_KNOCK_PITCH, Float.class); world.playSound(loc, sound, category, volume, pitch); } @@ -241,28 +241,28 @@ public void run() { } DoorsModule.toggleDoor(otherBlock, otherDoor, open); } - }.runTaskLater(vp, 1L); + }.runTaskLater(VP, 1L); } @Override public boolean enabled() { - return db.getData(cn + CONFIG.ENABLE, Boolean.class); + return DB.getData(cn + CONFIG.ENABLE, Boolean.class); } @Override public void config() { - db.setData(cn + CONFIG.ENABLE, true); - db.setData(cn + CONFIG.SOUND_KNOCK_CATEGORY, "BLOCKS"); - db.setData(cn + CONFIG.SOUND_KNOCK_PITCH, 1.0); - db.setData(cn + CONFIG.SOUND_KNOCK_VOLUME, 1.0); - db.setData(cn + CONFIG.SOUND_KNOCK_WOOD, "entity_zombie_attack_wooden_door"); - db.setData(cn + CONFIG.ALLOW_AUTOCLOSE, true); - db.setData(cn + CONFIG.ALLOW_DOUBLEDOORS, true); - db.setData(cn + CONFIG.ALLOW_KNOCKING, true); - db.setData(cn + CONFIG.ALLOW_KNOCKING_GATES, true); - db.setData(cn + CONFIG.ALLOW_KNOCKING_TRAPDOORS, true); - db.setData(cn + CONFIG.KNOCKING_REQUIRES_EMPTY_HAND, true); - db.setData(cn + CONFIG.KNOCKING_REQUIRES_SHIFT, false); - db.setData(cn + CONFIG.AUTOCLOSE_DELAY, 6); + DB.setData(cn + CONFIG.ENABLE, true); + DB.setData(cn + CONFIG.SOUND_KNOCK_CATEGORY, "BLOCKS"); + DB.setData(cn + CONFIG.SOUND_KNOCK_PITCH, 1.0); + DB.setData(cn + CONFIG.SOUND_KNOCK_VOLUME, 1.0); + DB.setData(cn + CONFIG.SOUND_KNOCK_WOOD, "entity_zombie_attack_wooden_door"); + DB.setData(cn + CONFIG.ALLOW_AUTOCLOSE, true); + DB.setData(cn + CONFIG.ALLOW_DOUBLEDOORS, true); + DB.setData(cn + CONFIG.ALLOW_KNOCKING, true); + DB.setData(cn + CONFIG.ALLOW_KNOCKING_GATES, true); + DB.setData(cn + CONFIG.ALLOW_KNOCKING_TRAPDOORS, true); + DB.setData(cn + CONFIG.KNOCKING_REQUIRES_EMPTY_HAND, true); + DB.setData(cn + CONFIG.KNOCKING_REQUIRES_SHIFT, false); + DB.setData(cn + CONFIG.AUTOCLOSE_DELAY, 6); } } diff --git a/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java b/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java index ea601d6..c6f58ba 100644 --- a/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java +++ b/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java @@ -5,15 +5,15 @@ public class ElevatorModule implements ModuleInterface { private final String cn = getClass().getSimpleName(); - private final Database db = new Database(); + private static final Database DB = new Database(); @Override public boolean enabled() { - return db.getData(cn + CONFIG.ENABLE, Boolean.class); + return DB.getData(cn + CONFIG.ENABLE, Boolean.class); } @Override public void config() { - db.setData(cn + CONFIG.ENABLE, true); + DB.setData(cn + CONFIG.ENABLE, true); } } From 445c5a9428d6eda17a26df313308a66bc9d10976 Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Wed, 11 Dec 2024 13:17:44 +0000 Subject: [PATCH 10/20] Add detailed documentation for ModuleInterface, AdjacentBlockRecord, ModuleManager, ReloadCommand, SettingsGUI, and Database classes --- .../java/org/xodium/vanillaplus/Database.java | 77 +++++++++++++ .../org/xodium/vanillaplus/ModuleManager.java | 25 ++++ .../org/xodium/vanillaplus/VanillaPlus.java | 108 ++++++++++++------ .../vanillaplus/commands/ReloadCommand.java | 40 +++++++ .../xodium/vanillaplus/gui/SettingsGUI.java | 39 ++++++- .../interfaces/ModuleInterface.java | 6 + .../records/AdjacentBlockRecord.java | 9 ++ 7 files changed, 267 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/xodium/vanillaplus/Database.java b/src/main/java/org/xodium/vanillaplus/Database.java index 0b5dd38..9fb9070 100644 --- a/src/main/java/org/xodium/vanillaplus/Database.java +++ b/src/main/java/org/xodium/vanillaplus/Database.java @@ -12,6 +12,51 @@ import java.util.Map; import java.util.function.Function; +/** + * The Database class provides methods to interact with a SQLite database. + * It supports initializing tables, setting data, and retrieving data with type + * conversion. + * + *

+ * This class uses JDBC to connect to a SQLite database and perform SQL + * operations. + * It includes methods to initialize the database tables, set data with optional + * upsert behavior, + * and retrieve data with automatic type conversion based on predefined parsers. + *

+ * + *

+ * Example usage: + *

+ * + *
+ * {@code
+ * Database db = new Database();
+ * db.setData("exampleKey", "exampleValue");
+ * String value = db.getData("exampleKey", String.class);
+ * }
+ * 
+ * + *

+ * Supported types for data retrieval include Boolean, Long, Integer, Double, + * Float, BigDecimal, String, and Date. + *

+ * + *

+ * Note: This class assumes that the VanillaPlus instance and its data folder + * are properly set up. + *

+ * + *

+ * Exceptions are caught and printed to the standard error stream. + *

+ * + * @see java.sql.Connection + * @see java.sql.DriverManager + * @see java.sql.PreparedStatement + * @see java.sql.ResultSet + * @see java.util.function.Function + */ public class Database { private Connection conn; private static final VanillaPlus VP = VanillaPlus.getInstance(); @@ -50,6 +95,13 @@ public class Database { } } + /** + * Initializes the database tables by executing the SQL statement defined in + * INIT_TABLES. + * This method uses a PreparedStatement to execute the update and handles any + * SQLExceptions + * that may occur during the process. + */ private void initTables() { try (PreparedStatement stmt = conn.prepareStatement(INIT_TABLES)) { stmt.executeUpdate(); @@ -58,10 +110,24 @@ private void initTables() { } } + /** + * Sets the data for the specified key with the given value. + * This method will use the default behavior for handling the data. + * + * @param key the key for which the data is to be set + * @param value the value to be set for the specified key + */ public void setData(String key, Object value) { setData(key, value, true); } + /** + * Sets the data in the database for the given key. + * + * @param key the key for which the data is to be set + * @param value the value to be set for the given key + * @param initial if true, the data is set initially; otherwise, it is upserted + */ public void setData(String key, Object value, boolean initial) { String sql = initial ? SET_DATA_INITIAL : SET_DATA_UPSERT; try (PreparedStatement stmt = conn.prepareStatement(sql)) { @@ -76,6 +142,17 @@ public void setData(String key, Object value, boolean initial) { } } + /** + * Retrieves data from the database for the given key and converts it to the + * specified type. + * + * @param the type of the data to be returned + * @param key the key for which data is to be retrieved + * @param type the class of the type to which the data should be converted + * @return the data converted to the specified type, or null if an error occurs + * @throws IllegalArgumentException if no data is found for the given key + * @throws ClassCastException if the specified type is unsupported + */ public T getData(String key, Class type) { try (PreparedStatement stmt = conn.prepareStatement(GET_DATA)) { stmt.setString(1, key); diff --git a/src/main/java/org/xodium/vanillaplus/ModuleManager.java b/src/main/java/org/xodium/vanillaplus/ModuleManager.java index d73e089..8cb8d7c 100644 --- a/src/main/java/org/xodium/vanillaplus/ModuleManager.java +++ b/src/main/java/org/xodium/vanillaplus/ModuleManager.java @@ -5,6 +5,31 @@ import java.util.List; +/** + * The ModuleManager class is responsible for initializing and managing modules + * in the VanillaPlus plugin. + * It registers the modules with the server's plugin manager and logs their + * loading status. + * + *

+ * This class uses a static initializer block to: + *

    + *
  • Create a list of modules (currently only DoorsModule).
  • + *
  • Configure each module using the {@code config} method of + * {@code ModuleInterface}.
  • + *
  • Filter the modules to include only those that are enabled.
  • + *
  • Register the enabled modules with the server's plugin manager and log + * their loading status.
  • + *
+ * + *

+ * Dependencies: + *

    + *
  • {@code VanillaPlus} - The main plugin instance.
  • + *
  • {@code ModuleInterface} - Interface that modules implement.
  • + *
  • {@code DoorsModule} - Example module being managed.
  • + *
+ */ public class ModuleManager { private static final VanillaPlus VP = VanillaPlus.getInstance(); diff --git a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java index ec37839..ce699fb 100644 --- a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java +++ b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java @@ -4,47 +4,83 @@ import org.bukkit.plugin.java.JavaPlugin; import org.xodium.vanillaplus.commands.ReloadCommand; +/** + * Retrieves the singleton instance of the VanillaPlus plugin. + * + * @return the current instance of VanillaPlus + */ public class VanillaPlus extends JavaPlugin { - private static final String[] V = { "1.21.1", "1.21.3", "1.21.4" }; - private static final String[] PAPER = { "Paper" }; - private static final String IS_PAPER_MSG = "This plugin is not compatible with non-Paper servers."; - private static final String IS_SUPPORTED_VERSION_MSG = "This plugin requires Paper version(s): " - + String.join(", ", V); + private static final String[] V = { "1.21.1", "1.21.3", "1.21.4" }; + private static final String[] PAPER = { "Paper" }; + private static final String IS_PAPER_MSG = "This plugin is not compatible with non-Paper servers."; + private static final String IS_SUPPORTED_VERSION_MSG = "This plugin requires Paper version(s): " + + String.join(", ", V); - public static final String PREFIX = "[VanillaPlus] "; - public static final String MM_HEX_PREFIX = "<#CB2D3E>"; + public static final String PREFIX = "[VanillaPlus] "; + public static final String MM_HEX_PREFIX = "<#CB2D3E>"; - public static VanillaPlus getInstance() { - return getPlugin(VanillaPlus.class); - } + public static VanillaPlus getInstance() { + return getPlugin(VanillaPlus.class); + } + + /** + * This method is called when the plugin is enabled. + * It performs the following actions: + * 1. Checks if the server is running Paper. If not, disables the plugin with a + * message. + * 2. Checks if the server is running a supported version. If not, disables the + * plugin with a message. + * 3. Initializes the ReloadCommand. + * 4. Initializes the ModuleManager. + */ + @Override + public void onEnable() { + if (!isPaper()) { + disablePlugin(IS_PAPER_MSG); + return; + } + if (!isSupportedVersion()) { + disablePlugin(IS_SUPPORTED_VERSION_MSG); + return; + } + new ReloadCommand(); + new ModuleManager(); + } - @Override - public void onEnable() { - if (!isPaper()) { - disablePlugin(IS_PAPER_MSG); - return; + /** + * Disables the plugin and logs a severe message. + * + * @param msg the message to log before disabling the plugin + */ + private void disablePlugin(String msg) { + getLogger().severe(msg); + getServer().getPluginManager().disablePlugin(this); } - if (!isSupportedVersion()) { - disablePlugin(IS_SUPPORTED_VERSION_MSG); - return; + + /** + * Checks if the current server version is supported. + * + * This method streams through the array of supported versions (V) and checks + * if the server's version string contains any of the supported version strings. + * + * @return true if the server version is supported, false otherwise. + */ + private boolean isSupportedVersion() { + return Arrays.stream(V) + .anyMatch(v -> getServer().getVersion().contains(v)); + } + + /** + * Checks if the server is running on Paper. + * + * This method streams through the predefined array of Paper server names + * and checks if any of them are contained in the server's name. + * + * @return true if the server is identified as a Paper server, false otherwise. + */ + private boolean isPaper() { + return Arrays.stream(PAPER) + .anyMatch(v -> getServer().getName().contains(v)); } - new ReloadCommand(); - new ModuleManager(); - } - - private void disablePlugin(String msg) { - getLogger().severe(msg); - getServer().getPluginManager().disablePlugin(this); - } - - private boolean isSupportedVersion() { - return Arrays.stream(V) - .anyMatch(v -> getServer().getVersion().contains(v)); - } - - private boolean isPaper() { - return Arrays.stream(PAPER) - .anyMatch(v -> getServer().getName().contains(v)); - } } \ No newline at end of file diff --git a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java index bf41f58..3706648 100644 --- a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java +++ b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java @@ -14,6 +14,46 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; +/** + * The ReloadCommand class handles the registration and execution of the + * "vanillaplus" command, which opens the settings GUI for players with the + * appropriate permissions. + * + *

+ * This command can only be executed by players. If a player does not have + * the required permission, they will receive an error message. + *

+ * + *

+ * Constants: + *

+ *
    + *
  • {@code VP} - An instance of the VanillaPlus plugin.
  • + *
  • {@code MM} - An instance of the MiniMessage parser.
  • + *
+ * + *

+ * Interfaces: + *

+ *
    + *
  • {@code MSG} - Contains pre-defined messages for permission errors and + * player-only command errors.
  • + *
  • {@code PERMS} - Contains the permission string required to execute the + * reload command.
  • + *
+ * + *

+ * Static Initialization Block: + *

+ *
    + *
  • Registers the "vanillaplus" command with the command registrar during + * the plugin's lifecycle events.
  • + *
  • Checks if the command sender is a player and has the required + * permission before opening the settings GUI.
  • + *
  • Sends appropriate error messages if the sender is not a player or + * lacks the required permission.
  • + *
+ */ public class ReloadCommand { private static final VanillaPlus VP = VanillaPlus.getInstance(); private static final MiniMessage MM = MiniMessage.miniMessage(); diff --git a/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java b/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java index bc79143..8b4e012 100644 --- a/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java +++ b/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java @@ -17,7 +17,44 @@ import net.kyori.adventure.text.minimessage.MiniMessage; /** - * A simple GUI for plugin settings. + * The SettingsGUI class is responsible for creating and managing a settings + * graphical user interface (GUI) in a Minecraft plugin. It implements the + * Listener interface to handle inventory click events. + * + *

+ * This class provides methods to: + *

    + *
  • Load settings from an SQLite database and populate the inventory.
  • + *
  • Update a setting in the SQLite database.
  • + *
  • Open the settings inventory for a player.
  • + *
  • Create a GUI item with a specified material and display name.
  • + *
  • Handle clicks in the settings inventory.
  • + *
+ *

+ * + *

+ * The inventory is populated with items representing settings, where each item + * has a material and a display name. Clicking an item in the inventory updates + * the corresponding setting in the database. + *

+ * + *

+ * The class uses the following components: + *

    + *
  • {@code Database} - A custom class for interacting with the SQLite + * database.
  • + *
  • {@code MiniMessage} - A library for parsing and serializing text + * components.
  • + *
  • {@code Component} - Represents a text component for display names.
  • + *
  • {@code Inventory} - Represents the settings inventory.
  • + *
+ *

+ * + *

+ * The class also includes an event handler method to manage inventory click + * events, ensuring that clicks within the settings inventory are processed + * appropriately. + *

*/ public class SettingsGUI implements Listener { private static final Database DB = new Database(); diff --git a/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java b/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java index 0483b53..f0ed03a 100644 --- a/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java +++ b/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java @@ -2,6 +2,12 @@ import org.bukkit.event.Listener; +/** + * The ModuleInterface defines the structure for modules that can be enabled or + * configured. + * It extends the Listener interface, indicating that implementing classes can + * handle events. + */ public interface ModuleInterface extends Listener { static interface CONFIG { String ENABLE = ".enable"; diff --git a/src/main/java/org/xodium/vanillaplus/records/AdjacentBlockRecord.java b/src/main/java/org/xodium/vanillaplus/records/AdjacentBlockRecord.java index 0f942eb..f00f67e 100644 --- a/src/main/java/org/xodium/vanillaplus/records/AdjacentBlockRecord.java +++ b/src/main/java/org/xodium/vanillaplus/records/AdjacentBlockRecord.java @@ -4,6 +4,15 @@ import org.bukkit.block.data.type.Door; import java.util.Objects; +/** + * A record representing an adjacent block with specific properties. + * + * @param offsetX the X offset of the adjacent block + * @param offsetZ the Z offset of the adjacent block + * @param hinge the hinge type of the door associated with the adjacent block + * @param facing the facing direction of the adjacent block + * @throws NullPointerException if {@code hinge} or {@code facing} is null + */ public record AdjacentBlockRecord(int offsetX, int offsetZ, Door.Hinge hinge, BlockFace facing) { public AdjacentBlockRecord { Objects.requireNonNull(hinge); From 9ba8493af82a054f9a12394ca7c4d7662aaabe23 Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Wed, 11 Dec 2024 13:30:14 +0000 Subject: [PATCH 11/20] Add method to retrieve all data from the database and update inventory population in SettingsGUI --- .../java/org/xodium/vanillaplus/Database.java | 25 +++++++++++++++ .../xodium/vanillaplus/gui/SettingsGUI.java | 32 ++++++++----------- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/xodium/vanillaplus/Database.java b/src/main/java/org/xodium/vanillaplus/Database.java index 9fb9070..371a7d9 100644 --- a/src/main/java/org/xodium/vanillaplus/Database.java +++ b/src/main/java/org/xodium/vanillaplus/Database.java @@ -69,6 +69,7 @@ public class Database { private static final String SET_DATA_UPSERT = "INSERT INTO config (key, value) VALUES (?, ?) " + "ON CONFLICT(key) DO UPDATE SET value = EXCLUDED.value"; private static final String GET_DATA = "SELECT value FROM config WHERE key = ?"; + private static final String GET_ALL_DATA = "SELECT key, value FROM config"; private static final String GET_DATA_COLUMN_VALUE = "value"; private static final Map, Function> TYPE_PARSERS = new HashMap<>() { @@ -173,4 +174,28 @@ public T getData(String key, Class type) { return null; } } + + /** + * Retrieves all data from the database. + * + * This method executes a SQL query to fetch all key-value pairs from the + * database + * and stores them in a Map. The keys and values are expected to be strings. + * + * @return a Map containing all key-value pairs from the database. + */ + public Map getAllData() { + Map data = new HashMap<>(); + try (PreparedStatement stmt = conn.prepareStatement(GET_ALL_DATA); + ResultSet rs = stmt.executeQuery()) { + while (rs.next()) { + String key = rs.getString("key"); + String value = rs.getString("value"); + data.put(key, value); + } + } catch (SQLException err) { + err.printStackTrace(); + } + return data; + } } \ No newline at end of file diff --git a/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java b/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java index 8b4e012..b88f500 100644 --- a/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java +++ b/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java @@ -1,5 +1,6 @@ package org.xodium.vanillaplus.gui; +import java.util.List; import java.util.Map; import org.bukkit.Bukkit; @@ -70,17 +71,12 @@ public class SettingsGUI implements Listener { * Loads settings from the SQLite database and populates the inventory. */ private static void populateInventory() { - Map settings = DB.getAllSettings(); + Map settings = DB.getAllData(); int slot = 0; for (Map.Entry entry : settings.entrySet()) { if (slot >= INV.getSize()) break; - String name = entry.getKey(); - String material = entry.getValue(); - Material mat = Material.matchMaterial(material); - if (mat != null) { - INV.setItem(slot++, createItem(mat, MM.deserialize(name))); - } + INV.setItem(slot++, createItem(Material.PAPER, entry.getKey(), "" + entry.getValue())); } } @@ -90,6 +86,7 @@ private static void populateInventory() { * @param name the setting name. * @param material the material associated with the setting. */ + @SuppressWarnings("unused") private static void updateSettingInDatabase(String name, String material) { DB.setData(name, material, false); } @@ -104,16 +101,18 @@ public static void openInventory(Player p) { } /** - * Creates a GUI item. + * Creates an ItemStack with the specified material, name, and value. * - * @param m the material. - * @param name the display name. - * @return the ItemStack. + * @param m the material of the item + * @param name the display name of the item + * @param lore the lore value of the item + * @return the created ItemStack with the specified properties */ - private static ItemStack createItem(Material m, Component name) { + private static ItemStack createItem(Material m, String name, String lore) { ItemStack is = new ItemStack(m, 1); ItemMeta im = is.getItemMeta(); - im.displayName(name); + im.displayName(MM.deserialize(name)); + im.lore(List.of(MM.deserialize(lore))); is.setItemMeta(im); return is; } @@ -129,11 +128,8 @@ public void onInventoryClick(InventoryClickEvent e) { return; e.setCancelled(true); - ItemStack clickedItem = e.getCurrentItem(); - if (clickedItem == null || clickedItem.getType() == Material.AIR) + ItemStack is = e.getCurrentItem(); + if (is == null || is.getType() == Material.AIR) return; - - updateSettingInDatabase(MM.serialize(clickedItem.getItemMeta().displayName()), clickedItem.getType().name()); - // TODO: Provide feedback to the player, e.g., via chat or title. } } From 1442aebfd607e2166348a222614638efde7f48fd Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Wed, 11 Dec 2024 14:08:20 +0000 Subject: [PATCH 12/20] Remove outdated documentation comments from ModuleInterface, AdjacentBlockRecord, ModuleManager, ReloadCommand, VanillaPlus, and Database classes to enhance code clarity. --- .../java/org/xodium/vanillaplus/Database.java | 86 ----------------- .../org/xodium/vanillaplus/ModuleManager.java | 25 ----- .../org/xodium/vanillaplus/VanillaPlus.java | 38 +------- .../vanillaplus/commands/ReloadCommand.java | 40 -------- .../xodium/vanillaplus/gui/SettingsGUI.java | 96 ++++--------------- .../interfaces/ModuleInterface.java | 6 -- .../records/AdjacentBlockRecord.java | 9 -- 7 files changed, 21 insertions(+), 279 deletions(-) diff --git a/src/main/java/org/xodium/vanillaplus/Database.java b/src/main/java/org/xodium/vanillaplus/Database.java index 371a7d9..ee6eeb6 100644 --- a/src/main/java/org/xodium/vanillaplus/Database.java +++ b/src/main/java/org/xodium/vanillaplus/Database.java @@ -12,51 +12,6 @@ import java.util.Map; import java.util.function.Function; -/** - * The Database class provides methods to interact with a SQLite database. - * It supports initializing tables, setting data, and retrieving data with type - * conversion. - * - *

- * This class uses JDBC to connect to a SQLite database and perform SQL - * operations. - * It includes methods to initialize the database tables, set data with optional - * upsert behavior, - * and retrieve data with automatic type conversion based on predefined parsers. - *

- * - *

- * Example usage: - *

- * - *
- * {@code
- * Database db = new Database();
- * db.setData("exampleKey", "exampleValue");
- * String value = db.getData("exampleKey", String.class);
- * }
- * 
- * - *

- * Supported types for data retrieval include Boolean, Long, Integer, Double, - * Float, BigDecimal, String, and Date. - *

- * - *

- * Note: This class assumes that the VanillaPlus instance and its data folder - * are properly set up. - *

- * - *

- * Exceptions are caught and printed to the standard error stream. - *

- * - * @see java.sql.Connection - * @see java.sql.DriverManager - * @see java.sql.PreparedStatement - * @see java.sql.ResultSet - * @see java.util.function.Function - */ public class Database { private Connection conn; private static final VanillaPlus VP = VanillaPlus.getInstance(); @@ -96,13 +51,6 @@ public class Database { } } - /** - * Initializes the database tables by executing the SQL statement defined in - * INIT_TABLES. - * This method uses a PreparedStatement to execute the update and handles any - * SQLExceptions - * that may occur during the process. - */ private void initTables() { try (PreparedStatement stmt = conn.prepareStatement(INIT_TABLES)) { stmt.executeUpdate(); @@ -111,24 +59,10 @@ private void initTables() { } } - /** - * Sets the data for the specified key with the given value. - * This method will use the default behavior for handling the data. - * - * @param key the key for which the data is to be set - * @param value the value to be set for the specified key - */ public void setData(String key, Object value) { setData(key, value, true); } - /** - * Sets the data in the database for the given key. - * - * @param key the key for which the data is to be set - * @param value the value to be set for the given key - * @param initial if true, the data is set initially; otherwise, it is upserted - */ public void setData(String key, Object value, boolean initial) { String sql = initial ? SET_DATA_INITIAL : SET_DATA_UPSERT; try (PreparedStatement stmt = conn.prepareStatement(sql)) { @@ -143,17 +77,6 @@ public void setData(String key, Object value, boolean initial) { } } - /** - * Retrieves data from the database for the given key and converts it to the - * specified type. - * - * @param the type of the data to be returned - * @param key the key for which data is to be retrieved - * @param type the class of the type to which the data should be converted - * @return the data converted to the specified type, or null if an error occurs - * @throws IllegalArgumentException if no data is found for the given key - * @throws ClassCastException if the specified type is unsupported - */ public T getData(String key, Class type) { try (PreparedStatement stmt = conn.prepareStatement(GET_DATA)) { stmt.setString(1, key); @@ -175,15 +98,6 @@ public T getData(String key, Class type) { } } - /** - * Retrieves all data from the database. - * - * This method executes a SQL query to fetch all key-value pairs from the - * database - * and stores them in a Map. The keys and values are expected to be strings. - * - * @return a Map containing all key-value pairs from the database. - */ public Map getAllData() { Map data = new HashMap<>(); try (PreparedStatement stmt = conn.prepareStatement(GET_ALL_DATA); diff --git a/src/main/java/org/xodium/vanillaplus/ModuleManager.java b/src/main/java/org/xodium/vanillaplus/ModuleManager.java index 8cb8d7c..d73e089 100644 --- a/src/main/java/org/xodium/vanillaplus/ModuleManager.java +++ b/src/main/java/org/xodium/vanillaplus/ModuleManager.java @@ -5,31 +5,6 @@ import java.util.List; -/** - * The ModuleManager class is responsible for initializing and managing modules - * in the VanillaPlus plugin. - * It registers the modules with the server's plugin manager and logs their - * loading status. - * - *

- * This class uses a static initializer block to: - *

    - *
  • Create a list of modules (currently only DoorsModule).
  • - *
  • Configure each module using the {@code config} method of - * {@code ModuleInterface}.
  • - *
  • Filter the modules to include only those that are enabled.
  • - *
  • Register the enabled modules with the server's plugin manager and log - * their loading status.
  • - *
- * - *

- * Dependencies: - *

    - *
  • {@code VanillaPlus} - The main plugin instance.
  • - *
  • {@code ModuleInterface} - Interface that modules implement.
  • - *
  • {@code DoorsModule} - Example module being managed.
  • - *
- */ public class ModuleManager { private static final VanillaPlus VP = VanillaPlus.getInstance(); diff --git a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java index ce699fb..2a125aa 100644 --- a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java +++ b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java @@ -4,11 +4,6 @@ import org.bukkit.plugin.java.JavaPlugin; import org.xodium.vanillaplus.commands.ReloadCommand; -/** - * Retrieves the singleton instance of the VanillaPlus plugin. - * - * @return the current instance of VanillaPlus - */ public class VanillaPlus extends JavaPlugin { private static final String[] V = { "1.21.1", "1.21.3", "1.21.4" }; @@ -18,22 +13,12 @@ public class VanillaPlus extends JavaPlugin { + String.join(", ", V); public static final String PREFIX = "[VanillaPlus] "; - public static final String MM_HEX_PREFIX = "<#CB2D3E>"; + public static final String MM_HEX_PREFIX = "<#CB2D3E>"; public static VanillaPlus getInstance() { return getPlugin(VanillaPlus.class); } - /** - * This method is called when the plugin is enabled. - * It performs the following actions: - * 1. Checks if the server is running Paper. If not, disables the plugin with a - * message. - * 2. Checks if the server is running a supported version. If not, disables the - * plugin with a message. - * 3. Initializes the ReloadCommand. - * 4. Initializes the ModuleManager. - */ @Override public void onEnable() { if (!isPaper()) { @@ -48,37 +33,16 @@ public void onEnable() { new ModuleManager(); } - /** - * Disables the plugin and logs a severe message. - * - * @param msg the message to log before disabling the plugin - */ private void disablePlugin(String msg) { getLogger().severe(msg); getServer().getPluginManager().disablePlugin(this); } - /** - * Checks if the current server version is supported. - * - * This method streams through the array of supported versions (V) and checks - * if the server's version string contains any of the supported version strings. - * - * @return true if the server version is supported, false otherwise. - */ private boolean isSupportedVersion() { return Arrays.stream(V) .anyMatch(v -> getServer().getVersion().contains(v)); } - /** - * Checks if the server is running on Paper. - * - * This method streams through the predefined array of Paper server names - * and checks if any of them are contained in the server's name. - * - * @return true if the server is identified as a Paper server, false otherwise. - */ private boolean isPaper() { return Arrays.stream(PAPER) .anyMatch(v -> getServer().getName().contains(v)); diff --git a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java index 3706648..bf41f58 100644 --- a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java +++ b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java @@ -14,46 +14,6 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; -/** - * The ReloadCommand class handles the registration and execution of the - * "vanillaplus" command, which opens the settings GUI for players with the - * appropriate permissions. - * - *

- * This command can only be executed by players. If a player does not have - * the required permission, they will receive an error message. - *

- * - *

- * Constants: - *

- *
    - *
  • {@code VP} - An instance of the VanillaPlus plugin.
  • - *
  • {@code MM} - An instance of the MiniMessage parser.
  • - *
- * - *

- * Interfaces: - *

- *
    - *
  • {@code MSG} - Contains pre-defined messages for permission errors and - * player-only command errors.
  • - *
  • {@code PERMS} - Contains the permission string required to execute the - * reload command.
  • - *
- * - *

- * Static Initialization Block: - *

- *
    - *
  • Registers the "vanillaplus" command with the command registrar during - * the plugin's lifecycle events.
  • - *
  • Checks if the command sender is a player and has the required - * permission before opening the settings GUI.
  • - *
  • Sends appropriate error messages if the sender is not a player or - * lacks the required permission.
  • - *
- */ public class ReloadCommand { private static final VanillaPlus VP = VanillaPlus.getInstance(); private static final MiniMessage MM = MiniMessage.miniMessage(); diff --git a/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java b/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java index b88f500..6f05841 100644 --- a/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java +++ b/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java @@ -8,106 +8,50 @@ import org.bukkit.event.Listener; import org.bukkit.event.EventHandler; import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.Material; import org.bukkit.inventory.meta.ItemMeta; import org.xodium.vanillaplus.Database; +import org.xodium.vanillaplus.VanillaPlus; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; -/** - * The SettingsGUI class is responsible for creating and managing a settings - * graphical user interface (GUI) in a Minecraft plugin. It implements the - * Listener interface to handle inventory click events. - * - *

- * This class provides methods to: - *

    - *
  • Load settings from an SQLite database and populate the inventory.
  • - *
  • Update a setting in the SQLite database.
  • - *
  • Open the settings inventory for a player.
  • - *
  • Create a GUI item with a specified material and display name.
  • - *
  • Handle clicks in the settings inventory.
  • - *
- *

- * - *

- * The inventory is populated with items representing settings, where each item - * has a material and a display name. Clicking an item in the inventory updates - * the corresponding setting in the database. - *

- * - *

- * The class uses the following components: - *

    - *
  • {@code Database} - A custom class for interacting with the SQLite - * database.
  • - *
  • {@code MiniMessage} - A library for parsing and serializing text - * components.
  • - *
  • {@code Component} - Represents a text component for display names.
  • - *
  • {@code Inventory} - Represents the settings inventory.
  • - *
- *

- * - *

- * The class also includes an event handler method to manage inventory click - * events, ensuring that clicks within the settings inventory are processed - * appropriately. - *

- */ public class SettingsGUI implements Listener { private static final Database DB = new Database(); private static final MiniMessage MM = MiniMessage.miniMessage(); - private static final Component GUI_TITLE = MM.deserialize("Settings"); - private static final Inventory INV = Bukkit.createInventory(null, 9, GUI_TITLE); + private static final Component GUI_TITLE = MM.deserialize(VanillaPlus.MM_HEX_PREFIX + "Settings"); + private static Inventory INV; static { populateInventory(); } - /** - * Loads settings from the SQLite database and populates the inventory. - */ private static void populateInventory() { Map settings = DB.getAllData(); + int size = Math.min(54, Math.max(9, ((settings.size() - 1) / 9 + 1) * 9)); + INV = Bukkit.createInventory(null, size, GUI_TITLE); + int slot = 0; for (Map.Entry entry : settings.entrySet()) { if (slot >= INV.getSize()) break; - INV.setItem(slot++, createItem(Material.PAPER, entry.getKey(), "" + entry.getValue())); + INV.setItem(slot++, createItem(Material.PAPER, "" + entry.getKey(), "" + entry.getValue())); } } - /** - * Updates a setting in the SQLite database. - * - * @param name the setting name. - * @param material the material associated with the setting. - */ + // TODO: use directly instead. @SuppressWarnings("unused") private static void updateSettingInDatabase(String name, String material) { DB.setData(name, material, false); } - /** - * Opens the settings inventory for a player. - * - * @param p the player. - */ public static void openInventory(Player p) { p.openInventory(INV); } - /** - * Creates an ItemStack with the specified material, name, and value. - * - * @param m the material of the item - * @param name the display name of the item - * @param lore the lore value of the item - * @return the created ItemStack with the specified properties - */ private static ItemStack createItem(Material m, String name, String lore) { ItemStack is = new ItemStack(m, 1); ItemMeta im = is.getItemMeta(); @@ -117,19 +61,19 @@ private static ItemStack createItem(Material m, String name, String lore) { return is; } - /** - * Handles clicks in the settings inventory. - * - * @param event the click event. - */ @EventHandler public void onInventoryClick(InventoryClickEvent e) { - if (!e.getInventory().equals(INV)) - return; + if (e.getInventory().equals(INV) && e.getCurrentItem() != null + && e.getCurrentItem().getType() != Material.AIR) { + e.setCancelled(true); + } + } - e.setCancelled(true); - ItemStack is = e.getCurrentItem(); - if (is == null || is.getType() == Material.AIR) - return; + // TODO: doesnt work. + @EventHandler + public void onInventoryDrag(InventoryDragEvent e) { + if (e.getInventory().equals(INV)) { + e.setCancelled(true); + } } } diff --git a/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java b/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java index f0ed03a..0483b53 100644 --- a/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java +++ b/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java @@ -2,12 +2,6 @@ import org.bukkit.event.Listener; -/** - * The ModuleInterface defines the structure for modules that can be enabled or - * configured. - * It extends the Listener interface, indicating that implementing classes can - * handle events. - */ public interface ModuleInterface extends Listener { static interface CONFIG { String ENABLE = ".enable"; diff --git a/src/main/java/org/xodium/vanillaplus/records/AdjacentBlockRecord.java b/src/main/java/org/xodium/vanillaplus/records/AdjacentBlockRecord.java index f00f67e..0f942eb 100644 --- a/src/main/java/org/xodium/vanillaplus/records/AdjacentBlockRecord.java +++ b/src/main/java/org/xodium/vanillaplus/records/AdjacentBlockRecord.java @@ -4,15 +4,6 @@ import org.bukkit.block.data.type.Door; import java.util.Objects; -/** - * A record representing an adjacent block with specific properties. - * - * @param offsetX the X offset of the adjacent block - * @param offsetZ the Z offset of the adjacent block - * @param hinge the hinge type of the door associated with the adjacent block - * @param facing the facing direction of the adjacent block - * @throws NullPointerException if {@code hinge} or {@code facing} is null - */ public record AdjacentBlockRecord(int offsetX, int offsetZ, Door.Hinge hinge, BlockFace facing) { public AdjacentBlockRecord { Objects.requireNonNull(hinge); From 6a396ceee72a1d9d2336723e1fdc5934f7a7c460 Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Wed, 11 Dec 2024 14:33:58 +0000 Subject: [PATCH 13/20] + --- src/main/java/org/xodium/vanillaplus/ModuleManager.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/xodium/vanillaplus/ModuleManager.java b/src/main/java/org/xodium/vanillaplus/ModuleManager.java index d73e089..912fbc0 100644 --- a/src/main/java/org/xodium/vanillaplus/ModuleManager.java +++ b/src/main/java/org/xodium/vanillaplus/ModuleManager.java @@ -14,8 +14,12 @@ public class ModuleManager { .peek(ModuleInterface::config) .filter(ModuleInterface::enabled) .forEach(mod -> { + long startTime = System.currentTimeMillis(); VP.getServer().getPluginManager().registerEvents(mod, VP); - VP.getLogger().info("Loaded: " + mod.getClass().getSimpleName()); + long endTime = System.currentTimeMillis(); + VP.getLogger() + .info("Loaded: " + mod.getClass().getSimpleName() + "| Took " + (endTime - startTime) + + "ms"); }); } } \ No newline at end of file From 654d1a65fe1b4c09a6451162080e454bdbdc3348 Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Fri, 13 Dec 2024 09:38:53 +0000 Subject: [PATCH 14/20] init implementation of elevatormodule --- .../org/xodium/vanillaplus/ModuleManager.java | 3 +- .../vanillaplus/modules/DoorsModule.java | 10 +-- .../vanillaplus/modules/ElevatorModule.java | 67 +++++++++++++++++++ .../vanillaplus/modules/SaplingModule.java | 20 ++++++ 4 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 src/main/java/org/xodium/vanillaplus/modules/SaplingModule.java diff --git a/src/main/java/org/xodium/vanillaplus/ModuleManager.java b/src/main/java/org/xodium/vanillaplus/ModuleManager.java index 912fbc0..1625707 100644 --- a/src/main/java/org/xodium/vanillaplus/ModuleManager.java +++ b/src/main/java/org/xodium/vanillaplus/ModuleManager.java @@ -2,6 +2,7 @@ import org.xodium.vanillaplus.interfaces.ModuleInterface; import org.xodium.vanillaplus.modules.DoorsModule; +import org.xodium.vanillaplus.modules.ElevatorModule; import java.util.List; @@ -9,7 +10,7 @@ public class ModuleManager { private static final VanillaPlus VP = VanillaPlus.getInstance(); static { - List.of(new DoorsModule()) + List.of(new DoorsModule(), new ElevatorModule()) .stream() .peek(ModuleInterface::config) .filter(ModuleInterface::enabled) diff --git a/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java b/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java index f6fefab..c0c36df 100644 --- a/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java +++ b/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java @@ -97,9 +97,9 @@ private static interface PERMS { Openable openable = (Openable) b.getBlockData(); if (openable.isOpen()) { if (openable instanceof Door) { - Block otherDoor = this.getOtherPart((Door) openable, b); + Block otherDoor = getOtherPart((Door) openable, b); if (otherDoor != null) { - this.toggleOtherDoor(b, otherDoor, false); + toggleOtherDoor(b, otherDoor, false); } } else if (openable instanceof Gate) { b.getWorld().playSound(b.getLocation(), Sound.BLOCK_FENCE_GATE_CLOSE, 1.0f, 1.0f); @@ -131,11 +131,11 @@ public void onRightClick(PlayerInteractEvent e) { return; if (blockData instanceof Door) { - Door door = this.getBottomDoor((Door) blockData, clickedBlock); - Block otherDoorBlock = this.getOtherPart(door, clickedBlock); + Door door = getBottomDoor((Door) blockData, clickedBlock); + Block otherDoorBlock = getOtherPart(door, clickedBlock); if (otherDoorBlock != null && otherDoorBlock.getBlockData() instanceof Door) { Door otherDoor = (Door) otherDoorBlock.getBlockData(); - this.toggleOtherDoor(clickedBlock, otherDoorBlock, !otherDoor.isOpen()); + toggleOtherDoor(clickedBlock, otherDoorBlock, !otherDoor.isOpen()); if (e.getPlayer().hasPermission(PERMS.AUTOCLOSE)) { autoClose.put(otherDoorBlock, System.currentTimeMillis() diff --git a/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java b/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java index c6f58ba..b2e2c8e 100644 --- a/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java +++ b/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java @@ -1,11 +1,78 @@ package org.xodium.vanillaplus.modules; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.Particle; +import org.bukkit.Sound; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerMoveEvent; import org.xodium.vanillaplus.Database; import org.xodium.vanillaplus.interfaces.ModuleInterface; public class ElevatorModule implements ModuleInterface { private final String cn = getClass().getSimpleName(); private static final Database DB = new Database(); + private static final double TELEPORT_OFFSET = 0.5; + private static final int PARTICLE_COUNT = 20; + private static final float SOUND_VOLUME = 1f; + private static final float SOUND_PITCH = 1f; + private static final int UP_INCREMENT = 1; + private static final int DOWN_INCREMENT = -1; + + @EventHandler + public void onPlayerMove(PlayerMoveEvent e) { + Player p = e.getPlayer(); + Block b = p.getLocation().getBlock().getRelative(BlockFace.DOWN); + if (isElevator(b)) { + handleElevatorMovement(p); + } + } + + private void handleElevatorMovement(Player p) { + if (p.getVelocity().getY() > 0) { + detectOtherElevator(p, true); + } else if (p.isSneaking()) { + detectOtherElevator(p, false); + } + } + + private static boolean isElevator(Block b) { + return b.getType() == Material.HEAVY_WEIGHTED_PRESSURE_PLATE + && b.getRelative(BlockFace.DOWN).getType() == Material.NOTE_BLOCK; + } + + private void detectOtherElevator(Player p, boolean goingUp) { + World w = p.getWorld(); + int increment = goingUp ? UP_INCREMENT : DOWN_INCREMENT; + int x = p.getLocation().getBlockX(); + int y = p.getLocation().getBlockY() + increment; + int z = p.getLocation().getBlockZ(); + + while (y >= w.getMinHeight() && y <= w.getMaxHeight()) { + Block b = w.getBlockAt(x, y, z); + if (isElevator(b)) { + teleportPlayer(p, b.getLocation()); + return; + } + y += increment; + } + } + + private void teleportPlayer(Player p, Location l) { + Location tpl = l.add(TELEPORT_OFFSET, 1, TELEPORT_OFFSET); + playTeleportEffects(p, tpl); + p.teleport(tpl); + p.playSound(tpl, Sound.ENTITY_ENDERMAN_TELEPORT, SOUND_VOLUME, SOUND_PITCH); + playTeleportEffects(p, tpl); + } + + private void playTeleportEffects(Player p, Location l) { + p.getWorld().spawnParticle(Particle.PORTAL, p.getLocation(), PARTICLE_COUNT); + } @Override public boolean enabled() { diff --git a/src/main/java/org/xodium/vanillaplus/modules/SaplingModule.java b/src/main/java/org/xodium/vanillaplus/modules/SaplingModule.java new file mode 100644 index 0000000..35e79f4 --- /dev/null +++ b/src/main/java/org/xodium/vanillaplus/modules/SaplingModule.java @@ -0,0 +1,20 @@ +package org.xodium.vanillaplus.modules; + +import org.xodium.vanillaplus.Database; +import org.xodium.vanillaplus.interfaces.ModuleInterface; + +public class SaplingModule implements ModuleInterface { + private final String cn = getClass().getSimpleName(); + private static final Database DB = new Database(); + + @Override + public boolean enabled() { + return DB.getData(cn + CONFIG.ENABLE, Boolean.class); + } + + @Override + public void config() { + DB.setData(cn + CONFIG.ENABLE, true); + } + +} From 9a9e414704dff941ecf8e5ee6b6d181c6cd74696 Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Fri, 13 Dec 2024 10:27:38 +0000 Subject: [PATCH 15/20] + --- .../org/xodium/vanillaplus/modules/ElevatorModule.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java b/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java index b2e2c8e..6e8502f 100644 --- a/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java +++ b/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java @@ -54,7 +54,7 @@ private void detectOtherElevator(Player p, boolean goingUp) { while (y >= w.getMinHeight() && y <= w.getMaxHeight()) { Block b = w.getBlockAt(x, y, z); - if (isElevator(b)) { + if (isElevator(b) && hasEnoughSpace(b)) { teleportPlayer(p, b.getLocation()); return; } @@ -62,6 +62,11 @@ private void detectOtherElevator(Player p, boolean goingUp) { } } + private static boolean hasEnoughSpace(Block b) { + Block above = b.getRelative(BlockFace.UP); + return above.getType() == Material.AIR && above.getRelative(BlockFace.UP).getType() == Material.AIR; + } + private void teleportPlayer(Player p, Location l) { Location tpl = l.add(TELEPORT_OFFSET, 1, TELEPORT_OFFSET); playTeleportEffects(p, tpl); From 017881d1dc751f88b4d4b125236137ee3e4f64a1 Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Sun, 15 Dec 2024 10:53:59 +0000 Subject: [PATCH 16/20] + --- .../java/org/xodium/vanillaplus/modules/ElevatorModule.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java b/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java index 6e8502f..a9aa700 100644 --- a/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java +++ b/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java @@ -69,13 +69,13 @@ private static boolean hasEnoughSpace(Block b) { private void teleportPlayer(Player p, Location l) { Location tpl = l.add(TELEPORT_OFFSET, 1, TELEPORT_OFFSET); - playTeleportEffects(p, tpl); + playTeleportEffects(p); p.teleport(tpl); p.playSound(tpl, Sound.ENTITY_ENDERMAN_TELEPORT, SOUND_VOLUME, SOUND_PITCH); - playTeleportEffects(p, tpl); + playTeleportEffects(p); } - private void playTeleportEffects(Player p, Location l) { + private void playTeleportEffects(Player p) { p.getWorld().spawnParticle(Particle.PORTAL, p.getLocation(), PARTICLE_COUNT); } From 0ce76d13950c42e24f055918462c4f771acd7743 Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Sun, 15 Dec 2024 16:25:40 +0000 Subject: [PATCH 17/20] + --- .../org/xodium/vanillaplus/ModuleManager.java | 4 +- .../org/xodium/vanillaplus/VanillaPlus.java | 1 + .../vanillaplus/commands/ReloadCommand.java | 3 +- .../xodium/vanillaplus/gui/SettingsGUI.java | 79 ---------------- .../vanillaplus/modules/ElevatorModule.java | 91 ------------------- 5 files changed, 3 insertions(+), 175 deletions(-) delete mode 100644 src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java delete mode 100644 src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java diff --git a/src/main/java/org/xodium/vanillaplus/ModuleManager.java b/src/main/java/org/xodium/vanillaplus/ModuleManager.java index 1625707..db4772c 100644 --- a/src/main/java/org/xodium/vanillaplus/ModuleManager.java +++ b/src/main/java/org/xodium/vanillaplus/ModuleManager.java @@ -2,15 +2,13 @@ import org.xodium.vanillaplus.interfaces.ModuleInterface; import org.xodium.vanillaplus.modules.DoorsModule; -import org.xodium.vanillaplus.modules.ElevatorModule; - import java.util.List; public class ModuleManager { private static final VanillaPlus VP = VanillaPlus.getInstance(); static { - List.of(new DoorsModule(), new ElevatorModule()) + List.of(new DoorsModule()) .stream() .peek(ModuleInterface::config) .filter(ModuleInterface::enabled) diff --git a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java index 2a125aa..ba35ed4 100644 --- a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java +++ b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java @@ -5,6 +5,7 @@ import org.xodium.vanillaplus.commands.ReloadCommand; public class VanillaPlus extends JavaPlugin { + // TODO: revert back to using the build in config system instead of a database. private static final String[] V = { "1.21.1", "1.21.3", "1.21.4" }; private static final String[] PAPER = { "Paper" }; diff --git a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java index bf41f58..f233c05 100644 --- a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java +++ b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java @@ -6,7 +6,6 @@ import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.xodium.vanillaplus.VanillaPlus; -import org.xodium.vanillaplus.gui.SettingsGUI; import com.mojang.brigadier.Command; import io.papermc.paper.command.brigadier.Commands; @@ -42,7 +41,7 @@ private static interface PERMS { p.sendMessage(MSG.PERM_ERR); return 0; } - SettingsGUI.openInventory(p); + // TODO: add reload config. return Command.SINGLE_SUCCESS; } cs.sendMessage(MSG.PLAYER_ONLY_CMD); diff --git a/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java b/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java deleted file mode 100644 index 6f05841..0000000 --- a/src/main/java/org/xodium/vanillaplus/gui/SettingsGUI.java +++ /dev/null @@ -1,79 +0,0 @@ -package org.xodium.vanillaplus.gui; - -import java.util.List; -import java.util.Map; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.event.Listener; -import org.bukkit.event.EventHandler; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryDragEvent; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; -import org.bukkit.Material; -import org.bukkit.inventory.meta.ItemMeta; -import org.xodium.vanillaplus.Database; -import org.xodium.vanillaplus.VanillaPlus; - -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.minimessage.MiniMessage; - -public class SettingsGUI implements Listener { - private static final Database DB = new Database(); - private static final MiniMessage MM = MiniMessage.miniMessage(); - private static final Component GUI_TITLE = MM.deserialize(VanillaPlus.MM_HEX_PREFIX + "Settings"); - private static Inventory INV; - - static { - populateInventory(); - } - - private static void populateInventory() { - Map settings = DB.getAllData(); - int size = Math.min(54, Math.max(9, ((settings.size() - 1) / 9 + 1) * 9)); - INV = Bukkit.createInventory(null, size, GUI_TITLE); - - int slot = 0; - for (Map.Entry entry : settings.entrySet()) { - if (slot >= INV.getSize()) - break; - INV.setItem(slot++, createItem(Material.PAPER, "" + entry.getKey(), "" + entry.getValue())); - } - } - - // TODO: use directly instead. - @SuppressWarnings("unused") - private static void updateSettingInDatabase(String name, String material) { - DB.setData(name, material, false); - } - - public static void openInventory(Player p) { - p.openInventory(INV); - } - - private static ItemStack createItem(Material m, String name, String lore) { - ItemStack is = new ItemStack(m, 1); - ItemMeta im = is.getItemMeta(); - im.displayName(MM.deserialize(name)); - im.lore(List.of(MM.deserialize(lore))); - is.setItemMeta(im); - return is; - } - - @EventHandler - public void onInventoryClick(InventoryClickEvent e) { - if (e.getInventory().equals(INV) && e.getCurrentItem() != null - && e.getCurrentItem().getType() != Material.AIR) { - e.setCancelled(true); - } - } - - // TODO: doesnt work. - @EventHandler - public void onInventoryDrag(InventoryDragEvent e) { - if (e.getInventory().equals(INV)) { - e.setCancelled(true); - } - } -} diff --git a/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java b/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java deleted file mode 100644 index a9aa700..0000000 --- a/src/main/java/org/xodium/vanillaplus/modules/ElevatorModule.java +++ /dev/null @@ -1,91 +0,0 @@ -package org.xodium.vanillaplus.modules; - -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.Particle; -import org.bukkit.Sound; -import org.bukkit.World; -import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.player.PlayerMoveEvent; -import org.xodium.vanillaplus.Database; -import org.xodium.vanillaplus.interfaces.ModuleInterface; - -public class ElevatorModule implements ModuleInterface { - private final String cn = getClass().getSimpleName(); - private static final Database DB = new Database(); - private static final double TELEPORT_OFFSET = 0.5; - private static final int PARTICLE_COUNT = 20; - private static final float SOUND_VOLUME = 1f; - private static final float SOUND_PITCH = 1f; - private static final int UP_INCREMENT = 1; - private static final int DOWN_INCREMENT = -1; - - @EventHandler - public void onPlayerMove(PlayerMoveEvent e) { - Player p = e.getPlayer(); - Block b = p.getLocation().getBlock().getRelative(BlockFace.DOWN); - if (isElevator(b)) { - handleElevatorMovement(p); - } - } - - private void handleElevatorMovement(Player p) { - if (p.getVelocity().getY() > 0) { - detectOtherElevator(p, true); - } else if (p.isSneaking()) { - detectOtherElevator(p, false); - } - } - - private static boolean isElevator(Block b) { - return b.getType() == Material.HEAVY_WEIGHTED_PRESSURE_PLATE - && b.getRelative(BlockFace.DOWN).getType() == Material.NOTE_BLOCK; - } - - private void detectOtherElevator(Player p, boolean goingUp) { - World w = p.getWorld(); - int increment = goingUp ? UP_INCREMENT : DOWN_INCREMENT; - int x = p.getLocation().getBlockX(); - int y = p.getLocation().getBlockY() + increment; - int z = p.getLocation().getBlockZ(); - - while (y >= w.getMinHeight() && y <= w.getMaxHeight()) { - Block b = w.getBlockAt(x, y, z); - if (isElevator(b) && hasEnoughSpace(b)) { - teleportPlayer(p, b.getLocation()); - return; - } - y += increment; - } - } - - private static boolean hasEnoughSpace(Block b) { - Block above = b.getRelative(BlockFace.UP); - return above.getType() == Material.AIR && above.getRelative(BlockFace.UP).getType() == Material.AIR; - } - - private void teleportPlayer(Player p, Location l) { - Location tpl = l.add(TELEPORT_OFFSET, 1, TELEPORT_OFFSET); - playTeleportEffects(p); - p.teleport(tpl); - p.playSound(tpl, Sound.ENTITY_ENDERMAN_TELEPORT, SOUND_VOLUME, SOUND_PITCH); - playTeleportEffects(p); - } - - private void playTeleportEffects(Player p) { - p.getWorld().spawnParticle(Particle.PORTAL, p.getLocation(), PARTICLE_COUNT); - } - - @Override - public boolean enabled() { - return DB.getData(cn + CONFIG.ENABLE, Boolean.class); - } - - @Override - public void config() { - DB.setData(cn + CONFIG.ENABLE, true); - } -} From 926ae332912c14e94ceeb0246012be81ed371469 Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Mon, 16 Dec 2024 11:07:35 +0000 Subject: [PATCH 18/20] - --- .../java/org/xodium/vanillaplus/Database.java | 115 ------------------ .../vanillaplus/modules/DoorsModule.java | 2 - .../vanillaplus/modules/SaplingModule.java | 2 - src/main/resources/config.yml | 0 4 files changed, 119 deletions(-) delete mode 100644 src/main/java/org/xodium/vanillaplus/Database.java create mode 100644 src/main/resources/config.yml diff --git a/src/main/java/org/xodium/vanillaplus/Database.java b/src/main/java/org/xodium/vanillaplus/Database.java deleted file mode 100644 index ee6eeb6..0000000 --- a/src/main/java/org/xodium/vanillaplus/Database.java +++ /dev/null @@ -1,115 +0,0 @@ -package org.xodium.vanillaplus; - -import java.io.File; -import java.math.BigDecimal; -import java.sql.Connection; -import java.sql.Date; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Function; - -public class Database { - private Connection conn; - private static final VanillaPlus VP = VanillaPlus.getInstance(); - - private static final String DB_URL_PREFIX = "jdbc:sqlite:"; - private static final String DB_FILE = "vanillaplus.db"; - - private static final String INIT_TABLES = "CREATE TABLE IF NOT EXISTS config (key TEXT PRIMARY KEY, value TEXT)"; - private static final String SET_DATA_INITIAL = "INSERT INTO config (key, value) SELECT ?, ? WHERE NOT EXISTS (SELECT 1 FROM config WHERE key = ?)"; - private static final String SET_DATA_UPSERT = "INSERT INTO config (key, value) VALUES (?, ?) " - + "ON CONFLICT(key) DO UPDATE SET value = EXCLUDED.value"; - private static final String GET_DATA = "SELECT value FROM config WHERE key = ?"; - private static final String GET_ALL_DATA = "SELECT key, value FROM config"; - private static final String GET_DATA_COLUMN_VALUE = "value"; - - private static final Map, Function> TYPE_PARSERS = new HashMap<>() { - { - put(Boolean.class, Boolean::parseBoolean); - put(Long.class, Long::parseLong); - put(Integer.class, Integer::parseInt); - put(Double.class, Double::parseDouble); - put(Float.class, Float::parseFloat); - put(BigDecimal.class, BigDecimal::new); - put(String.class, Function.identity()); - put(Date.class, Date::valueOf); - } - }; - - { - try { - VP.getDataFolder().mkdirs(); - conn = DriverManager - .getConnection(DB_URL_PREFIX + new File(VP.getDataFolder(), DB_FILE).getAbsolutePath()); - initTables(); - } catch (SQLException err) { - err.printStackTrace(); - } - } - - private void initTables() { - try (PreparedStatement stmt = conn.prepareStatement(INIT_TABLES)) { - stmt.executeUpdate(); - } catch (SQLException err) { - err.printStackTrace(); - } - } - - public void setData(String key, Object value) { - setData(key, value, true); - } - - public void setData(String key, Object value, boolean initial) { - String sql = initial ? SET_DATA_INITIAL : SET_DATA_UPSERT; - try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setString(1, key); - stmt.setObject(2, value instanceof Boolean ? ((Boolean) value ? "true" : "false") : value); - if (initial) { - stmt.setString(3, key); - } - stmt.executeUpdate(); - } catch (SQLException err) { - err.printStackTrace(); - } - } - - public T getData(String key, Class type) { - try (PreparedStatement stmt = conn.prepareStatement(GET_DATA)) { - stmt.setString(1, key); - try (ResultSet rs = stmt.executeQuery()) { - if (rs.next()) { - String value = rs.getString(GET_DATA_COLUMN_VALUE); - Function parser = TYPE_PARSERS.get(type); - if (parser != null) { - return type.cast(parser.apply(value)); - } - throw new ClassCastException("Unsupported type: " + type.getSimpleName()); - } else { - throw new IllegalArgumentException("No data found for key: " + key); - } - } - } catch (SQLException | IllegalArgumentException err) { - err.printStackTrace(); - return null; - } - } - - public Map getAllData() { - Map data = new HashMap<>(); - try (PreparedStatement stmt = conn.prepareStatement(GET_ALL_DATA); - ResultSet rs = stmt.executeQuery()) { - while (rs.next()) { - String key = rs.getString("key"); - String value = rs.getString("value"); - data.put(key, value); - } - } catch (SQLException err) { - err.printStackTrace(); - } - return data; - } -} \ No newline at end of file diff --git a/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java b/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java index c0c36df..4784a34 100644 --- a/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java +++ b/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java @@ -30,7 +30,6 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.scheduler.BukkitRunnable; -import org.xodium.vanillaplus.Database; import org.xodium.vanillaplus.VanillaPlus; import org.xodium.vanillaplus.interfaces.ModuleInterface; import org.xodium.vanillaplus.records.AdjacentBlockRecord; @@ -41,7 +40,6 @@ public class DoorsModule implements ModuleInterface { private final String cn = getClass().getSimpleName(); private static final VanillaPlus VP = VanillaPlus.getInstance(); - private static final Database DB = new Database(); private static interface CONFIG extends ModuleInterface.CONFIG { // Sound settings diff --git a/src/main/java/org/xodium/vanillaplus/modules/SaplingModule.java b/src/main/java/org/xodium/vanillaplus/modules/SaplingModule.java index 35e79f4..b706458 100644 --- a/src/main/java/org/xodium/vanillaplus/modules/SaplingModule.java +++ b/src/main/java/org/xodium/vanillaplus/modules/SaplingModule.java @@ -1,11 +1,9 @@ package org.xodium.vanillaplus.modules; -import org.xodium.vanillaplus.Database; import org.xodium.vanillaplus.interfaces.ModuleInterface; public class SaplingModule implements ModuleInterface { private final String cn = getClass().getSimpleName(); - private static final Database DB = new Database(); @Override public boolean enabled() { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..e69de29 From a62be5074b9024d325713a7b8dfa9d579f0f6fc6 Mon Sep 17 00:00:00 2001 From: illyrius666 Date: Mon, 16 Dec 2024 13:51:49 +0100 Subject: [PATCH 19/20] Refactor configuration system and remove dev-specific files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace custom database-based configuration handling with Bukkit’s FileConfiguration API for consistency and maintainability. Remove `.devcontainer` files, VS Code settings, and tasks files to clean up unused development-specific configurations. Add a default `config.yml` for module settings. Signed-off-by: illyrius666 --- .devcontainer/devcontainer.json | 28 --- .gitignore | 3 - .vscode/settings.json | 4 - .vscode/tasks.json | 12 -- .../org/xodium/vanillaplus/ModuleManager.java | 6 +- .../org/xodium/vanillaplus/VanillaPlus.java | 5 +- .../vanillaplus/commands/ReloadCommand.java | 49 ++--- .../interfaces/ModuleInterface.java | 2 +- .../vanillaplus/modules/DoorsModule.java | 177 ++++++++---------- .../vanillaplus/modules/SaplingModule.java | 9 +- src/main/resources/config.yml | 17 ++ 11 files changed, 137 insertions(+), 175 deletions(-) delete mode 100644 .devcontainer/devcontainer.json delete mode 100644 .vscode/settings.json delete mode 100644 .vscode/tasks.json diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json deleted file mode 100644 index 43335aa..0000000 --- a/.devcontainer/devcontainer.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "image": "mcr.microsoft.com/devcontainers/java", - "remoteUser": "root", - "features": { - "ghcr.io/devcontainers/features/common-utils:2": { - "configureZshAsDefaultShell": true, - "installOhMyZsh": true, - "upgradePackages": true - }, - "ghcr.io/devcontainers/features/git:1": {} - }, - "customizations": { - "vscode": { - "extensions": [ - "esbenp.prettier-vscode", - "formulahendry.code-runner", - "github.copilot", - "github.vscode-github-actions", - "pkief.material-icon-theme", - "qwtel.sqlite-viewer", - "usernamehw.errorlens", - "vsls-contrib.gistfs" - ] - } - }, - "postCreateCommand": "sudo apt update -y && sudo apt install gradle -y", - "shutdownAction": "stopContainer" -} diff --git a/.gitignore b/.gitignore index b936cdc..a61e10e 100644 --- a/.gitignore +++ b/.gitignore @@ -10,9 +10,6 @@ build/ .idea/ *.iml -# VS Code specific files -.vscode/ - # OS generated files .DS_Store Thumbs.db diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 3a655b7..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "java.import.gradle.java.home": "/usr/lib/jvm/msopenjdk-current", - "java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx4G -Xms100m -Xlog:disable" -} diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index b1d9f76..0000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "version": "2.0.0", - "tasks": [ - { - "label": "buildJar", - "type": "shell", - "command": "./gradlew build", - "options": { "cwd": "${workspaceFolder}" }, - "group": { "kind": "build", "isDefault": true } - } - ] -} diff --git a/src/main/java/org/xodium/vanillaplus/ModuleManager.java b/src/main/java/org/xodium/vanillaplus/ModuleManager.java index db4772c..3f4c9d9 100644 --- a/src/main/java/org/xodium/vanillaplus/ModuleManager.java +++ b/src/main/java/org/xodium/vanillaplus/ModuleManager.java @@ -2,14 +2,14 @@ import org.xodium.vanillaplus.interfaces.ModuleInterface; import org.xodium.vanillaplus.modules.DoorsModule; -import java.util.List; + +import java.util.stream.Stream; public class ModuleManager { private static final VanillaPlus VP = VanillaPlus.getInstance(); static { - List.of(new DoorsModule()) - .stream() + Stream.of(new DoorsModule()) .peek(ModuleInterface::config) .filter(ModuleInterface::enabled) .forEach(mod -> { diff --git a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java index ba35ed4..7e666d8 100644 --- a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java +++ b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java @@ -1,9 +1,10 @@ package org.xodium.vanillaplus; -import java.util.Arrays; import org.bukkit.plugin.java.JavaPlugin; import org.xodium.vanillaplus.commands.ReloadCommand; +import java.util.Arrays; + public class VanillaPlus extends JavaPlugin { // TODO: revert back to using the build in config system instead of a database. @@ -14,7 +15,6 @@ public class VanillaPlus extends JavaPlugin { + String.join(", ", V); public static final String PREFIX = "[VanillaPlus] "; - public static final String MM_HEX_PREFIX = "<#CB2D3E>"; public static VanillaPlus getInstance() { return getPlugin(VanillaPlus.class); @@ -30,6 +30,7 @@ public void onEnable() { disablePlugin(IS_SUPPORTED_VERSION_MSG); return; } + saveDefaultConfig(); new ReloadCommand(); new ModuleManager(); } diff --git a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java index f233c05..3529670 100644 --- a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java +++ b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java @@ -1,35 +1,21 @@ package org.xodium.vanillaplus.commands; -import java.util.List; - -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; -import org.xodium.vanillaplus.VanillaPlus; - import com.mojang.brigadier.Command; import io.papermc.paper.command.brigadier.Commands; import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.xodium.vanillaplus.VanillaPlus; + +import java.util.List; public class ReloadCommand { private static final VanillaPlus VP = VanillaPlus.getInstance(); private static final MiniMessage MM = MiniMessage.miniMessage(); - private static interface MSG { - @NotNull - Component PERM_ERR = MM.deserialize(VanillaPlus.PREFIX - + "You do not have permission to use this command!"); - @NotNull - Component PLAYER_ONLY_CMD = MM - .deserialize("This command can only be run by a player."); - } - - private static interface PERMS { - String RELOAD = VP.getClass().getSimpleName() + ".reload"; - } - static { VP.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, e -> { e.registrar().register( @@ -41,15 +27,30 @@ private static interface PERMS { p.sendMessage(MSG.PERM_ERR); return 0; } - // TODO: add reload config. - return Command.SINGLE_SUCCESS; } - cs.sendMessage(MSG.PLAYER_ONLY_CMD); - return 0; + VP.reloadConfig(); + cs.sendMessage(MSG.RELOAD_SUCC_MSG); + VP.getLogger().info(MSG.RELOAD_SUCC_LOG_MSG); + return Command.SINGLE_SUCCESS; }) .build(), "Opens the GUI", List.of("vp")); }); } + + private interface MSG { + String PREFIX = "[VanillaPlus] "; + @NotNull + Component PERM_ERR = MM.deserialize(VanillaPlus.PREFIX + + "You do not have permission to use this command!"); + @NotNull + Component RELOAD_SUCC_MSG = MM + .deserialize(PREFIX + "Configuration reloaded successfully."); + String RELOAD_SUCC_LOG_MSG = "Configuration reloaded successfully."; + } + + private interface PERMS { + String RELOAD = VP.getClass().getSimpleName() + ".reload"; + } } diff --git a/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java b/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java index 0483b53..61a68d9 100644 --- a/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java +++ b/src/main/java/org/xodium/vanillaplus/interfaces/ModuleInterface.java @@ -3,7 +3,7 @@ import org.bukkit.event.Listener; public interface ModuleInterface extends Listener { - static interface CONFIG { + interface CONFIG { String ENABLE = ".enable"; } diff --git a/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java b/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java index 4784a34..58b2a2e 100644 --- a/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java +++ b/src/main/java/org/xodium/vanillaplus/modules/DoorsModule.java @@ -1,20 +1,7 @@ package org.xodium.vanillaplus.modules; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Optional; - -import org.bukkit.event.Event; -import org.bukkit.Bukkit; -import org.bukkit.GameMode; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.NamespacedKey; -import org.bukkit.Registry; -import org.bukkit.Sound; -import org.bukkit.SoundCategory; -import org.bukkit.World; +import com.google.common.base.Enums; +import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.data.Bisected; @@ -23,7 +10,9 @@ import org.bukkit.block.data.type.Door; import org.bukkit.block.data.type.Gate; import org.bukkit.block.data.type.TrapDoor; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; +import org.bukkit.event.Event; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.block.Action; @@ -34,40 +23,13 @@ import org.xodium.vanillaplus.interfaces.ModuleInterface; import org.xodium.vanillaplus.records.AdjacentBlockRecord; -import com.google.common.base.Enums; +import java.util.*; // TODO: refactor. public class DoorsModule implements ModuleInterface { private final String cn = getClass().getSimpleName(); private static final VanillaPlus VP = VanillaPlus.getInstance(); - - private static interface CONFIG extends ModuleInterface.CONFIG { - // Sound settings - String SOUND_KNOCK_CATEGORY = ".sound_knock_category"; - String SOUND_KNOCK_PITCH = ".sound_knock_pitch"; - String SOUND_KNOCK_VOLUME = ".sound_knock_volume"; - String SOUND_KNOCK_WOOD = ".sound_knock_wood"; - - // Behavior settings - String ALLOW_AUTOCLOSE = ".allow_autoclose"; - String ALLOW_DOUBLEDOORS = ".allow_doubledoors"; - String ALLOW_KNOCKING = ".allow_knocking"; - String ALLOW_KNOCKING_GATES = ".allow_knocking_gates"; - String ALLOW_KNOCKING_TRAPDOORS = ".allow_knocking_trapdoors"; - String KNOCKING_REQUIRES_EMPTY_HAND = ".knocking_requires_empty_hand"; - String KNOCKING_REQUIRES_SHIFT = ".knocking_requires_shift"; - - // Auto-close settings - String AUTOCLOSE_DELAY = ".autoclose_delay"; - } - - private static interface PERMS { - String USE = VP.getClass().getSimpleName() + ".doubledoors"; - String KNOCK = VP.getClass().getSimpleName() + ".knock"; - String AUTOCLOSE = VP.getClass().getSimpleName() + ".autoclose"; - } - - private final HashMap autoClose = new HashMap<>(); + private static final FileConfiguration FC = VP.getConfig(); private static final AdjacentBlockRecord[] POSSIBLE_NEIGHBOURS = { new AdjacentBlockRecord(0, -1, Door.Hinge.RIGHT, BlockFace.EAST), new AdjacentBlockRecord(0, 1, Door.Hinge.LEFT, BlockFace.EAST), @@ -81,6 +43,7 @@ private static interface PERMS { new AdjacentBlockRecord(-1, 0, Door.Hinge.RIGHT, BlockFace.NORTH), new AdjacentBlockRecord(1, 0, Door.Hinge.LEFT, BlockFace.NORTH) }; + private final HashMap autoClose = new HashMap<>(); { Bukkit.getScheduler().runTaskTimer(VP, () -> { @@ -91,8 +54,7 @@ private static interface PERMS { Long time = entry.getValue(); if (System.currentTimeMillis() < time) continue; - if (b.getBlockData() instanceof Openable) { - Openable openable = (Openable) b.getBlockData(); + if (b.getBlockData() instanceof Openable openable) { if (openable.isOpen()) { if (openable instanceof Door) { Block otherDoor = getOtherPart((Door) openable, b); @@ -125,25 +87,24 @@ public void onRightClick(PlayerInteractEvent e) { || e.useItemInHand() == Event.Result.DENY || !e.getPlayer().hasPermission(PERMS.USE) || !(blockData instanceof Door || blockData instanceof Gate) - || !DB.getData(cn + CONFIG.ALLOW_DOUBLEDOORS, Boolean.class)) + || !FC.getBoolean(cn + CONFIG.ALLOW_DOUBLEDOORS)) return; if (blockData instanceof Door) { Door door = getBottomDoor((Door) blockData, clickedBlock); Block otherDoorBlock = getOtherPart(door, clickedBlock); - if (otherDoorBlock != null && otherDoorBlock.getBlockData() instanceof Door) { - Door otherDoor = (Door) otherDoorBlock.getBlockData(); + if (otherDoorBlock != null && otherDoorBlock.getBlockData() instanceof Door otherDoor) { toggleOtherDoor(clickedBlock, otherDoorBlock, !otherDoor.isOpen()); if (e.getPlayer().hasPermission(PERMS.AUTOCLOSE)) { autoClose.put(otherDoorBlock, System.currentTimeMillis() - + DB.getData(cn + CONFIG.AUTOCLOSE_DELAY, Long.class) * 1000); + + FC.getLong(cn + CONFIG.AUTOCLOSE_DELAY) * 1000); } } } if (e.getPlayer().hasPermission(PERMS.AUTOCLOSE)) { autoClose.put(clickedBlock, - System.currentTimeMillis() + DB.getData(cn + CONFIG.AUTOCLOSE_DELAY, Long.class) * 1000); + System.currentTimeMillis() + FC.getLong(cn + CONFIG.AUTOCLOSE_DELAY) * 1000); } } @@ -154,8 +115,8 @@ public void onKnock(PlayerInteractEvent e) { if (p.getGameMode() == GameMode.CREATIVE || p.getGameMode() == GameMode.SPECTATOR || !p.hasPermission(PERMS.KNOCK) || e.getAction() != Action.LEFT_CLICK_BLOCK || e.getHand() != EquipmentSlot.HAND - || (DB.getData(cn + CONFIG.KNOCKING_REQUIRES_SHIFT, Boolean.class) && !p.isSneaking()) - || (DB.getData(cn + CONFIG.KNOCKING_REQUIRES_EMPTY_HAND, Boolean.class) + || (FC.getBoolean(cn + CONFIG.KNOCKING_REQUIRES_SHIFT) && !p.isSneaking()) + || (FC.getBoolean(cn + CONFIG.KNOCKING_REQUIRES_EMPTY_HAND) && p.getInventory().getItemInMainHand().getType() != Material.AIR) || e.getClickedBlock() == null) return; @@ -163,9 +124,9 @@ public void onKnock(PlayerInteractEvent e) { Block block = e.getClickedBlock(); BlockData blockData = block.getBlockData(); - if ((blockData instanceof Door && DB.getData(cn + CONFIG.ALLOW_KNOCKING, Boolean.class)) - || (blockData instanceof TrapDoor && DB.getData(cn + CONFIG.ALLOW_KNOCKING_TRAPDOORS, Boolean.class)) - || (blockData instanceof Gate && DB.getData(cn + CONFIG.ALLOW_KNOCKING_GATES, Boolean.class))) { + if ((blockData instanceof Door && FC.getBoolean(cn + CONFIG.ALLOW_KNOCKING)) + || (blockData instanceof TrapDoor && FC.getBoolean(cn + CONFIG.ALLOW_KNOCKING_TRAPDOORS)) + || (blockData instanceof Gate && FC.getBoolean(cn + CONFIG.ALLOW_KNOCKING_GATES))) { playKnockSound(block); } } @@ -177,18 +138,41 @@ public void playKnockSound(Block block) { .ofNullable( Registry.SOUNDS .get(NamespacedKey.minecraft( - DB.getData(cn + CONFIG.SOUND_KNOCK_WOOD, String.class).toLowerCase()))) + Objects.requireNonNull(FC.getString(cn + CONFIG.SOUND_KNOCK_WOOD)).toLowerCase()))) .orElse(Sound.ITEM_SHIELD_BLOCK); SoundCategory category = Enums .getIfPresent(SoundCategory.class, - DB.getData(cn + CONFIG.SOUND_KNOCK_CATEGORY, String.class).toUpperCase()) + Objects.requireNonNull(FC.getString(cn + CONFIG.SOUND_KNOCK_CATEGORY)).toUpperCase()) .or(SoundCategory.BLOCKS); - float volume = DB.getData(cn + CONFIG.SOUND_KNOCK_VOLUME, Float.class); - float pitch = DB.getData(cn + CONFIG.SOUND_KNOCK_PITCH, Float.class); + float volume = (float) FC.getInt(cn + CONFIG.SOUND_KNOCK_VOLUME); + float pitch = (float) FC.getInt(cn + CONFIG.SOUND_KNOCK_PITCH); world.playSound(loc, sound, category, volume, pitch); } + public void toggleOtherDoor(Block block, Block otherBlock, boolean open) { + if (!(block.getBlockData() instanceof Door door) || !(otherBlock.getBlockData() instanceof Door otherDoor)) + return; + + new BukkitRunnable() { + @Override + public void run() { + if (!(otherBlock.getBlockData() instanceof Door)) + return; + Door newDoor = (Door) block.getBlockData(); + if (newDoor.isOpen() == door.isOpen()) { + return; + } + DoorsModule.toggleDoor(otherBlock, otherDoor, open); + } + }.runTaskLater(VP, 1L); + } + + @Override + public boolean enabled() { + return FC.getBoolean(cn + CONFIG.ENABLE); + } + public static void toggleDoor(Block doorBlock, Openable openable, boolean open) { openable.setOpen(open); doorBlock.setBlockData(openable); @@ -221,46 +205,47 @@ public Block getOtherPart(Door door, Block block) { return null; } - public void toggleOtherDoor(Block block, Block otherBlock, boolean open) { - if (!(block.getBlockData() instanceof Door) || !(otherBlock.getBlockData() instanceof Door)) - return; + @Override + public void config() { + FC.addDefault(cn + CONFIG.ENABLE, true); + FC.addDefault(cn + CONFIG.SOUND_KNOCK_CATEGORY, "BLOCKS"); + FC.addDefault(cn + CONFIG.SOUND_KNOCK_PITCH, 1.0); + FC.addDefault(cn + CONFIG.SOUND_KNOCK_VOLUME, 1.0); + FC.addDefault(cn + CONFIG.SOUND_KNOCK_WOOD, "entity_zombie_attack_wooden_door"); + FC.addDefault(cn + CONFIG.ALLOW_AUTOCLOSE, true); + FC.addDefault(cn + CONFIG.ALLOW_DOUBLEDOORS, true); + FC.addDefault(cn + CONFIG.ALLOW_KNOCKING, true); + FC.addDefault(cn + CONFIG.ALLOW_KNOCKING_GATES, true); + FC.addDefault(cn + CONFIG.ALLOW_KNOCKING_TRAPDOORS, true); + FC.addDefault(cn + CONFIG.KNOCKING_REQUIRES_EMPTY_HAND, true); + FC.addDefault(cn + CONFIG.KNOCKING_REQUIRES_SHIFT, false); + FC.addDefault(cn + CONFIG.AUTOCLOSE_DELAY, 6); + VP.saveConfig(); + } - Door door = (Door) block.getBlockData(); - Door otherDoor = (Door) otherBlock.getBlockData(); + private interface CONFIG extends ModuleInterface.CONFIG { + // Sound settings + String SOUND_KNOCK_CATEGORY = ".sound_knock_category"; + String SOUND_KNOCK_PITCH = ".sound_knock_pitch"; + String SOUND_KNOCK_VOLUME = ".sound_knock_volume"; + String SOUND_KNOCK_WOOD = ".sound_knock_wood"; - new BukkitRunnable() { - @Override - public void run() { - if (!(otherBlock.getBlockData() instanceof Door)) - return; - Door newDoor = (Door) block.getBlockData(); - if (newDoor.isOpen() == door.isOpen()) { - return; - } - DoorsModule.toggleDoor(otherBlock, otherDoor, open); - } - }.runTaskLater(VP, 1L); - } + // Behavior settings + String ALLOW_AUTOCLOSE = ".allow_autoclose"; + String ALLOW_DOUBLEDOORS = ".allow_doubledoors"; + String ALLOW_KNOCKING = ".allow_knocking"; + String ALLOW_KNOCKING_GATES = ".allow_knocking_gates"; + String ALLOW_KNOCKING_TRAPDOORS = ".allow_knocking_trapdoors"; + String KNOCKING_REQUIRES_EMPTY_HAND = ".knocking_requires_empty_hand"; + String KNOCKING_REQUIRES_SHIFT = ".knocking_requires_shift"; - @Override - public boolean enabled() { - return DB.getData(cn + CONFIG.ENABLE, Boolean.class); + // Auto-close settings + String AUTOCLOSE_DELAY = ".autoclose_delay"; } - @Override - public void config() { - DB.setData(cn + CONFIG.ENABLE, true); - DB.setData(cn + CONFIG.SOUND_KNOCK_CATEGORY, "BLOCKS"); - DB.setData(cn + CONFIG.SOUND_KNOCK_PITCH, 1.0); - DB.setData(cn + CONFIG.SOUND_KNOCK_VOLUME, 1.0); - DB.setData(cn + CONFIG.SOUND_KNOCK_WOOD, "entity_zombie_attack_wooden_door"); - DB.setData(cn + CONFIG.ALLOW_AUTOCLOSE, true); - DB.setData(cn + CONFIG.ALLOW_DOUBLEDOORS, true); - DB.setData(cn + CONFIG.ALLOW_KNOCKING, true); - DB.setData(cn + CONFIG.ALLOW_KNOCKING_GATES, true); - DB.setData(cn + CONFIG.ALLOW_KNOCKING_TRAPDOORS, true); - DB.setData(cn + CONFIG.KNOCKING_REQUIRES_EMPTY_HAND, true); - DB.setData(cn + CONFIG.KNOCKING_REQUIRES_SHIFT, false); - DB.setData(cn + CONFIG.AUTOCLOSE_DELAY, 6); + private interface PERMS { + String USE = VP.getClass().getSimpleName() + ".doubledoors"; + String KNOCK = VP.getClass().getSimpleName() + ".knock"; + String AUTOCLOSE = VP.getClass().getSimpleName() + ".autoclose"; } } diff --git a/src/main/java/org/xodium/vanillaplus/modules/SaplingModule.java b/src/main/java/org/xodium/vanillaplus/modules/SaplingModule.java index b706458..c06b291 100644 --- a/src/main/java/org/xodium/vanillaplus/modules/SaplingModule.java +++ b/src/main/java/org/xodium/vanillaplus/modules/SaplingModule.java @@ -1,18 +1,23 @@ package org.xodium.vanillaplus.modules; +import org.bukkit.configuration.file.FileConfiguration; +import org.xodium.vanillaplus.VanillaPlus; import org.xodium.vanillaplus.interfaces.ModuleInterface; public class SaplingModule implements ModuleInterface { private final String cn = getClass().getSimpleName(); + private static final VanillaPlus VP = VanillaPlus.getInstance(); + private static final FileConfiguration FC = VP.getConfig(); @Override public boolean enabled() { - return DB.getData(cn + CONFIG.ENABLE, Boolean.class); + return FC.getBoolean(cn + CONFIG.ENABLE); } @Override public void config() { - DB.setData(cn + CONFIG.ENABLE, true); + FC.addDefault(cn + CONFIG.ENABLE, true); + VP.saveConfig(); } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e69de29..3ebc95d 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -0,0 +1,17 @@ +DoorsModule: + enable: true + sound_knock_category: "BLOCKS" + sound_knock_pitch: 1.0 + sound_knock_volume: 1.0 + sound_knock_wood: "entity_zombie_attack_wooden_door" + allow_autoclose: true + allow_doubledoors: true + allow_knocking: true + allow_knocking_gates: true + allow_knocking_trapdoors: true + knocking_requires_empty_hand: true + knocking_requires_shift: false + autoclose_delay: 6 + +SaplingModule: + enable: false \ No newline at end of file From 0719fbc13ad755b78abe4c267154f44e6b34f873 Mon Sep 17 00:00:00 2001 From: Illyrius <28700752+illyrius666@users.noreply.github.com> Date: Mon, 16 Dec 2024 14:32:32 +0100 Subject: [PATCH 20/20] + some small fixes Signed-off-by: illyrius666 --- build.gradle.kts | 2 -- .../org/xodium/vanillaplus/ModuleManager.java | 3 +- .../org/xodium/vanillaplus/VanillaPlus.java | 4 +-- .../vanillaplus/commands/ReloadCommand.java | 36 +++++++++---------- 4 files changed, 20 insertions(+), 25 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index dedee55..3ec74c7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -15,9 +15,7 @@ repositories { } dependencies { - compileOnly("io.papermc.paper:paper-api:1.21.1-R0.1-SNAPSHOT") compileOnly("io.papermc.paper:paper-api:1.21.3-R0.1-SNAPSHOT") - compileOnly("io.papermc.paper:paper-api:1.21.4-R0.1-SNAPSHOT") implementation("net.kyori:adventure-api:4.17.0") } diff --git a/src/main/java/org/xodium/vanillaplus/ModuleManager.java b/src/main/java/org/xodium/vanillaplus/ModuleManager.java index 3f4c9d9..621f5e1 100644 --- a/src/main/java/org/xodium/vanillaplus/ModuleManager.java +++ b/src/main/java/org/xodium/vanillaplus/ModuleManager.java @@ -2,6 +2,7 @@ import org.xodium.vanillaplus.interfaces.ModuleInterface; import org.xodium.vanillaplus.modules.DoorsModule; +import org.xodium.vanillaplus.modules.SaplingModule; import java.util.stream.Stream; @@ -9,7 +10,7 @@ public class ModuleManager { private static final VanillaPlus VP = VanillaPlus.getInstance(); static { - Stream.of(new DoorsModule()) + Stream.of(new DoorsModule(), new SaplingModule()) .peek(ModuleInterface::config) .filter(ModuleInterface::enabled) .forEach(mod -> { diff --git a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java index 7e666d8..ea48cd5 100644 --- a/src/main/java/org/xodium/vanillaplus/VanillaPlus.java +++ b/src/main/java/org/xodium/vanillaplus/VanillaPlus.java @@ -6,9 +6,7 @@ import java.util.Arrays; public class VanillaPlus extends JavaPlugin { - // TODO: revert back to using the build in config system instead of a database. - - private static final String[] V = { "1.21.1", "1.21.3", "1.21.4" }; + private static final String[] V = {"1.21.3"}; private static final String[] PAPER = { "Paper" }; private static final String IS_PAPER_MSG = "This plugin is not compatible with non-Paper servers."; private static final String IS_SUPPORTED_VERSION_MSG = "This plugin requires Paper version(s): " diff --git a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java index 3529670..9592ca1 100644 --- a/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java +++ b/src/main/java/org/xodium/vanillaplus/commands/ReloadCommand.java @@ -17,26 +17,24 @@ public class ReloadCommand { private static final MiniMessage MM = MiniMessage.miniMessage(); static { - VP.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, e -> { - e.registrar().register( - Commands.literal("vanillaplus") - .executes(ctx -> { - CommandSender cs = ctx.getSource().getSender(); - if (cs instanceof Player p) { - if (!p.hasPermission(PERMS.RELOAD)) { - p.sendMessage(MSG.PERM_ERR); - return 0; - } + VP.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, e -> e.registrar().register( + Commands.literal("vanillaplus") + .executes(ctx -> { + CommandSender cs = ctx.getSource().getSender(); + if (cs instanceof Player p) { + if (!p.hasPermission(PERMS.RELOAD)) { + p.sendMessage(MSG.PERM_ERR); + return 0; } - VP.reloadConfig(); - cs.sendMessage(MSG.RELOAD_SUCC_MSG); - VP.getLogger().info(MSG.RELOAD_SUCC_LOG_MSG); - return Command.SINGLE_SUCCESS; - }) - .build(), - "Opens the GUI", - List.of("vp")); - }); + } + VP.reloadConfig(); + cs.sendMessage(MSG.RELOAD_SUCC_MSG); + VP.getLogger().info(MSG.RELOAD_SUCC_LOG_MSG); + return Command.SINGLE_SUCCESS; + }) + .build(), + "Opens the GUI", + List.of("vp"))); } private interface MSG {