Skip to content

Commit

Permalink
Adds "%psi%" to Formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
anjoismysign committed Aug 20, 2024
1 parent 2468fdd commit f9680bb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/main/java/us/mytheria/bloblib/BlobLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
54 changes: 52 additions & 2 deletions src/main/java/us/mytheria/bloblib/itemapi/ValueItemAPI.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -112,4 +112,54 @@ default List<T> 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<T> getEquipment(@NotNull Entity entity) {
Objects.requireNonNull(entity, "'entity' cannot be null");
List<T> values = new ArrayList<>();
if (isInstance(entity))
values.add(get(entity));
if (entity.getType() == EntityType.PLAYER) {
Player player = (Player) entity;
PlayerInventory playerInventory = player.getInventory();
List<ItemStack> 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<ItemStack> 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;
}
}
14 changes: 13 additions & 1 deletion src/main/java/us/mytheria/bloblib/utilities/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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";
}
}

0 comments on commit f9680bb

Please sign in to comment.