From f9680bb8ba90bc5d5a5e97d9766963a33b414fa9 Mon Sep 17 00:00:00 2001 From: lbenav8095 Date: Tue, 20 Aug 2024 15:12:44 -0400 Subject: [PATCH] Adds "%psi%" to Formatter --- .../java/us/mytheria/bloblib/BlobLib.java | 2 + .../bloblib/itemapi/ValueItemAPI.java | 54 ++++++++++++++++++- .../mytheria/bloblib/utilities/Formatter.java | 14 ++++- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/main/java/us/mytheria/bloblib/BlobLib.java b/src/main/java/us/mytheria/bloblib/BlobLib.java index fb37ea4..47d619b 100644 --- a/src/main/java/us/mytheria/bloblib/BlobLib.java +++ b/src/main/java/us/mytheria/bloblib/BlobLib.java @@ -64,6 +64,7 @@ public class BlobLib extends JavaPlugin { private MinecraftVersion running; private SoulAPI soulAPI; private UniqueAPI uniqueAPI; + private FluidPressureAPI fluidPressureAPI; private static BlobLib instance; @@ -94,6 +95,7 @@ public void onEnable() { running = MinecraftVersion.getRunning(); soulAPI = SoulAPI.getInstance(this); uniqueAPI = UniqueAPI.getInstance(this); + fluidPressureAPI = FluidPressureAPI.getInstance(this); api = BlobLibAPI.getInstance(this); bloblibupdater = new BlobLibUpdater(this); serializationLib = SerializationLib.getInstance(this); diff --git a/src/main/java/us/mytheria/bloblib/itemapi/ValueItemAPI.java b/src/main/java/us/mytheria/bloblib/itemapi/ValueItemAPI.java index 550c1bf..ab23d9a 100644 --- a/src/main/java/us/mytheria/bloblib/itemapi/ValueItemAPI.java +++ b/src/main/java/us/mytheria/bloblib/itemapi/ValueItemAPI.java @@ -1,9 +1,9 @@ package us.mytheria.bloblib.itemapi; import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; -import org.bukkit.inventory.InventoryHolder; -import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.*; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataHolder; @@ -112,4 +112,54 @@ default List getAll(@NotNull Entity entity) { } return values; } + + /** + * Gets all the instances values from an Entity by checking its PersistentDataContainer and its equipment. + * + * @param entity The entity to check + * @return A list holding all the values + */ + @NotNull + default List getEquipment(@NotNull Entity entity) { + Objects.requireNonNull(entity, "'entity' cannot be null"); + List values = new ArrayList<>(); + if (isInstance(entity)) + values.add(get(entity)); + if (entity.getType() == EntityType.PLAYER) { + Player player = (Player) entity; + PlayerInventory playerInventory = player.getInventory(); + List equipment = new ArrayList<>(); + for (EquipmentSlot equipmentSlot : EquipmentSlot.values()) { + if (equipmentSlot.name().equalsIgnoreCase("BODY")) + continue; + equipment.add(playerInventory.getItem(equipmentSlot)); + } + for (ItemStack itemStack : equipment) { + if (itemStack == null) + continue; + if (isInstance(itemStack)) { + T t = get(itemStack.getItemMeta()); + values.add(t); + } + } + return values; + } + if (entity instanceof EntityEquipment entityEquipment) { + List equipment = new ArrayList<>(); + for (EquipmentSlot equipmentSlot : EquipmentSlot.values()) { + if (equipmentSlot.name().equalsIgnoreCase("BODY")) + continue; + equipment.add(entityEquipment.getItem(equipmentSlot)); + } + for (ItemStack itemStack : equipment) { + if (itemStack == null) + continue; + if (isInstance(itemStack)) { + T t = get(itemStack.getItemMeta()); + values.add(t); + } + } + } + return values; + } } diff --git a/src/main/java/us/mytheria/bloblib/utilities/Formatter.java b/src/main/java/us/mytheria/bloblib/utilities/Formatter.java index bf1e8fb..61e51d0 100644 --- a/src/main/java/us/mytheria/bloblib/utilities/Formatter.java +++ b/src/main/java/us/mytheria/bloblib/utilities/Formatter.java @@ -23,7 +23,8 @@ public String formatAll( "%wattsBalance%", watts((float) amount)).replace( "%bytesBalance%", bytes((float) amount)).replace( "%gramsBalance%", grams((float) amount)).replace( - "%litersBalance%", liters((float) amount)); + "%litersBalance%", liters((float) amount)).replace( + "%psi%", psi((float) amount)); } private String watts(float value) { @@ -69,4 +70,15 @@ private String liters(float value) { DecimalFormat decimalFormat = new DecimalFormat("#.##"); return String.format("%s%s", decimalFormat.format(value), arr[index]); } + + private String psi(float value) { + String[] arr = {"", "k", "M", "G", "T", "P", "E", "Z", "Y"}; + int index = 0; + while ((value / 1000) >= 1) { + value = value / 1000; + index++; + } + DecimalFormat decimalFormat = new DecimalFormat("#.##"); + return String.format("%s%s", decimalFormat.format(value), arr[index]) + "psi"; + } }