From 6a6f2c0377de7ee2e0604417b1d57a99ea39f8f6 Mon Sep 17 00:00:00 2001 From: Jules Date: Sat, 27 Jul 2024 14:17:05 -0700 Subject: [PATCH] Fixed `mi_tools` region flag not working --- .../java/net/Indyuce/mmoitems/api/Type.java | 44 ++++++++++++++----- .../mmoitems/api/interaction/Tool.java | 12 +++-- .../mmoitems/api/interaction/UseItem.java | 13 +----- .../api/interaction/weapon/Weapon.java | 10 ++++- .../Indyuce/mmoitems/listener/ItemUse.java | 12 ++--- 5 files changed, 59 insertions(+), 32 deletions(-) diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/Type.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/Type.java index 4a8f9b9e..6b4f74c7 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/Type.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/Type.java @@ -10,12 +10,18 @@ import io.lumine.mythic.lib.skill.handler.SkillHandler; import io.lumine.mythic.lib.util.PostLoadAction; import io.lumine.mythic.lib.util.PreloadedObject; import net.Indyuce.mmoitems.MMOItems; +import net.Indyuce.mmoitems.api.interaction.*; +import net.Indyuce.mmoitems.api.interaction.weapon.Weapon; +import net.Indyuce.mmoitems.api.interaction.weapon.untargeted.Lute; +import net.Indyuce.mmoitems.api.interaction.weapon.untargeted.Musket; import net.Indyuce.mmoitems.api.item.util.identify.UnidentifiedItem; +import net.Indyuce.mmoitems.api.player.PlayerData; import net.Indyuce.mmoitems.manager.TypeManager; import net.Indyuce.mmoitems.stat.type.ItemStat; import org.apache.commons.lang.Validate; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; @@ -23,6 +29,7 @@ import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.function.BiFunction; @SuppressWarnings("unused") public class Type implements CooldownObject, PreloadedObject { @@ -43,11 +50,11 @@ public class Type implements CooldownObject, PreloadedObject { public static final Type STAFF = new Type("STAFF", ModifierSource.RANGED_WEAPON); public static final Type BOW = new Type("BOW", ModifierSource.RANGED_WEAPON); public static final Type CROSSBOW = new Type("CROSSBOW", ModifierSource.RANGED_WEAPON); - public static final Type MUSKET = new Type("MUSKET", ModifierSource.RANGED_WEAPON); - public static final Type LUTE = new Type("LUTE", ModifierSource.RANGED_WEAPON); + public static final Type MUSKET = new Type("MUSKET", ModifierSource.RANGED_WEAPON, Musket::new); + public static final Type LUTE = new Type("LUTE", ModifierSource.RANGED_WEAPON, Lute::new); // Other weapons - public static final Type TOOL = new Type("TOOL", ModifierSource.MELEE_WEAPON); + public static final Type TOOL = new Type("TOOL", ModifierSource.MELEE_WEAPON, Tool::new); // Hand Accessories public static final Type CATALYST = new Type("CATALYST", ModifierSource.HAND_ITEM); @@ -57,15 +64,16 @@ public class Type implements CooldownObject, PreloadedObject { // Other public static final Type ORNAMENT = new Type("ORNAMENT", ModifierSource.VOID); public static final Type ARMOR = new Type("ARMOR", ModifierSource.ARMOR); - public static final Type CONSUMABLE = new Type("CONSUMABLE", ModifierSource.MAINHAND_ITEM); + public static final Type CONSUMABLE = new Type("CONSUMABLE", ModifierSource.MAINHAND_ITEM, Consumable::new); public static final Type MISCELLANEOUS = new Type("MISCELLANEOUS", ModifierSource.MAINHAND_ITEM); - public static final Type GEM_STONE = new Type("GEM_STONE", ModifierSource.VOID); - public static final Type SKIN = new Type("SKIN", ModifierSource.VOID); + public static final Type GEM_STONE = new Type("GEM_STONE", ModifierSource.VOID, GemStone::new); + public static final Type SKIN = new Type("SKIN", ModifierSource.VOID, ItemSkin::new); public static final Type ACCESSORY = new Type("ACCESSORY", ModifierSource.ACCESSORY); public static final Type BLOCK = new Type("BLOCK", ModifierSource.VOID); private final String id; private final ModifierSource modifierSource; + private final BiFunction interactionProvider; private String name; @@ -112,13 +120,18 @@ public class Type implements CooldownObject, PreloadedObject { onEntityInteract = config.contains("on-entity-interact") ? MythicLib.plugin.getSkills().loadSkillHandler(config.get("on-entity-interact")) : null; }); - /** - * Hard-coded type with given parameters. Can be used by other plugins - * to create types using MMOItems API. - */ public Type(@NotNull String id, @NotNull ModifierSource modifierSource) { + this(id, modifierSource, modifierSource.isWeapon() ? Weapon::new : UseItem::new); + } + + /** + * Hard-coded type with given parameters. Can be used + * by other plugins to create types using MMOItems API. + */ + public Type(@NotNull String id, @NotNull ModifierSource modifierSource, @NotNull BiFunction interactionProvider) { this.id = UtilityMethods.enumName(id); this.modifierSource = modifierSource; + this.interactionProvider = interactionProvider; } /** @@ -128,6 +141,7 @@ public class Type implements CooldownObject, PreloadedObject { id = UtilityMethods.enumName(config.getName()); parent = manager.get(config.getString("parent", "").toUpperCase().replace("-", "_").replace(" ", "_")); modifierSource = config.contains("modifier-source") ? ModifierSource.valueOf(UtilityMethods.enumName(config.getString("modifier-source"))) : (parent != null ? parent.modifierSource : ModifierSource.OTHER); + interactionProvider = parent.interactionProvider; } public void load(@NotNull ConfigurationSection config) { @@ -152,6 +166,16 @@ public class Type implements CooldownObject, PreloadedObject { return !hideInGame; } + @NotNull + public UseItem toUseItem(@NotNull Player player, @NotNull NBTItem item) { + return toUseItem(PlayerData.get(player), item); + } + + @NotNull + public UseItem toUseItem(@NotNull PlayerData playerData, @NotNull NBTItem item) { + return interactionProvider.apply(playerData, item); + } + /** * Example OFF_CATALYST * diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/Tool.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/Tool.java index aa922dda..cfa94914 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/Tool.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/Tool.java @@ -10,6 +10,7 @@ import io.lumine.mythic.lib.version.OreDrops; import io.lumine.mythic.lib.version.VEnchantment; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.api.event.BouncingCrackBlockBreakEvent; +import net.Indyuce.mmoitems.api.interaction.weapon.Weapon; import net.Indyuce.mmoitems.api.player.PlayerData; import net.Indyuce.mmoitems.util.MMOUtils; import org.bukkit.Bukkit; @@ -24,14 +25,19 @@ import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.util.Vector; import org.jetbrains.annotations.NotNull; -public class Tool extends UseItem { +public class Tool extends Weapon { + public Tool(PlayerData playerData, NBTItem item) { + super(playerData, item); + } + + @Deprecated public Tool(Player player, NBTItem item) { super(player, item); } @Override - public boolean checkItemRequirements() { - return MythicLib.plugin.getFlags().isFlagAllowed(player, CustomFlag.MI_TOOLS) && playerData.getRPG().canUse(getNBTItem(), true); + public CustomFlag getUseFlag() { + return CustomFlag.MI_TOOLS; } private static final BlockFace[] NEIGHBORS = {BlockFace.NORTH, BlockFace.DOWN, BlockFace.EAST, BlockFace.UP, BlockFace.WEST, BlockFace.SOUTH}; diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/UseItem.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/UseItem.java index 1ae20a86..58768440 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/UseItem.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/UseItem.java @@ -6,9 +6,6 @@ import io.lumine.mythic.lib.comp.flags.CustomFlag; import net.Indyuce.mmoitems.ItemStats; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.api.Type; -import net.Indyuce.mmoitems.api.interaction.weapon.Weapon; -import net.Indyuce.mmoitems.api.interaction.weapon.untargeted.Lute; -import net.Indyuce.mmoitems.api.interaction.weapon.untargeted.Musket; import net.Indyuce.mmoitems.api.item.mmoitem.VolatileMMOItem; import net.Indyuce.mmoitems.api.player.PlayerData; import net.Indyuce.mmoitems.stat.data.CommandData; @@ -123,14 +120,8 @@ public class UseItem { return getItem(player, item, Type.get(type)); } + @Deprecated public static UseItem getItem(@NotNull Player player, @NotNull NBTItem item, @NotNull Type type) { - final PlayerData playerData = PlayerData.get(player); - if (type.corresponds(Type.CONSUMABLE)) return new Consumable(playerData, item); - if (type.corresponds(Type.SKIN)) return new ItemSkin(playerData, item); - if (type.corresponds(Type.GEM_STONE)) return new GemStone(playerData, item); - if (type.corresponds(Type.MUSKET)) return new Musket(playerData, item); - if (type.corresponds(Type.LUTE)) return new Lute(playerData, item); - - return type.isWeapon() ? new Weapon(playerData, item) : new UseItem(playerData, item); + return type.toUseItem(PlayerData.get(player), item); } } diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/weapon/Weapon.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/weapon/Weapon.java index 414a2e95..fd867f73 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/weapon/Weapon.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/weapon/Weapon.java @@ -33,7 +33,6 @@ import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; public class Weapon extends UseItem { - @Deprecated public Weapon(Player player, NBTItem item) { this(PlayerData.get(player), item); @@ -45,13 +44,19 @@ public class Weapon extends UseItem { @Override public boolean checkItemRequirements() { + + // Light checks first if (playerData.isEncumbered()) { Message.HANDS_TOO_CHARGED.format(ChatColor.RED).send(getPlayer()); return false; } // Check for class, level... then flags - return playerData.getRPG().canUse(getNBTItem(), true) && MythicLib.plugin.getFlags().isFlagAllowed(getPlayer(), CustomFlag.MI_WEAPONS); + return playerData.getRPG().canUse(getNBTItem(), true) && MythicLib.plugin.getFlags().isFlagAllowed(getPlayer(), getUseFlag()); + } + + public CustomFlag getUseFlag() { + return CustomFlag.MI_WEAPONS; } /** @@ -233,6 +238,7 @@ public class Weapon extends UseItem { return WeaponAttackResult.SUCCESS; } + @Deprecated protected Location getGround(Location loc) { for (int j = 0; j < 20; j++) { if (loc.getBlock().getType().isSolid()) return loc; diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/listener/ItemUse.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/listener/ItemUse.java index 562a1a31..ee0a64a6 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/listener/ItemUse.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/listener/ItemUse.java @@ -59,7 +59,7 @@ public class ItemUse implements Listener { * animation and are handled there {@link #handleVanillaEatenConsumables(PlayerItemConsumeEvent)} */ final Player player = event.getPlayer(); - final UseItem useItem = UseItem.getItem(player, item, itemType); + final UseItem useItem = itemType.toUseItem(player, item); if (useItem instanceof Consumable && ((Consumable) useItem).hasVanillaEating()) return; // (BUG FIX) Cancel the event to prevent things like shield blocking @@ -138,7 +138,7 @@ public class ItemUse implements Listener { NBTItem item = MythicLib.plugin.getVersion().getWrapper().getNBTItem(player.getInventory().getItemInMainHand()); if (!item.hasType()) return; - Tool tool = new Tool(player, item); + Tool tool = new Tool(PlayerData.get(player), item); if (!tool.checkItemRequirements()) { event.setCancelled(true); return; @@ -160,11 +160,11 @@ public class ItemUse implements Listener { if (!UtilityMethods.canTarget(player, target, InteractionType.OFFENSE_ACTION)) return; // Check for usability - final UseItem usableItem = UseItem.getItem(player, item, itemType); + final UseItem usableItem = itemType.toUseItem(player, item); if (!usableItem.checkItemRequirements()) return; // Apply type-specific entity interactions - final SkillHandler onEntityInteract = usableItem.getMMOItem().getType().onEntityInteract(); + final SkillHandler onEntityInteract = usableItem.getMMOItem().getType().onEntityInteract(); if (onEntityInteract != null) { SpecialWeaponAttackEvent called = new SpecialWeaponAttackEvent(usableItem.getPlayerData(), (Weapon) usableItem, target); Bukkit.getPluginManager().callEvent(called); @@ -183,7 +183,7 @@ public class ItemUse implements Listener { final Type type = Type.get(item); if (type == null) return; - final UseItem useItem = UseItem.getItem(player, item, type); + final UseItem useItem = type.toUseItem(player, item); if (!useItem.checkItemRequirements()) return; if (useItem instanceof ItemSkin) { @@ -269,7 +269,7 @@ public class ItemUse implements Listener { if (itemType == null) return; Player player = event.getPlayer(); - UseItem useItem = UseItem.getItem(player, item, itemType); + UseItem useItem = itemType.toUseItem(player, item); if (!useItem.checkItemRequirements()) { event.setCancelled(true); return;