Skip to content

Commit

Permalink
Adds 'liters' to Formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
anjoismysign committed Aug 9, 2024
1 parent 3001b79 commit 4a3dc0b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
<dependency>
<groupId>me.anjoismysign</groupId>
<artifactId>blis</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>de.oliver</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,8 @@ public String getDisplayName() {
* @return the formatted String
*/
public String display(double amount) {
return display.replace("%balance%", decimalFormat.format(amount)).replace(
"%wattsBalance%", Formatter.WATTS((float) amount)).replace(
"%bytesBalance%", Formatter.BYTES((float) amount)).replace(
"%gramsBalance%", Formatter.GRAMS((float) amount));
String balance = display.replace("%balance%", decimalFormat.format(amount));
return Formatter.getInstance().formatAll(balance, amount);
}

/**
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/us/mytheria/bloblib/itemstack/ItemStackBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.jetbrains.annotations.Nullable;
import us.mytheria.bloblib.utilities.TextColor;

import java.util.*;
Expand All @@ -23,15 +25,15 @@ public final class ItemStackBuilder {
private static final ItemFlag[] ALL_CONSTANTS = ItemFlag.values();

public static ItemStackBuilder build(ItemStack itemStack) {
return new ItemStackBuilder(itemStack.clone()).hideAll();
return new ItemStackBuilder(new ItemStack(itemStack));
}

public static ItemStackBuilder build(Material material) {
return build(material, 1);
}

public static ItemStackBuilder build(Material material, int amount) {
return build(new ItemStack(material, amount));
return new ItemStackBuilder(new ItemStack(material, amount));
}

public static ItemStackBuilder build(String material) {
Expand Down Expand Up @@ -249,11 +251,15 @@ public ItemStackBuilder color(Color color) {
});
}

public ItemStackBuilder attribute(Attribute attribute, double amount, AttributeModifier.Operation operation) {
public ItemStackBuilder attribute(
Attribute attribute,
double amount,
AttributeModifier.Operation operation,
@Nullable EquipmentSlot equipmentSlot) {
return itemMeta(itemMeta -> {
try {
UUID uuid = UUID.randomUUID();
AttributeModifier modifier = new AttributeModifier(uuid, uuid.toString(), amount, operation);
AttributeModifier modifier = new AttributeModifier(uuid, uuid.toString(), amount, operation, equipmentSlot);
itemMeta.addAttributeModifier(attribute, modifier);
} catch (Exception exception) {
Bukkit.getLogger().log(Level.SEVERE, exception, () -> "Failed to add attribute modifier");
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/us/mytheria/bloblib/itemstack/ItemStackReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -102,9 +103,19 @@ public static ItemStackBuilder READ_OR_FAIL_FAST(ConfigurationSection section) {
double amount = attributeSection.getDouble("Amount");
if (!attributeSection.isString("Operation"))
throw new ConfigurationFieldException("Attribute '" + key + "' is missing 'Operation' field");
EquipmentSlot equipmentSlot;
String readEquipmentSlot = attributeSection.getString("EquipmentSlot");
if (readEquipmentSlot != null) {
try {
equipmentSlot = EquipmentSlot.valueOf(readEquipmentSlot);
} catch (IllegalArgumentException exception) {
throw new ConfigurationFieldException("EquipmentSlot '" + readEquipmentSlot + "' is not valid");
}
} else
equipmentSlot = null;
AttributeModifier.Operation operation = AttributeModifier.Operation.valueOf(attributeSection.getString("Operation"));
uber.talk(uber.thanks().attribute(attribute, amount, operation));
} catch (IllegalArgumentException e) {
uber.talk(uber.thanks().attribute(attribute, amount, operation, equipmentSlot));
} catch (IllegalArgumentException exception) {
throw new ConfigurationFieldException("Attribute '" + key + "' has an invalid Operation");
}
});
Expand Down
41 changes: 37 additions & 4 deletions src/main/java/us/mytheria/bloblib/utilities/Formatter.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
package us.mytheria.bloblib.utilities;

import org.jetbrains.annotations.NotNull;

import java.text.DecimalFormat;

public class Formatter {
private static Formatter instance;

private Formatter() {
}

public static Formatter getInstance() {
if (instance == null)
instance = new Formatter();
return instance;
}

public String formatAll(
@NotNull String input,
double amount) {
return input.replace(
"%wattsBalance%", watts((float) amount)).replace(
"%bytesBalance%", bytes((float) amount)).replace(
"%gramsBalance%", grams((float) amount)).replace(
"%litersBalance%", liters((float) amount));
}

public static String WATTS(float value) {
private String watts(float value) {
String[] arr = {"", "k", "M", "G", "T", "P", "E", "Z", "Y"};
int index = 0;
while ((value / 1000) >= 1) {
Expand All @@ -15,7 +37,7 @@ public static String WATTS(float value) {
return String.format("%s%s", decimalFormat.format(value), arr[index]) + "W";
}

public static String BYTES(float value) {
private String bytes(float value) {
String[] arr = {"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"};
int index = 0;
while ((value / 1024) >= 1) {
Expand All @@ -26,8 +48,19 @@ public static String BYTES(float value) {
return String.format("%s%s", decimalFormat.format(value), arr[index]) + "B";
}

public static String GRAMS(float value) {
String[] arr = {"g", "kg", "t", "kt", "mt", "gt", "tt", "pt", "et", "zt"};
private String grams(float value) {
String[] arr = {"g", "kg", "t", "kt", "Mt", "Gt", "Tt", "Pt", "Et", "Zt"};
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]);
}

private String liters(float value) {
String[] arr = {"L", "kL", "ML", "GL", "TL", "PL", "EL", "ZL", "YL"};
int index = 0;
while ((value / 1000) >= 1) {
value = value / 1000;
Expand Down

0 comments on commit 4a3dc0b

Please sign in to comment.