From 07106e61a220ec4137a3de200a393cf6aaa50be7 Mon Sep 17 00:00:00 2001 From: kernitus <2789734+kernitus@users.noreply.github.com> Date: Sat, 26 Oct 2024 13:19:48 +0100 Subject: [PATCH] fix: resolve elytras always unequipped by removing out-of-scope module Closes #725 --- .github/CONTRIBUTING.md | 2 +- README.md | 1 - .../plugin/OldCombatMechanics/OCMMain.java | 1 - .../module/ModuleDisableElytra.java | 121 ------------------ src/main/resources/config.yml | 6 +- 5 files changed, 2 insertions(+), 129 deletions(-) delete mode 100644 src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleDisableElytra.java diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index ca43f6fe..db9251fb 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -11,7 +11,7 @@ All contributions should be in the form of [pull requests](https://github.com/ke OldCombatMechanics uses a modular system to make sure each feature is completely independent from any other, and can be toggled off and have no impact on server performance. This means each new module must extend the `Module` class, and implement a public constructor which takes an instance of the plugin and passes the module name to the superconstructor. The `Module` class also provides an overloadable `reload()` method which is called whenever the plugin is reloaded. If you are using any class-level variables they should probably be updated in here. This should also be used for any initialisation that might need to be done if the config section is changed and `ocm reload` is called. You may then call `reload()` from the constructor. ## Module naming -The name specified in the module constructor must be the same as the one used in the config.yml. The module name must meaningfully describe the purpose of the module, for example `disable-elytra` or `old-burn-delay`. The module classes should thus be respectively named `ModuleDisableElytra` and `ModuleOldBurnDelay`. As you can see, kebab case is used for the constructor and config name, while pascal case prefixed by `Module` is used for class names. +The name specified in the module constructor must be the same as the one used in the config.yml. The module name must meaningfully describe the purpose of the module, for example `disable-offhand` or `old-burn-delay`. The module classes should thus be respectively named `ModuleDisableOffhand` and `ModuleOldBurnDelay`. As you can see, kebab case is used for the constructor and config name, while pascal case prefixed by `Module` is used for class names. ## Module configuration All module configuration is done through the Module class in a subsection of the config.yml for each module. To access config variables under the module section, you can use the methods provided by `module()`, such as `module().getBoolean("enableBlue")`. The module config section must contain an `enabled` boolean key which is used by the module system to selectively register/unregister the module, and, if applicable, a `worlds: []` list key to configure which worlds your module will work in. It is the responsibility of the module to use the `module().isEnabled(world)` to make sure this is enforced. diff --git a/README.md b/README.md index 0c0fe7e2..a58d2898 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,6 @@ Features are grouped in `module`s as listed below, and can be individually confi #### New feature disabling - Item crafting - Offhand -- Elytra - Bow boost - New attack sounds - Enderpearl cooldown diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/OCMMain.java b/src/main/java/kernitus/plugin/OldCombatMechanics/OCMMain.java index df933a1d..e73ad62d 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/OCMMain.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/OCMMain.java @@ -227,7 +227,6 @@ private void registerModules() { ModuleLoader.addModule(new ModuleDisableCrafting(this)); ModuleLoader.addModule(new ModuleDisableOffHand(this)); ModuleLoader.addModule(new ModuleOldBrewingStand(this)); - ModuleLoader.addModule(new ModuleDisableElytra(this)); ModuleLoader.addModule(new ModuleDisableProjectileRandomness(this)); ModuleLoader.addModule(new ModuleDisableBowBoost(this)); ModuleLoader.addModule(new ModuleProjectileKnockback(this)); diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleDisableElytra.java b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleDisableElytra.java deleted file mode 100644 index 8e218e45..00000000 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleDisableElytra.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ -package kernitus.plugin.OldCombatMechanics.module; - -import kernitus.plugin.OldCombatMechanics.OCMMain; -import org.bukkit.GameMode; -import org.bukkit.Material; -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; -import org.bukkit.event.inventory.ClickType; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryDragEvent; -import org.bukkit.event.inventory.InventoryType; -import org.bukkit.event.player.PlayerChangedWorldEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.PlayerInventory; - -/** - * Prevents players from equipping an elytra. - */ -public class ModuleDisableElytra extends OCMModule { - - private static final int CHEST_SLOT = 38; - private static final int OFFHAND_SLOT = 40; - - public ModuleDisableElytra(OCMMain plugin) { - super(plugin, "disable-elytra"); - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onInventoryClick(InventoryClickEvent e) { - if (!(e.getWhoClicked() instanceof Player)) return; - final Player player = (Player) e.getWhoClicked(); - - if (!isEnabled(player) || player.getGameMode() == GameMode.CREATIVE) return; - - // If they're in their own inventory, and not chests etc. - final InventoryType inventoryType = e.getInventory().getType(); - if (inventoryType != InventoryType.CRAFTING && inventoryType != InventoryType.PLAYER) return; - - final ItemStack cursor = e.getCursor(); - final ItemStack currentItem = e.getCurrentItem(); - final ClickType clickType = e.getClick(); - final PlayerInventory inv = player.getInventory(); - final int slot = e.getSlot(); - - try { - // If they used a number key to swap an elytra from the hotbar into the chest slot - if ((clickType == ClickType.NUMBER_KEY && slot == CHEST_SLOT && isElytra(inv.getItem(e.getHotbarButton()))) - - // If they are placing it into the chest slot directly - || (slot == CHEST_SLOT && isElytra(cursor)) - - // If they shift-clicked an elytra into the chest slot - || (isElytra(currentItem) && slot != CHEST_SLOT && e.isShiftClick()) - - // If they used F to swap an elytra from the offhand slot into the chest slot - || (clickType == ClickType.SWAP_OFFHAND && slot == CHEST_SLOT && isElytra(inv.getItem(OFFHAND_SLOT))) - ) - e.setCancelled(true); - } catch (NoSuchFieldError ignored) { - } // For versions below 1.16 where you couldn't use F to swap offhand in inventory - } - - private boolean isElytra(ItemStack item) { - return item != null && item.getType() == Material.ELYTRA; - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onRightClick(PlayerInteractEvent e) { - if (!isEnabled(e.getPlayer())) return; - - // Must not be able to right click while holding an elytra to wear it - final Action action = e.getAction(); - if (action != Action.RIGHT_CLICK_AIR && action != Action.RIGHT_CLICK_BLOCK) return; - - // Stop usage of item, but allow interacting with blocks - if (isElytra(e.getItem())) - e.setUseItemInHand(Event.Result.DENY); - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onDrag(InventoryDragEvent e) { - if (!isEnabled(e.getWhoClicked())) return; - - final ItemStack oldCursor = e.getOldCursor(); - - if (e.getInventorySlots().contains(CHEST_SLOT) && isElytra(oldCursor)) - e.setCancelled(true); - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onWorldChange(PlayerChangedWorldEvent e) { - onModesetChange(e.getPlayer()); - } - - @Override - public void onModesetChange(Player player) { - if (!isEnabled(player)) return; - - final PlayerInventory inventory = player.getInventory(); - final ItemStack chestplate = inventory.getChestplate(); - - if (!isElytra(chestplate)) return; - - inventory.setChestplate(new ItemStack(Material.AIR)); - - if (inventory.firstEmpty() != -1) - inventory.addItem(chestplate); - else - player.getWorld().dropItem(player.getLocation(), chestplate); - - } -} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e4251bc9..a1c661a9 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -480,10 +480,6 @@ no-lapis-enchantments: # Whether to only allow this for players with oldcombatmechanics.nolapis permission usePermission: false -disable-elytra: - # Do not allow players to wear elytra - enabled: false - disable-enderpearl-cooldown: # Disables enderpearl cooldown enabled: true @@ -580,4 +576,4 @@ debug: enabled: false # DO NOT CHANGE THIS NUMBER AS IT WILL RESET YOUR CONFIG -config-version: 65 +config-version: 66