mirror of
https://github.com/xHyroM/lighteco.git
synced 2024-11-25 08:11:04 +01:00
feat(common): use typed map instead normal hashmap
This commit is contained in:
parent
5a0945b041
commit
aa2a48e462
8 changed files with 87 additions and 20 deletions
|
@ -34,8 +34,8 @@ public abstract class Currency<T> {
|
||||||
* @param amount The amount to calculate the tax for
|
* @param amount The amount to calculate the tax for
|
||||||
* @return The tax
|
* @return The tax
|
||||||
*/
|
*/
|
||||||
public double calculateTax(T amount) {
|
public T calculateTax(T amount) {
|
||||||
return 0;
|
return getDefaultBalance();
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -26,7 +26,7 @@ public interface User {
|
||||||
* @param currency the currency
|
* @param currency the currency
|
||||||
* @return the balance
|
* @return the balance
|
||||||
*/
|
*/
|
||||||
<T> T getBalance(@NonNull Currency<T> currency);
|
<T> T getBalance(@NonNull Currency<?> currency);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the balance of this user for the specified currency.
|
* Set the balance of this user for the specified currency.
|
||||||
|
@ -34,5 +34,5 @@ public interface User {
|
||||||
* @param currency the currency
|
* @param currency the currency
|
||||||
* @param balance the balance
|
* @param balance the balance
|
||||||
*/
|
*/
|
||||||
<T> void setBalance(@NonNull Currency<T> currency, @NonNull T balance);
|
<T> void setBalance(@NonNull Currency<?> currency, @NonNull T balance);
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,4 +26,9 @@ public class TestCurrency extends Currency<Integer> {
|
||||||
public Integer getDefaultBalance() {
|
public Integer getDefaultBalance() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer calculateTax(Integer amount) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
package dev.xhyrom.lighteco.bukkittest;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.api.model.currency.Currency;
|
||||||
|
|
||||||
|
public class TestCurrency2 extends Currency<Double> {
|
||||||
|
public TestCurrency2() {
|
||||||
|
super(Double.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getIdentifier() {
|
||||||
|
return "test2";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Type getType() {
|
||||||
|
return Type.LOCAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isPayable() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Double getDefaultBalance() {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Double calculateTax(Double amount) {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
}
|
|
@ -25,14 +25,12 @@ public class TestPlugin extends JavaPlugin implements Listener {
|
||||||
CurrencyManager currencyManager = provider.getCurrencyManager();
|
CurrencyManager currencyManager = provider.getCurrencyManager();
|
||||||
|
|
||||||
currencyManager.registerCurrency(new TestCurrency());
|
currencyManager.registerCurrency(new TestCurrency());
|
||||||
|
currencyManager.registerCurrency(new TestCurrency2());
|
||||||
|
|
||||||
getLogger().info("TestCurrency registered!");
|
getLogger().info("TestCurrency registered!");
|
||||||
|
|
||||||
currencyManager.getRegisteredCurrencies().forEach(currency -> {
|
currencyManager.getRegisteredCurrencies().forEach(currency -> {
|
||||||
getLogger().info("Currency: " + currency.getIdentifier() + " (" + currency.getType() + ", " + currency.getValueType() + ", " + currency.isPayable() + ")");
|
getLogger().info("Currency: " + currency.getIdentifier() + " (" + currency.getType() + ", " + currency.getValueType() + ", " + currency.isPayable() + ")");
|
||||||
/*currencyManager.getTopUsers(currency, 5).forEach(user -> {
|
|
||||||
plugin.getLogger().info("User: " + user.getUniqueId() + " (" + user.getUsername() + ")");
|
|
||||||
});*/
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,17 +40,27 @@ public class TestPlugin extends JavaPlugin implements Listener {
|
||||||
|
|
||||||
LightEco provider = LightEcoProvider.get();
|
LightEco provider = LightEcoProvider.get();
|
||||||
CurrencyManager currencyManager = provider.getCurrencyManager();
|
CurrencyManager currencyManager = provider.getCurrencyManager();
|
||||||
Currency<Integer> currency = currencyManager.getCurrency("test");
|
|
||||||
|
|
||||||
User user = provider.getPlayerAdapter(Player.class).getUser(player);
|
User user = provider.getPlayerAdapter(Player.class).getUser(player);
|
||||||
|
|
||||||
switch (event.getMessage()) {
|
String message = event.getMessage();
|
||||||
|
String command = message.split(" ")[0];
|
||||||
|
String[] args = message.substring(command.length()).trim().split(" ");
|
||||||
|
|
||||||
|
Currency<?> currency = currencyManager.getCurrency(args[0]);
|
||||||
|
|
||||||
|
switch (command) {
|
||||||
case "balance": {
|
case "balance": {
|
||||||
player.sendMessage(user.getBalance(currency).toString());
|
player.sendMessage(user.getBalance(currency).toString());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "add": {
|
case "add": {
|
||||||
user.setBalance(currency, user.getBalance(currency) + 1);
|
if (currency.getValueType().equals(Integer.class)) {
|
||||||
|
user.setBalance(currency, Integer.parseInt(args[1]));
|
||||||
|
} else if (currency.getValueType().equals(Double.class)) {
|
||||||
|
user.setBalance(currency, Double.parseDouble(args[1]));
|
||||||
|
}
|
||||||
|
|
||||||
provider.getUserManager().saveUser(user).thenAccept(aVoid -> player.sendMessage("Saved!"));
|
provider.getUserManager().saveUser(user).thenAccept(aVoid -> player.sendMessage("Saved!"));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,8 +24,8 @@ public class ApiUser implements User {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T getBalance(@NonNull Currency<T> currency) {
|
public <T> T getBalance(@NonNull Currency<?> currency) {
|
||||||
dev.xhyrom.lighteco.common.model.currency.Currency<T> internal = (dev.xhyrom.lighteco.common.model.currency.Currency<T>) this.handler.getPlugin()
|
dev.xhyrom.lighteco.common.model.currency.Currency<?> internal = this.handler.getPlugin()
|
||||||
.getCurrencyManager()
|
.getCurrencyManager()
|
||||||
.getIfLoaded(currency.getIdentifier());
|
.getIfLoaded(currency.getIdentifier());
|
||||||
|
|
||||||
|
@ -33,8 +33,8 @@ public class ApiUser implements User {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> void setBalance(@NonNull Currency<T> currency, @NonNull T balance) {
|
public <T> void setBalance(@NonNull Currency<?> currency, @NonNull T balance) {
|
||||||
dev.xhyrom.lighteco.common.model.currency.Currency<T> internal = (dev.xhyrom.lighteco.common.model.currency.Currency<T>) this.handler.getPlugin()
|
dev.xhyrom.lighteco.common.model.currency.Currency<?> internal = this.handler.getPlugin()
|
||||||
.getCurrencyManager()
|
.getCurrencyManager()
|
||||||
.getIfLoaded(currency.getIdentifier());
|
.getIfLoaded(currency.getIdentifier());
|
||||||
|
|
||||||
|
|
20
common/src/main/java/dev/xhyrom/lighteco/common/cache/TypedMap.java
vendored
Normal file
20
common/src/main/java/dev/xhyrom/lighteco/common/cache/TypedMap.java
vendored
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
package dev.xhyrom.lighteco.common.cache;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class TypedMap<T> {
|
||||||
|
private final Map<T, Object> map = new HashMap<>();
|
||||||
|
|
||||||
|
public <V> V get(T key) {
|
||||||
|
return (V) map.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V> V getOrDefault(T key, V defaultValue) {
|
||||||
|
return (V) map.getOrDefault(key, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V> void put(T key, V value) {
|
||||||
|
map.put(key, value);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package dev.xhyrom.lighteco.common.model.user;
|
package dev.xhyrom.lighteco.common.model.user;
|
||||||
|
|
||||||
import dev.xhyrom.lighteco.common.api.impl.ApiUser;
|
import dev.xhyrom.lighteco.common.api.impl.ApiUser;
|
||||||
|
import dev.xhyrom.lighteco.common.cache.TypedMap;
|
||||||
import dev.xhyrom.lighteco.common.model.currency.Currency;
|
import dev.xhyrom.lighteco.common.model.currency.Currency;
|
||||||
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
@ -18,19 +19,18 @@ public class User {
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final UUID uniqueId;
|
private final UUID uniqueId;
|
||||||
private final Map<Currency<?>, Number> balances = new HashMap<>();
|
private final TypedMap<Currency<?>> balances = new TypedMap<>();
|
||||||
|
|
||||||
public User(LightEcoPlugin plugin, UUID uniqueId) {
|
public User(LightEcoPlugin plugin, UUID uniqueId) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.uniqueId = uniqueId;
|
this.uniqueId = uniqueId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T getBalance(@NonNull Currency<T> currency) {
|
public <T> T getBalance(@NonNull Currency<?> currency) {
|
||||||
T balance = (T) balances.get(currency);
|
return balances.<T>getOrDefault(currency, (T) currency.getDefaultBalance());
|
||||||
return balance == null ? currency.getDefaultBalance() : balance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> void setBalance(@NonNull Currency<T> currency, @NonNull T balance) {
|
public <T> void setBalance(@NonNull Currency<?> currency, @NonNull T balance) {
|
||||||
balances.put(currency, (Number) balance);
|
balances.put(currency, balance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue