1
0
Fork 0
mirror of https://github.com/xHyroM/lighteco.git synced 2024-12-22 12:31:05 +01:00

feat(common): use typed map instead normal hashmap

This commit is contained in:
xHyroM 2023-08-03 19:57:10 +02:00
parent 5a0945b041
commit aa2a48e462
No known key found for this signature in database
GPG key ID: BE0423F386C436AA
8 changed files with 87 additions and 20 deletions

View file

@ -34,8 +34,8 @@ public abstract class Currency<T> {
* @param amount The amount to calculate the tax for
* @return The tax
*/
public double calculateTax(T amount) {
return 0;
public T calculateTax(T amount) {
return getDefaultBalance();
};
/**

View file

@ -26,7 +26,7 @@ public interface User {
* @param currency the currency
* @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.
@ -34,5 +34,5 @@ public interface User {
* @param currency the currency
* @param balance the balance
*/
<T> void setBalance(@NonNull Currency<T> currency, @NonNull T balance);
<T> void setBalance(@NonNull Currency<?> currency, @NonNull T balance);
}

View file

@ -26,4 +26,9 @@ public class TestCurrency extends Currency<Integer> {
public Integer getDefaultBalance() {
return 0;
}
@Override
public Integer calculateTax(Integer amount) {
return 0;
}
}

View file

@ -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;
}
}

View file

@ -25,14 +25,12 @@ public class TestPlugin extends JavaPlugin implements Listener {
CurrencyManager currencyManager = provider.getCurrencyManager();
currencyManager.registerCurrency(new TestCurrency());
currencyManager.registerCurrency(new TestCurrency2());
getLogger().info("TestCurrency registered!");
currencyManager.getRegisteredCurrencies().forEach(currency -> {
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();
CurrencyManager currencyManager = provider.getCurrencyManager();
Currency<Integer> currency = currencyManager.getCurrency("test");
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": {
player.sendMessage(user.getBalance(currency).toString());
break;
}
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!"));
break;
}

View file

@ -24,8 +24,8 @@ public class ApiUser implements User {
}
@Override
public <T> T getBalance(@NonNull Currency<T> currency) {
dev.xhyrom.lighteco.common.model.currency.Currency<T> internal = (dev.xhyrom.lighteco.common.model.currency.Currency<T>) this.handler.getPlugin()
public <T> T getBalance(@NonNull Currency<?> currency) {
dev.xhyrom.lighteco.common.model.currency.Currency<?> internal = this.handler.getPlugin()
.getCurrencyManager()
.getIfLoaded(currency.getIdentifier());
@ -33,8 +33,8 @@ public class ApiUser implements User {
}
@Override
public <T> void setBalance(@NonNull Currency<T> currency, @NonNull T balance) {
dev.xhyrom.lighteco.common.model.currency.Currency<T> internal = (dev.xhyrom.lighteco.common.model.currency.Currency<T>) this.handler.getPlugin()
public <T> void setBalance(@NonNull Currency<?> currency, @NonNull T balance) {
dev.xhyrom.lighteco.common.model.currency.Currency<?> internal = this.handler.getPlugin()
.getCurrencyManager()
.getIfLoaded(currency.getIdentifier());

View 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);
}
}

View file

@ -1,6 +1,7 @@
package dev.xhyrom.lighteco.common.model.user;
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.plugin.LightEcoPlugin;
import lombok.Getter;
@ -18,19 +19,18 @@ public class User {
@Getter
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) {
this.plugin = plugin;
this.uniqueId = uniqueId;
}
public <T> T getBalance(@NonNull Currency<T> currency) {
T balance = (T) balances.get(currency);
return balance == null ? currency.getDefaultBalance() : balance;
public <T> T getBalance(@NonNull Currency<?> currency) {
return balances.<T>getOrDefault(currency, (T) currency.getDefaultBalance());
}
public <T> void setBalance(@NonNull Currency<T> currency, @NonNull T balance) {
balances.put(currency, (Number) balance);
public <T> void setBalance(@NonNull Currency<?> currency, @NonNull T balance) {
balances.put(currency, balance);
}
}