PAPI update

This commit is contained in:
Jules 2020-07-30 10:19:43 +02:00
parent 2578a15f32
commit b858a0d334
3 changed files with 42 additions and 18 deletions

Binary file not shown.

View File

@ -2,19 +2,21 @@ package net.Indyuce.mmoitems.comp.parse.placeholders;
import java.text.DecimalFormat;
import net.mmogroup.mmolib.api.player.MMOPlayerData;
import net.mmogroup.mmolib.listener.DamageReduction;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.OfflinePlayer;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.player.PlayerData;
import net.mmogroup.mmolib.api.util.AltChar;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.mmogroup.mmolib.MMOLib;
import net.mmogroup.mmolib.api.item.NBTItem;
import net.mmogroup.mmolib.api.player.MMOPlayerData;
import net.mmogroup.mmolib.api.util.AltChar;
import net.mmogroup.mmolib.listener.DamageReduction;
public class MMOItemsPlaceholders extends PlaceholderExpansion {
private final DecimalFormat oneDigit = new DecimalFormat("0.#"), twoDigits = new DecimalFormat("0.##");
@ -31,48 +33,68 @@ public class MMOItemsPlaceholders extends PlaceholderExpansion {
@Override
public String getVersion() {
return "1.0";
return MMOItems.plugin.getDescription().getVersion();
}
public String onPlaceholderRequest(Player player, String identifier) {
/**
* Because this is an internal class, you must override this method to let
* PlaceholderAPI know to not unregister your expansion class when
* PlaceholderAPI is reloaded
*
* @return true to persist through reloads
*/
@Override
public boolean persist() {
return true;
}
@Override
public String onRequest(@Nullable OfflinePlayer player, @NotNull String identifier) {
// registering before identifier.startsWith("stat_") to prevent issues
// i don't register it in the starts with condition because it will mess with substring
// i don't register it in the starts with condition because it will mess with
// substring
if (identifier.equals("stat_defense_percent"))
return twoDigits.format(new DamageReduction.DefenseCalculator(MMOPlayerData.get(player)).getReductionPercent()) + "%";
return twoDigits.format(
new DamageReduction.DefenseCalculator(MMOPlayerData.get(player)).getReductionPercent()) + "%";
if (identifier.startsWith("stat_")) {
ItemStat stat = MMOItems.plugin.getStats().get(identifier.substring(5).toUpperCase());
if (stat != null)
return twoDigits.format(PlayerData.get(player).getStats().getStat(stat));
}
if (identifier.startsWith("ability_cd_")) {
PlayerData data = PlayerData.get(player);
return data.hasCooldownInfo(identifier.substring(11)) ? oneDigit.format(data.getCooldownInfo(identifier.substring(11)).getRemaining())
return data.hasCooldownInfo(identifier.substring(11))
? oneDigit.format(data.getCooldownInfo(identifier.substring(11)).getRemaining())
: "0";
}
if (!player.isOnline())
return null;
if (identifier.equals("durability"))
return "" + (int) MMOLib.plugin.getNMS().getNBTItem(player.getInventory().getItemInMainHand()).getDouble("MMOITEMS_DURABILITY");
return "" + (int) MMOLib.plugin.getNMS().getNBTItem(player.getPlayer().getInventory().getItemInMainHand())
.getDouble("MMOITEMS_DURABILITY");
if (identifier.equals("durability_max"))
return "" + (int) MMOLib.plugin.getNMS().getNBTItem(player.getInventory().getItemInMainHand()).getDouble("MMOITEMS_MAX_DURABILITY");
return "" + (int) MMOLib.plugin.getNMS().getNBTItem(player.getPlayer().getInventory().getItemInMainHand())
.getDouble("MMOITEMS_MAX_DURABILITY");
if (identifier.equals("durability_ratio")) {
NBTItem item = MMOLib.plugin.getNMS().getNBTItem(player.getInventory().getItemInMainHand());
NBTItem item = MMOLib.plugin.getNMS().getNBTItem(player.getPlayer().getInventory().getItemInMainHand());
double durability = item.getDouble("MMOITEMS_DURABILITY");
double maxDurability = item.getDouble("MMOITEMS_MAX_DURABILITY");
return oneDigit.format(durability / maxDurability * 100);
}
if (identifier.equals("durability_bar_square"))
return getCurrentDurabilityBar(player.getInventory().getItemInMainHand(), AltChar.square, 10);
return getCurrentDurabilityBar(player.getPlayer().getInventory().getItemInMainHand(), AltChar.square, 10);
if (identifier.equals("durability_bar_diamond"))
return getCurrentDurabilityBar(player.getInventory().getItemInMainHand(), AltChar.diamond, 15);
return getCurrentDurabilityBar(player.getPlayer().getInventory().getItemInMainHand(), AltChar.diamond, 15);
if (identifier.equals("durability_bar_thin"))
return getCurrentDurabilityBar(player.getInventory().getItemInMainHand(), "|", 20);
return getCurrentDurabilityBar(player.getPlayer().getInventory().getItemInMainHand(), "|", 20);
return null;
}

View File

@ -1,13 +1,15 @@
package net.Indyuce.mmoitems.comp.parse.placeholders;
import net.asangarin.hexcolors.ColorParse;
import org.bukkit.OfflinePlayer;
import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.asangarin.hexcolors.ColorParse;
public class PlaceholderAPIParser implements PlaceholderParser {
public PlaceholderAPIParser() {
new MMOItemsPlaceholders().register();
PlaceholderExpansion expansion = new MMOItemsPlaceholders();
expansion.getPlaceholderAPI().getLocalExpansionManager().register(expansion);
}
@Override