Merge branch 'master' into feature/player-list-name

This commit is contained in:
Gabriel Dumitru authored and GitHub committed 2021-10-04 18:11:52 +03:00
commit cd6b0e368a
4 files changed
+272 -92

No files matched your search

+2 -2
View File
@@ -3,7 +3,7 @@
<groupId>com.extendedclip.papi.expansion.player</groupId>
<artifactId>player-expansion</artifactId>
<version>1.8.0</version>
<version>2.0.3</version>
<name>PAPI-Expansion-Player</name>
<description>PlaceholderAPI expansion for player placeholders</description>
@@ -30,7 +30,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.16.3-R0.1-SNAPSHOT</version>
<version>1.17-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
@@ -33,29 +33,37 @@ import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffectType;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.extendedclip.papi.expansion.player.PlayerUtil.durability;
import static com.extendedclip.papi.expansion.player.PlayerUtil.format12;
import static com.extendedclip.papi.expansion.player.PlayerUtil.format24;
import static com.extendedclip.papi.expansion.player.PlayerUtil.getBiome;
import static com.extendedclip.papi.expansion.player.PlayerUtil.getCapitalizedBiome;
import static com.extendedclip.papi.expansion.player.PlayerUtil.getCardinalDirection;
import static com.extendedclip.papi.expansion.player.PlayerUtil.getEmptySlots;
import static com.extendedclip.papi.expansion.player.PlayerUtil.getDirection;
import static com.extendedclip.papi.expansion.player.PlayerUtil.getTotalExperience;
import static com.extendedclip.papi.expansion.player.PlayerUtil.getXZDirection;
import static com.extendedclip.papi.expansion.player.PlayerUtil.itemInHand;
public final class PlayerExpansion extends PlaceholderExpansion implements Configurable {
private final String VERSION = getClass().getPackage().getImplementationVersion();
private String low;
private String medium;
private String high;
private int mediumValue;
private int highValue;
private String north;
private String northEast;
private String east;
private String southEast;
private String south;
private String southWest;
private String west;
private String northWest;
@Override
public String getIdentifier() {
return "player";
@@ -68,7 +76,7 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
@Override
public String getVersion() {
return VERSION;
return "2.0.2";
}
@Override
@@ -77,6 +85,16 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
defaults.put("ping_color.high", "&c");
defaults.put("ping_color.medium", "&e");
defaults.put("ping_color.low", "&a");
defaults.put("ping_value.medium", 50);
defaults.put("ping_value.high", 100);
defaults.put("direction.north", "N");
defaults.put("direction.north_east", "NE");
defaults.put("direction.east", "E");
defaults.put("direction.south_east", "SE");
defaults.put("direction.south", "S");
defaults.put("direction.south_west", "SW");
defaults.put("direction.west", "W");
defaults.put("direction.north_west", "NW");
return defaults;
}
@@ -131,6 +149,7 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
case "bed_world":
return player.getBedSpawnLocation() != null ? player.getBedSpawnLocation().getWorld()
.getName() : "";
}
// online placeholders
@@ -140,6 +159,11 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
Player p = player.getPlayer();
// to get rid of IDE warnings
if (p == null) {
return "";
}
if (identifier.startsWith("has_permission_")) {
if (identifier.split("has_permission_").length > 1) {
String perm = identifier.split("has_permission_")[1];
@@ -171,7 +195,41 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
return "0";
}
if (identifier.startsWith("locale")) {
String localeStr = PlayerUtil.getLocale(p);
String localeStrISO = localeStr.replace("_", "-");
switch (identifier) {
case "locale":
return localeStr;
case "locale_country":
Locale locale = Locale.forLanguageTag(localeStrISO);
if (locale == null)
return "";
return locale.getCountry();
case "locale_display_country":
locale = Locale.forLanguageTag(localeStrISO);
if (locale == null)
return "";
return locale.getDisplayCountry();
case "locale_display_name":
locale = Locale.forLanguageTag(localeStrISO);
if (locale == null)
return "";
return locale.getDisplayName();
case "locale_short":
return localeStr.substring(0, localeStr.indexOf("_"));
}
}
switch (identifier) {
case "absorption": {
if (VersionHelper.HAS_ABSORPTION_METHODS) {
return Integer.toString((int) p.getAbsorptionAmount());
} else {
return "-1";
}
}
case "has_empty_slot":
return bool(p.getInventory().firstEmpty() > -1);
case "empty_slots":
@@ -183,12 +241,28 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
return p.getDisplayName();
case "list_name":
return p.getPlayerListName();
case "locale":
return p.getLocale();
case "gamemode":
return p.getGameMode().name();
case "direction":
return getCardinalDirection(p);
switch (getDirection(p)) {
case NORTH:
return north;
case NORTH_EAST:
return northEast;
case EAST:
return east;
case SOUTH_EAST:
return southEast;
case SOUTH:
return south;
case SOUTH_WEST:
return southWest;
case WEST:
return west;
case NORTH_WEST:
return northWest;
}
return "";
case "direction_xz":
return getXZDirection(p);
case "world":
@@ -205,10 +279,16 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
return "";
case "x":
return String.valueOf(p.getLocation().getBlockX());
case "x_long":
return String.valueOf(p.getLocation().getX());
case "y":
return String.valueOf(p.getLocation().getBlockY());
case "y_long":
return String.valueOf(p.getLocation().getY());
case "z":
return String.valueOf(p.getLocation().getBlockZ());
case "z_long":
return String.valueOf(p.getLocation().getZ());
case "yaw":
return String.valueOf(p.getLocation().getYaw());
case "pitch":
@@ -233,6 +313,8 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
return p.getCompassTarget() != null ? String.valueOf(p.getCompassTarget().getBlockZ()) : "";
case "compass_world":
return p.getCompassTarget() != null ? p.getCompassTarget().getWorld().getName() : "";
case "block_underneath":
return String.valueOf(p.getLocation().clone().subtract(0, 1, 0).getBlock().getType());
case "custom_name":
return p.getCustomName() != null ? p.getCustomName() : p.getName();
case "exp":
@@ -261,12 +343,16 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
return itemInHand(p).getType() != Material.AIR && itemInHand(p).getItemMeta().hasDisplayName() ? itemInHand(p).getItemMeta().getDisplayName() : "";
case "item_in_hand_data":
return itemInHand(p).getType() != Material.AIR ? String.valueOf(itemInHand(p).getDurability()) : "0";
case "item_in_hand_durability":
return String.valueOf(durability(itemInHand(p)));
case "item_in_offhand":
return String.valueOf(p.getInventory().getItemInOffHand().getType());
case "item_in_offhand_name":
return p.getInventory().getItemInOffHand().getType() != Material.AIR && p.getInventory().getItemInOffHand().getItemMeta().hasDisplayName() ? p.getInventory().getItemInOffHand().getItemMeta().getDisplayName() : "";
case "item_in_offhand_data":
return p.getInventory().getItemInOffHand().getType() != Material.AIR ? String.valueOf(p.getInventory().getItemInOffHand().getDurability()) : "0";
case "item_in_offhand_durability":
return String.valueOf(durability(p.getInventory().getItemInOffHand()));
case "last_damage":
return String.valueOf(p.getLastDamage());
case "max_health":
@@ -283,18 +369,26 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
return Optional.ofNullable(p.getInventory().getHelmet()).map(a -> a.getItemMeta().getDisplayName()).orElse("");
case "armor_helmet_data":
return p.getInventory().getHelmet() != null ? String.valueOf(p.getInventory().getHelmet().getDurability()) : "0";
case "armor_helmet_durability":
return String.valueOf(durability(p.getInventory().getHelmet()));
case "armor_chestplate_name":
return Optional.ofNullable(p.getInventory().getChestplate()).map(a -> a.getItemMeta().getDisplayName()).orElse("");
case "armor_chestplate_data":
return p.getInventory().getChestplate() != null ? String.valueOf(p.getInventory().getChestplate().getDurability()) : "0";
case "armor_chestplate_durability":
return String.valueOf(durability(p.getInventory().getChestplate()));
case "armor_leggings_name":
return Optional.ofNullable(p.getInventory().getLeggings()).map(a -> a.getItemMeta().getDisplayName()).orElse("");
case "armor_leggings_data":
return p.getInventory().getLeggings() != null ? String.valueOf(p.getInventory().getLeggings().getDurability()) : "0";
case "armor_leggings_durability":
return String.valueOf(durability(p.getInventory().getLeggings()));
case "armor_boots_name":
return Optional.ofNullable(p.getInventory().getBoots()).map(a -> a.getItemMeta().getDisplayName()).orElse("");
case "armor_boots_data":
return p.getInventory().getBoots() != null ? String.valueOf(p.getInventory().getBoots().getDurability()) : "0";
case "armor_boots_durability":
return String.valueOf(durability(p.getInventory().getBoots()));
case "ping":
return retrievePing(p, false);
case "colored_ping":
@@ -314,9 +408,9 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
case "ticks_lived":
return String.valueOf(p.getTicksLived());
case "seconds_lived":
return String.valueOf(p.getTicksLived() * 20);
return String.valueOf(p.getTicksLived() / 20);
case "minutes_lived":
return String.valueOf((p.getTicksLived() * 20) / 60);
return String.valueOf((p.getTicksLived() / 20) / 60);
case "walk_speed":
return String.valueOf(p.getWalkSpeed());
case "weather_duration":
@@ -327,6 +421,22 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
return format12(p.getWorld().getTime());
case "world_time_24":
return format24(p.getWorld().getTime());
case "is_flying":
return bool(p.isFlying());
case "is_sleeping":
return bool(p.isSleeping());
case "is_conversing":
return bool(p.isConversing());
case "is_dead":
return bool(p.isDead());
case "is_sneaking":
return bool(p.isSneaking());
case "is_sprinting":
return bool(p.isSprinting());
case "is_leashed":
return bool(p.isLeashed());
case "is_inside_vehicle":
return bool(p.isInsideVehicle());
}
// return null for unknown placeholders
return null;
@@ -337,6 +447,17 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
low = this.getString("ping_color.low", "&a");
medium = this.getString("ping_color.medium", "&e");
high = this.getString("ping_color.high", "&c");
mediumValue = this.getInt("ping_value.medium", 50);
highValue = this.getInt("ping_value.high", 100);
north = this.getString("direction.north", "N");
northEast = this.getString("direction.north_east", "NE");
east = this.getString("direction.east", "E");
southEast = this.getString("direction.south_east", "SE");
south = this.getString("direction.south", "S");
southWest = this.getString("direction.south_west", "SW");
west = this.getString("direction.west", "W");
northWest = this.getString("direction.north_west", "NW");
return super.register();
}
@@ -352,7 +473,7 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
return String.valueOf(ping);
}
return ChatColor.translateAlternateColorCodes('&', ping > 100 ? high : ping > 50 ? medium : low) + ping;
return ChatColor.translateAlternateColorCodes('&', ping > highValue ? high : ping > mediumValue ? medium : low) + ping;
}
}
@@ -2,6 +2,7 @@ package com.extendedclip.papi.expansion.player;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
@@ -42,58 +43,88 @@ public final class PlayerUtil {
public static final double ticksPerMinute = 1000d / 60d;
private static final SimpleDateFormat twentyFour = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
private static final SimpleDateFormat twelve = new SimpleDateFormat("h:mm aa", Locale.ENGLISH);
private static final BlockFace[] radial = { BlockFace.NORTH, BlockFace.NORTH_EAST, BlockFace.EAST, BlockFace.SOUTH_EAST, BlockFace.SOUTH, BlockFace.SOUTH_WEST, BlockFace.WEST, BlockFace.NORTH_WEST };
private PlayerUtil()
{}
private PlayerUtil() { }
private static final Function<Player, Integer> PLAYER_GET_PING = new Function<Player, Integer>() {
private static final Function<Player, Integer> PLAYER_GET_PING = new Function<Player, Integer>() {
private Field ping;
private Method getHandle;
private Field ping;
private Method getHandle;
@Override
public Integer apply(final Player player)
{
if (ping == null)
{
try
{
cacheReflection(player);
}
catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException | NoSuchFieldException ex)
{
ex.printStackTrace();
}
}
@Override
public Integer apply(final Player player) {
if (ping == null) {
try {
cacheReflection(player);
} catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException | NoSuchFieldException ex) {
ex.printStackTrace();
}
}
try
{
return ping.getInt(getHandle.invoke(player));
}
catch (final IllegalAccessException | InvocationTargetException ex)
{
ex.printStackTrace();
}
return -1;
}
try {
return ping.getInt(getHandle.invoke(player));
} catch (final IllegalAccessException | InvocationTargetException ex) {
ex.printStackTrace();
}
return -1;
}
private void cacheReflection(final Player player) throws NoSuchFieldException, NoSuchMethodException, InvocationTargetException, IllegalAccessException
{
getHandle = player.getClass().getDeclaredMethod("getHandle");
getHandle.setAccessible(true);
private void cacheReflection(final Player player) throws NoSuchFieldException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
getHandle = player.getClass().getDeclaredMethod("getHandle");
getHandle.setAccessible(true);
final Object entityPlayer = getHandle.invoke(player);
final Object entityPlayer = getHandle.invoke(player);
ping = entityPlayer.getClass().getDeclaredField("ping");
ping.setAccessible(true);
}
};
ping = entityPlayer.getClass().getDeclaredField(VersionHelper.IS_1_17_OR_NEWER ? "e" : "ping");
ping.setAccessible(true);
}
};
private static final Function<Player, String> PLAYER_GET_LOCALE = new Function<Player, String>() {
private Field locale;
private Method getHandle;
@Override
public String apply(final Player player) {
if (locale == null) {
try {
cacheReflection(player);
} catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException | NoSuchFieldException ex) {
ex.printStackTrace();
}
}
try {
final Object entityPlayer = getHandle.invoke(player);
return (String) locale.get(entityPlayer);
} catch (final IllegalAccessException | InvocationTargetException ex) {
ex.printStackTrace();
}
return "en_US";
}
public static int getPing(final Player player) {
return PLAYER_GET_PING.apply(player);
}
private void cacheReflection(final Player player) throws NoSuchFieldException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
getHandle = player.getClass().getDeclaredMethod("getHandle");
getHandle.setAccessible(true);
final Object entityPlayer = getHandle.invoke(player);
locale = entityPlayer.getClass().getField("locale");
}
};
public static int getPing(final Player player) {
return PLAYER_GET_PING.apply(player);
}
public static String getLocale(final Player player) {
return PLAYER_GET_LOCALE.apply(player);
}
public static String format12(long ticks) {
try {
@@ -118,39 +149,8 @@ public final class PlayerUtil {
return (hours < 10 ? "0" + hours : hours) + ":" + (mins < 10 ? "0" + mins : mins);
}
public static String getCardinalDirection(Player player) {
double rotation = player.getLocation().getYaw() - 180.0F;
if (rotation < 0.0D) {
rotation += 360.0D;
}
if ((0.0D <= rotation) && (rotation < 22.5D)) {
return "N";
}
if ((22.5D <= rotation) && (rotation < 67.5D)) {
return "NE";
}
if ((67.5D <= rotation) && (rotation < 112.5D)) {
return "E";
}
if ((112.5D <= rotation) && (rotation < 157.5D)) {
return "SE";
}
if ((157.5D <= rotation) && (rotation < 202.5D)) {
return "S";
}
if ((202.5D <= rotation) && (rotation < 247.5D) || (rotation <= -119.33) && (rotation > -179)) {
return "SW";
}
if ((247.5D <= rotation) && (rotation < 292.5D) || (rotation <= -59.66) && (rotation > -119.33)) {
return "W";
}
if ((292.5D <= rotation) && (rotation < 337.5D) || (rotation <= -0.0) && (rotation > -59.66)) {
return "NW";
}
if ((337.5D <= rotation) && (rotation < 360.0D)) {
return "N";
}
return "";
public static BlockFace getDirection(Player player) {
return radial[Math.round(player.getLocation().getYaw() / 45f) & 0x7].getOppositeFace();
}
public static String getXZDirection(Player player) {
@@ -180,6 +180,10 @@ public final class PlayerUtil {
}
}
public static int durability(ItemStack item) {
return item != null ? item.getType().getMaxDurability() - item.getDurability() : 0;
}
public static int getEmptySlots(Player p) {
int slots = 0;
PlayerInventory inv = p.getInventory();
@@ -0,0 +1,55 @@
package com.extendedclip.papi.expansion.player;
import com.google.common.primitives.Ints;
import org.bukkit.Bukkit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Matt (<a href="https://github.com/ipsk">@ipsk</a>)
*/
public final class VersionHelper {
private static final int VERSION = getCurrentVersion();
/**
* <a href="https://docs.docdex.helpch.at/1.15/org/bukkit/entity/Damageable.html#getAbsorptionAmount--">
* Damageable#getAbsorptionAmount
* </a>
*/
public static final boolean HAS_ABSORPTION_METHODS = VERSION >= 1_15_0;
public static final boolean IS_1_17_OR_NEWER = VERSION >= 1_17_0;
private VersionHelper() { }
/**
* Gets the current server version
*
* @return A protocol like number representing the version, for example 1.16.5 - 1165
*/
private static int getCurrentVersion() {
// No need to cache since will only run once
final Matcher matcher = Pattern.compile("(?<version>\\d+\\.\\d+)(?<patch>\\.\\d+)?").matcher(Bukkit.getBukkitVersion());
final StringBuilder stringBuilder = new StringBuilder();
if (matcher.find()) {
final String patch = matcher.group("patch");
stringBuilder
.append(matcher.group("version").replace(".", ""))
.append((patch == null) ? "0" : patch.replace(".", ""));
}
final Integer version = Ints.tryParse(stringBuilder.toString());
// Should never fail
if (version == null) {
throw new IllegalArgumentException("Could not retrieve server version!");
}
return version;
}
}