Skip to content

Commit

Permalink
add minorType option
Browse files Browse the repository at this point in the history
  • Loading branch information
HimaJyun committed Jul 19, 2021
1 parent bd7031b commit 27c0c9d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Economy base plugin for Bukkit/Spigot
<dependency>
<groupId>jp.jyn</groupId>
<artifactId>Jecon</artifactId>
<version>2.2.0</version>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>jp.jyn</groupId>
<artifactId>Jecon</artifactId>
<version>2.2.0</version>
<version>2.2.1</version>
<url>https://github.com/HimaJyun/Jecon</url>
<description>Jecon is a simple economy plugin.</description>
<properties>
Expand All @@ -22,7 +22,7 @@
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<distributionManagement>
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/jp/jyn/jecon/config/MainConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ public class MainConfig {
}

public final static class FormatConfig {
public enum MinorType {OMIT, ACCURATE, ASIS}

public final String singularMajor;
public final String pluralMajor;
public final String singularMinor;
public final String pluralMinor;
public final TemplateParser format;
public final TemplateParser formatZeroMinor;
public final MinorType minorType;

private FormatConfig(ConfigurationSection config) {
singularMajor = config.getString("singularMajor");
Expand All @@ -52,6 +55,8 @@ private FormatConfig(ConfigurationSection config) {
} else {
formatZeroMinor = format;
}

minorType = MinorType.valueOf(config.getString("minorType").toUpperCase(Locale.ENGLISH));
}
}

Expand All @@ -78,7 +83,8 @@ private DatabaseConfig(ConfigurationSection config) {
username = null;
password = null;
} else if (type.equals("mysql")) {
url = String.format("jdbc:mysql://%s/%s", config.getString("mysql.host"), config.getString("mysql.name"));
url = String.format("jdbc:mysql://%s/%s", config.getString("mysql.host"), config.getString("mysql" +
".name"));
username = config.getString("mysql.username");
password = config.getString("mysql.password");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class MainMigration {
private final static String FILE = "config.yml";
private final static int CURRENT_VERSION = 3;
private final static int CURRENT_VERSION = 4;

private MainMigration() {}

Expand All @@ -27,6 +27,8 @@ public static boolean migration(ConfigurationSection config) {
v1to2(config);
case 2:
v2to3(config);
case 3:
v3to4(config);
break;
default:
logger.severe(MigrationUtils.ERROR_1);
Expand Down Expand Up @@ -81,6 +83,11 @@ private static void v2to3(ConfigurationSection config) {
config.set("version", 3);
}

private static void v3to4(ConfigurationSection config) {
config.set("format.minorType", "asis");
config.set("version", 4);
}

@SuppressWarnings("SpellCheckingInspection")
private static void v2DBProperties(ConfigurationSection config) {
config.set("database.mysql.properties.useSSL", "false");
Expand Down
40 changes: 39 additions & 1 deletion src/main/java/jp/jyn/jecon/repository/AbstractRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.OptionalDouble;
import java.util.OptionalLong;
import java.util.UUID;
import java.util.function.LongFunction;

public abstract class AbstractRepository implements BalanceRepository {
public final static int FRACTIONAL_DIGITS = 2;
Expand All @@ -24,13 +25,28 @@ public abstract class AbstractRepository implements BalanceRepository {

protected final Database db;
private final MainConfig.FormatConfig formatConfig;
private final LongFunction<String> minorFormat;
private final Map<UUID, Integer> uuidToIdCache;

protected AbstractRepository(MainConfig config, Database db) {
this.db = db;
formatConfig = config.format;

uuidToIdCache = new HashMap<>();

switch (formatConfig.minorType) {
case OMIT:
minorFormat = this::minorOmit;
break;
case ACCURATE:
minorFormat = this::minorAccurate;
break;
case ASIS:
minorFormat = String::valueOf;
break;
default:
throw new IllegalStateException();
}
}

private long double2long(double value) {
Expand All @@ -46,13 +62,35 @@ private String format(long value) {
long minor = value % MULTIPLIER;
TemplateVariable v = variable.clear()
.put("major", numberFormat.format(major))
.put("minor", minor)
.put("minor", minorFormat.apply(minor))
.put("majorcurrency", major > 1 ? formatConfig.pluralMajor : formatConfig.singularMajor)
.put("minorcurrency", minor > 1 ? formatConfig.pluralMinor : formatConfig.singularMinor);

return (minor == 0 ? formatConfig.formatZeroMinor : formatConfig.format).toString(v);
}

private String minorOmit(long minor) {
if (minor == 0) {
return "0";
} else if (minor < 10) {
return new String(new char[]{'0', (char) (minor + '0')});
} else if ((minor % 10) == 0) {
return String.valueOf(minor / 10);
} else {
return String.valueOf(minor);
}
}

private String minorAccurate(long minor) {
if (minor == 0) {
return "0";
} else if (minor < 10) {
return new String(new char[]{'0', (char) (minor + '0')});
} else {
return String.valueOf(minor);
}
}

protected final Integer getId(UUID uuid) {
return uuidToIdCache.computeIfAbsent(uuid, db::getId);
}
Expand Down
13 changes: 10 additions & 3 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ format:
singularMinor: "cent"
pluralMinor: "cents"
format: "{major} {majorcurrency} {minor} {minorcurrency}"
# Optional
#formatZeroMinor: "{major} {majorcurrency}"
# Example:
#format: "$ {major}.{minor}"
#format: "{major}円"
# Optional
#formatZeroMinor: "{major} {majorcurrency}"
minorType: asis # omit/accurate/asis
# Hint:
# omit: 1.02 -> 1.02, 3.40 -> 3.4
# accurate: 1.02 -> 1.02, 3.40 -> 3.40
# asis: 1.02 -> 1.2, 3.40 -> 3.40
# omit or accurate for number style format (eg: 1.02 -> $ 1.02, 3.40 -> $ 3.4 / $ 3.40)
# asis for text style format (eg: 1.02 -> 1 dollar 2 cents, 3.40 -> 3 dollars 40 cents)

database:
# Database type. (sqlite/mysql)
Expand Down Expand Up @@ -60,4 +67,4 @@ database:
idleTimeout: -1

# Don't touch
version: 3
version: 4

0 comments on commit 27c0c9d

Please sign in to comment.