Add Coal Cents economy
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package com.librewiki.coalgov.util;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public final class CoalMoney {
|
||||
public static final long CENTS_PER_COAL = 100L;
|
||||
|
||||
private CoalMoney() {
|
||||
}
|
||||
|
||||
public static long fromCoal(double coal) {
|
||||
return Math.round(coal * CENTS_PER_COAL);
|
||||
}
|
||||
|
||||
public static long fromCoalConfig(double coal) {
|
||||
return Math.max(0L, fromCoal(coal));
|
||||
}
|
||||
|
||||
public static String format(long cents) {
|
||||
boolean negative = cents < 0L;
|
||||
long absolute = Math.abs(cents);
|
||||
long coal = absolute / CENTS_PER_COAL;
|
||||
long coalCents = absolute % CENTS_PER_COAL;
|
||||
String prefix = negative ? "-" : "";
|
||||
if (coal > 0L && coalCents > 0L) {
|
||||
return prefix + coal + " coal " + coalCents + "cc";
|
||||
}
|
||||
if (coal > 0L) {
|
||||
return prefix + coal + " coal";
|
||||
}
|
||||
return prefix + coalCents + "cc";
|
||||
}
|
||||
|
||||
public static long parsePositive(String raw) {
|
||||
long amount = parse(raw);
|
||||
return amount > 0L ? amount : -1L;
|
||||
}
|
||||
|
||||
public static long parse(String raw) {
|
||||
if (raw == null) {
|
||||
return -1L;
|
||||
}
|
||||
String normalized = raw.trim().toLowerCase();
|
||||
if (normalized.isEmpty() || normalized.startsWith("-")) {
|
||||
return -1L;
|
||||
}
|
||||
try {
|
||||
if (normalized.endsWith("cc")) {
|
||||
return Long.parseLong(normalized.substring(0, normalized.length() - 2));
|
||||
}
|
||||
if (normalized.endsWith("c")) {
|
||||
return parseDecimalCoal(normalized.substring(0, normalized.length() - 1));
|
||||
}
|
||||
if (normalized.endsWith("coal")) {
|
||||
return parseDecimalCoal(normalized.substring(0, normalized.length() - 4));
|
||||
}
|
||||
return parseDecimalCoal(normalized);
|
||||
} catch (ArithmeticException | NumberFormatException exception) {
|
||||
return -1L;
|
||||
}
|
||||
}
|
||||
|
||||
private static long parseDecimalCoal(String raw) {
|
||||
BigDecimal coal = new BigDecimal(raw.trim());
|
||||
BigDecimal cents = coal.multiply(BigDecimal.valueOf(CENTS_PER_COAL));
|
||||
return cents.setScale(0, RoundingMode.HALF_UP).longValueExact();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.librewiki.coalgov.util;
|
||||
|
||||
public record Cuboid2D(String world, int x1, int z1, int x2, int z2) {
|
||||
public Cuboid2D {
|
||||
int minX = Math.min(x1, x2);
|
||||
int maxX = Math.max(x1, x2);
|
||||
int minZ = Math.min(z1, z2);
|
||||
int maxZ = Math.max(z1, z2);
|
||||
x1 = minX;
|
||||
x2 = maxX;
|
||||
z1 = minZ;
|
||||
z2 = maxZ;
|
||||
}
|
||||
|
||||
public boolean contains(String worldName, int x, int z) {
|
||||
return world.equals(worldName) && x >= x1 && x <= x2 && z >= z1 && z <= z2;
|
||||
}
|
||||
|
||||
public boolean overlaps(Cuboid2D other) {
|
||||
return world.equals(other.world)
|
||||
&& x1 <= other.x2
|
||||
&& x2 >= other.x1
|
||||
&& z1 <= other.z2
|
||||
&& z2 >= other.z1;
|
||||
}
|
||||
|
||||
public long area() {
|
||||
return (long) (x2 - x1 + 1) * (z2 - z1 + 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.librewiki.coalgov.util;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
public final class Messages {
|
||||
private final FileConfiguration config;
|
||||
|
||||
public Messages(FileConfiguration config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public String prefix() {
|
||||
return color(config.getString("messages.prefix", "&8[&6CoalGov&8]&r "));
|
||||
}
|
||||
|
||||
public String text(String message) {
|
||||
return prefix() + color(message);
|
||||
}
|
||||
|
||||
public String raw(String message) {
|
||||
return color(message);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private String color(String message) {
|
||||
return ChatColor.translateAlternateColorCodes('&', message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user