Now uses MMOLib method

This commit is contained in:
Indyuce 2020-05-09 15:42:28 +02:00
parent 1c70cd10ec
commit de6eb0f35a
8 changed files with 69 additions and 53 deletions
src/main/java/net/Indyuce/mmoitems

View File

@ -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);

View File

@ -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));
/*

View File

@ -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;

View File

@ -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;

View File

@ -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<AbilityData> getItemAbilities(CastingMode castMode) {
return itemAbilities.stream().filter(abilityData -> abilityData.getCastingMode() == castMode).collect(Collectors.toSet());
public Set<AbilityData> 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;
/*

View File

@ -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;

View File

@ -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];

View File

@ -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();