From de6eb0f35a2934b45355c5d60a576e40b2388f2c Mon Sep 17 00:00:00 2001 From: Indyuce Date: Sat, 9 May 2020 15:42:28 +0200 Subject: [PATCH] Now uses MMOLib method --- .../java/net/Indyuce/mmoitems/MMOUtils.java | 57 +++++++++---------- .../api/crafting/recipe/CraftingRecipe.java | 4 +- .../mmoitems/api/interaction/Consumable.java | 3 +- .../interaction/weapon/untargeted/Staff.java | 5 ++ .../mmoitems/api/player/PlayerData.java | 41 ++++++++----- .../mmoitems/api/player/RPGPlayer.java | 4 +- .../mmoitems/command/MMOItemsCommand.java | 3 +- .../mmoitems/gui/CraftingStationView.java | 5 +- 8 files changed, 69 insertions(+), 53 deletions(-) diff --git a/src/main/java/net/Indyuce/mmoitems/MMOUtils.java b/src/main/java/net/Indyuce/mmoitems/MMOUtils.java index b1a0ac89..43cde634 100644 --- a/src/main/java/net/Indyuce/mmoitems/MMOUtils.java +++ b/src/main/java/net/Indyuce/mmoitems/MMOUtils.java @@ -57,17 +57,12 @@ public class MMOUtils { return bar.substring(0, (int) (ratio * n)) + ChatColor.WHITE + bar.substring((int) (ratio * n)); } - public static void giveOrDrop(Player player, ItemStack item) { - for (ItemStack drop : player.getInventory().addItem(item).values()) - player.getWorld().dropItem(player.getLocation(), drop); - } - -// public static PotionEffectType valueOfPotionEffectType(String effect) { -// for (PotionEffectType checked : PotionEffectType.values()) -// if (checked.getName().equals(effect.toUpperCase().replace("-", "_"))) -// return checked; -// return null; -// } + // public static PotionEffectType valueOfPotionEffectType(String effect) { + // for (PotionEffectType checked : PotionEffectType.values()) + // if (checked.getName().equals(effect.toUpperCase().replace("-", "_"))) + // return checked; + // return null; + // } public static LivingEntity getDamager(EntityDamageByEntityEvent event) { @@ -115,20 +110,20 @@ public class MMOUtils { } public static String getDisplayName(ItemStack item) { - if (!item.hasItemMeta()) - return MMOUtils.caseOnWords(item.getType().name().toLowerCase().replace("_", " ")); - return item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName() : MMOUtils.caseOnWords(item.getType().name().toLowerCase().replace("_", " ")); + return item.hasItemMeta() && item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName() + : MMOUtils.caseOnWords(item.getType().name().toLowerCase().replace("_", " ")); } public static boolean twoHandedCase(Player player) { - int normal = 0; - int twoHanded = 0; - for (ItemStack item : new ItemStack[] { player.getInventory().getItemInMainHand(), player.getInventory().getItemInOffHand() }) { - if (item.getType() != Material.AIR) + int normal = 0, twoHanded = 0; + + for (ItemStack item : new ItemStack[] { player.getInventory().getItemInMainHand(), player.getInventory().getItemInOffHand() }) + if (item.getType() != Material.AIR) { normal++; - if (MMOLib.plugin.getNMS().getNBTItem(item).getBoolean("MMOITEMS_TWO_HANDED")) - twoHanded++; - } + if (MMOLib.plugin.getNMS().getNBTItem(item).getBoolean("MMOITEMS_TWO_HANDED")) + twoHanded++; + } + return twoHanded > 0 && normal > 1; } @@ -140,16 +135,15 @@ public class MMOUtils { if (isLastSpace && ch >= 'a' && ch <= 'z') { builder.setCharAt(i, (char) (ch + ('A' - 'a'))); isLastSpace = false; - } else if (ch != ' ') - isLastSpace = false; - else - isLastSpace = true; + } else + isLastSpace = ch == ' '; } return builder.toString(); } public static boolean isMetaItem(ItemStack item, boolean lore) { - return item != null && item.getType() != Material.AIR && item.getItemMeta() != null && item.getItemMeta().getDisplayName() != null && (!lore || item.getItemMeta().getLore() != null); + return item != null && item.getType() != Material.AIR && item.getItemMeta() != null && item.getItemMeta().getDisplayName() != null + && (!lore || item.getItemMeta().getLore() != null); } public static void saturate(Player player, double saturation) { @@ -183,21 +177,22 @@ public class MMOUtils { * if the entity is dead since a dying entity (dying effect takes some * time) can still be targeted but we dont want that */ - if (target.equals(player) || !(target instanceof LivingEntity) || target instanceof ArmorStand || target.isDead()) + if (target.equals(player) || target.isDead() || !(target instanceof LivingEntity) || target instanceof ArmorStand) return false; /* - * can spam your console - an error message is sent each time an NPC - * gets damaged since it is considered as a player. + * extra plugin compatibility, everything is handled via MMOLib because + * the same system is used by MMOCore */ - if (target.hasMetadata("NPC")) + if (MMOLib.plugin.getEntities().findCustom(target)) return false; /* * the ability player damage option is cached for quicker access in the * config manager instance since it is used in runnables */ - if (target instanceof Player && (!MMOItems.plugin.getLanguage().abilityPlayerDamage || !MMOItems.plugin.getFlags().isPvpAllowed(target.getLocation()))) + if (target instanceof Player + && (!MMOItems.plugin.getLanguage().abilityPlayerDamage || !MMOItems.plugin.getFlags().isPvpAllowed(target.getLocation()))) return false; return loc == null ? true : MMOLib.plugin.getNMS().isInBoundingBox(target, loc); diff --git a/src/main/java/net/Indyuce/mmoitems/api/crafting/recipe/CraftingRecipe.java b/src/main/java/net/Indyuce/mmoitems/api/crafting/recipe/CraftingRecipe.java index bd28526d..24c3b2eb 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/crafting/recipe/CraftingRecipe.java +++ b/src/main/java/net/Indyuce/mmoitems/api/crafting/recipe/CraftingRecipe.java @@ -5,7 +5,6 @@ import org.bukkit.Sound; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; -import net.Indyuce.mmoitems.MMOUtils; import net.Indyuce.mmoitems.api.crafting.ConfigMMOItem; import net.Indyuce.mmoitems.api.crafting.CraftingStation; import net.Indyuce.mmoitems.api.crafting.CraftingStatus.CraftingQueue; @@ -13,6 +12,7 @@ import net.Indyuce.mmoitems.api.crafting.IngredientInventory; import net.Indyuce.mmoitems.api.item.plugin.ConfigItem; import net.Indyuce.mmoitems.api.player.PlayerData; import net.Indyuce.mmoitems.api.util.message.Message; +import net.mmogroup.mmolib.api.util.SmartGive; public class CraftingRecipe extends Recipe { private final ConfigMMOItem output; @@ -53,7 +53,7 @@ public class CraftingRecipe extends Recipe { * directly add the ingredients to the player inventory */ if (isInstant()) { - MMOUtils.giveOrDrop(data.getPlayer(), getOutput().generate()); + new SmartGive(data.getPlayer()).give(getOutput().generate()); recipe.getRecipe().getTriggers().forEach(trigger -> trigger.whenCrafting(data)); /* diff --git a/src/main/java/net/Indyuce/mmoitems/api/interaction/Consumable.java b/src/main/java/net/Indyuce/mmoitems/api/interaction/Consumable.java index c3e60248..0f573b7a 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/interaction/Consumable.java +++ b/src/main/java/net/Indyuce/mmoitems/api/interaction/Consumable.java @@ -28,6 +28,7 @@ import net.Indyuce.mmoitems.stat.type.ItemStat; import net.mmogroup.mmolib.MMOLib; import net.mmogroup.mmolib.api.item.ItemTag; import net.mmogroup.mmolib.api.item.NBTItem; +import net.mmogroup.mmolib.api.util.SmartGive; public class Consumable extends UseItem { public Consumable(Player player, NBTItem item, Type type) { @@ -318,7 +319,7 @@ public class Consumable extends UseItem { if (item.getAmount() > 1) { item.setAmount(item.getAmount() - 1); - MMOUtils.giveOrDrop(player, item); + new SmartGive(player).give(item); } return false; diff --git a/src/main/java/net/Indyuce/mmoitems/api/interaction/weapon/untargeted/Staff.java b/src/main/java/net/Indyuce/mmoitems/api/interaction/weapon/untargeted/Staff.java index 36d2506d..10657300 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/interaction/weapon/untargeted/Staff.java +++ b/src/main/java/net/Indyuce/mmoitems/api/interaction/weapon/untargeted/Staff.java @@ -1,5 +1,6 @@ package net.Indyuce.mmoitems.api.interaction.weapon.untargeted; +import org.bukkit.Bukkit; import org.bukkit.EntityEffect; import org.bukkit.Location; import org.bukkit.Particle; @@ -33,6 +34,10 @@ public class Staff extends UntargetedWeapon { public void untargetedAttack(EquipmentSlot slot) { CachedStats stats = getPlayerData().getStats().newTemporary(); + + + Bukkit.broadcastMessage(stats.getStat(ItemStat.ATTACK_SPEED)+" " +( getValue(stats.getStat(ItemStat.ATTACK_SPEED), MMOItems.plugin.getConfig().getDouble("default.attack-speed")))); + if (!hasEnoughResources(1 / getValue(stats.getStat(ItemStat.ATTACK_SPEED), MMOItems.plugin.getConfig().getDouble("default.attack-speed")), CooldownType.ATTACK, false)) return; diff --git a/src/main/java/net/Indyuce/mmoitems/api/player/PlayerData.java b/src/main/java/net/Indyuce/mmoitems/api/player/PlayerData.java index 4ba8d556..a4cd432b 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/player/PlayerData.java +++ b/src/main/java/net/Indyuce/mmoitems/api/player/PlayerData.java @@ -8,7 +8,6 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; -import java.util.stream.Collectors; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -352,27 +351,40 @@ public class PlayerData { return stats; } - public Set getItemAbilities(CastingMode castMode) { - return itemAbilities.stream().filter(abilityData -> abilityData.getCastingMode() == castMode).collect(Collectors.toSet()); + public Set getItemAbilities() { + return itemAbilities; + } + + private boolean hasAbility(CastingMode castMode) { + for (AbilityData ability : itemAbilities) + if (ability.getCastingMode() == castMode) + return true; + return false; } public ItemAttackResult castAbilities(LivingEntity target, ItemAttackResult result, CastingMode castMode) { + + /* + * performance improvement, do not cache the player stats into a + * CachedStats if the player has no ability on that cast mode + */ + if (!hasAbility(castMode)) + return result; + return castAbilities(getStats().newTemporary(), target, result, castMode); } public ItemAttackResult castAbilities(CachedStats stats, LivingEntity target, ItemAttackResult result, CastingMode castMode) { - if (target == null) { - if (!MMOItems.plugin.getFlags().isFlagAllowed(player, CustomFlag.MI_ABILITIES)) - return result.setSuccessful(false); - } else if (!MMOItems.plugin.getFlags().isFlagAllowed(target.getLocation(), CustomFlag.MI_ABILITIES)) + if ((target == null && !MMOItems.plugin.getFlags().isFlagAllowed(player, CustomFlag.MI_ABILITIES)) + || !MMOItems.plugin.getFlags().isFlagAllowed(target.getLocation(), CustomFlag.MI_ABILITIES)) return result.setSuccessful(false); if (target != null && !MMOUtils.canDamage(player, target)) return result.setSuccessful(false); - boolean message = castMode.displaysMessage(); - for (AbilityData ability : getItemAbilities(castMode)) - cast(stats, target, result, ability, message); + for (AbilityData ability : itemAbilities) + if (ability.getCastingMode() == castMode) + cast(stats, target, result, ability); return result; } @@ -382,21 +394,22 @@ public class PlayerData { * also requires the initial damage value and a target to be successfully * cast */ + @Deprecated public void cast(Ability ability) { - cast(getStats().newTemporary(), null, new ItemAttackResult(true, DamageType.SKILL), new AbilityData(ability, null), true); + cast(getStats().newTemporary(), null, new ItemAttackResult(true, DamageType.SKILL), new AbilityData(ability, CastingMode.RIGHT_CLICK)); } public void cast(AbilityData data) { - cast(getStats().newTemporary(), null, new ItemAttackResult(true, DamageType.SKILL), data, true); + cast(getStats().newTemporary(), null, new ItemAttackResult(true, DamageType.SKILL), data); } - public void cast(CachedStats stats, LivingEntity target, ItemAttackResult attack, AbilityData ability, boolean message) { + public void cast(CachedStats stats, LivingEntity target, ItemAttackResult attack, AbilityData ability) { AbilityUseEvent event = new AbilityUseEvent(this, ability, target); Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) return; - if (!rpgPlayer.canCast(ability, message)) + if (!rpgPlayer.canCast(ability)) return; /* diff --git a/src/main/java/net/Indyuce/mmoitems/api/player/RPGPlayer.java b/src/main/java/net/Indyuce/mmoitems/api/player/RPGPlayer.java index 99f4e32b..27da6d84 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/player/RPGPlayer.java +++ b/src/main/java/net/Indyuce/mmoitems/api/player/RPGPlayer.java @@ -70,12 +70,12 @@ public abstract class RPGPlayer { return true; } - public boolean canCast(AbilityData data, boolean message) { + public boolean canCast(AbilityData data) { if (playerData.hasCooldownInfo(data.getAbility())) { CooldownInformation info = playerData.getCooldownInfo(data.getAbility()); if (!info.hasCooledDown()) { - if (message) { + if (data.getCastingMode().displaysMessage()) { String progressBar = ChatColor.YELLOW + ""; double progress = (info.getInitialCooldown() - info.getRemaining()) / info.getInitialCooldown() * 10; diff --git a/src/main/java/net/Indyuce/mmoitems/command/MMOItemsCommand.java b/src/main/java/net/Indyuce/mmoitems/command/MMOItemsCommand.java index f2c37722..3b3539bd 100644 --- a/src/main/java/net/Indyuce/mmoitems/command/MMOItemsCommand.java +++ b/src/main/java/net/Indyuce/mmoitems/command/MMOItemsCommand.java @@ -31,6 +31,7 @@ import net.Indyuce.mmoitems.api.PluginUpdate; import net.Indyuce.mmoitems.api.Type; import net.Indyuce.mmoitems.api.UpdaterData; import net.Indyuce.mmoitems.api.ability.Ability; +import net.Indyuce.mmoitems.api.ability.Ability.CastingMode; import net.Indyuce.mmoitems.api.crafting.CraftingStation; import net.Indyuce.mmoitems.api.droptable.item.MMOItemDropItem; import net.Indyuce.mmoitems.api.item.MMOItem; @@ -986,7 +987,7 @@ public class MMOItemsCommand implements CommandExecutor { } // modifiers - AbilityData ability = new AbilityData(MMOItems.plugin.getAbilities().getAbility(key), null); + AbilityData ability = new AbilityData(MMOItems.plugin.getAbilities().getAbility(key), CastingMode.RIGHT_CLICK); for (int j = 3; j < args.length - 1; j += 2) { String name = args[j]; String value = args[j + 1]; diff --git a/src/main/java/net/Indyuce/mmoitems/gui/CraftingStationView.java b/src/main/java/net/Indyuce/mmoitems/gui/CraftingStationView.java index 66290b38..56cd1192 100644 --- a/src/main/java/net/Indyuce/mmoitems/gui/CraftingStationView.java +++ b/src/main/java/net/Indyuce/mmoitems/gui/CraftingStationView.java @@ -28,6 +28,7 @@ import net.Indyuce.mmoitems.api.util.message.Message; import net.Indyuce.mmoitems.listener.CustomSoundListener; import net.mmogroup.mmolib.MMOLib; import net.mmogroup.mmolib.api.item.NBTItem; +import net.mmogroup.mmolib.api.util.SmartGive; public class CraftingStationView extends PluginInventory { private final CraftingStation station; @@ -186,10 +187,10 @@ public class CraftingStationView extends PluginInventory { recipe.getTriggers().forEach(trigger -> trigger.whenCrafting(data)); ItemStack craftedItem = recipe.getOutput().generate(); CustomSoundListener.stationCrafting(craftedItem, data.getPlayer()); - MMOUtils.giveOrDrop(data.getPlayer(), craftedItem); + new SmartGive(data.getPlayer()).give(craftedItem); } else for (Ingredient ingredient : craft.getRecipe().getIngredients()) - MMOUtils.giveOrDrop(player, ingredient.generateItemStack()); + new SmartGive(data.getPlayer()).give(ingredient.generateItemStack()); updateData(); open();