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

feat: bukkittest, load user on join...

This commit is contained in:
xHyroM 2023-08-02 23:01:55 +02:00
parent 9a0b64b371
commit 7f56f34b47
No known key found for this signature in database
GPG key ID: BE0423F386C436AA
22 changed files with 267 additions and 81 deletions

View file

@ -1,13 +1,18 @@
package dev.xhyrom.lighteco.api.managers;
import dev.xhyrom.lighteco.api.model.currency.Currency;
import dev.xhyrom.lighteco.api.model.user.User;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Collection;
import java.util.List;
public interface CurrencyManager {
@NonNull Collection<Currency<?>> getRegisteredCurrencies();
Currency<?> getCurrency(@NonNull String identifier);
<T> Currency<T> getCurrency(@NonNull String identifier);
void registerCurrency(@NonNull Currency<?> currency);
List<User> getTopUsers(@NonNull Currency<?> currency, int length);
}

View file

@ -24,6 +24,13 @@ public abstract class Currency<T> {
public abstract boolean isPayable();
/**
* Get the users that have a balance in this currency
*
* @return The users
*/
public abstract T getDefaultBalance();
/**
* Calculate the tax for the given amount
* Used for payables
@ -35,11 +42,6 @@ public abstract class Currency<T> {
return 0;
};
// Implemented in common module
public List<User> getTopUsers(int length) {
throw new NotImplementedException();
}
/**
* Represents the type of currency
*/

View file

@ -27,4 +27,12 @@ public interface User {
* @return the balance
*/
<T> T getBalance(@NonNull Currency<T> currency);
/**
* Set the balance of this user for the specified currency.
*
* @param currency the currency
* @param balance the balance
*/
<T> void setBalance(@NonNull Currency<T> currency, @NonNull T balance);
}

View file

@ -1,15 +1,18 @@
package dev.xhyrom.lighteco.bukkit;
import dev.xhyrom.lighteco.bukkit.test.TestPlugin;
import dev.xhyrom.lighteco.bukkit.listeners.BukkitConnectionListener;
import org.bukkit.plugin.java.JavaPlugin;
public class BukkitLightEcoLoader extends JavaPlugin {
private final BukkitLightEcoPlugin plugin = new BukkitLightEcoPlugin();
@Override
public void onEnable() {
public void onLoad() {
plugin.enable();
}
new TestPlugin(this);
@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(new BukkitConnectionListener(plugin), this);
}
}

View file

@ -0,0 +1,37 @@
package dev.xhyrom.lighteco.bukkit.listeners;
import dev.xhyrom.lighteco.api.model.user.User;
import dev.xhyrom.lighteco.bukkit.BukkitLightEcoPlugin;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent;
public class BukkitConnectionListener implements Listener {
private final BukkitLightEcoPlugin plugin;
public BukkitConnectionListener(BukkitLightEcoPlugin plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerPreLogin(AsyncPlayerPreLoginEvent event) {
if (event.getLoginResult() != AsyncPlayerPreLoginEvent.Result.ALLOWED) {
return;
}
try {
User user = this.plugin.getStorage().loadUser(event.getUniqueId()).join();
} catch (Exception e) {
e.printStackTrace();
}
}
public void onPlayerQuit(PlayerQuitEvent event) {
// house keeper stuff
// for now:
this.plugin.getUserManager().unload(event.getPlayer().getUniqueId());
}
}

View file

@ -1,25 +0,0 @@
package dev.xhyrom.lighteco.bukkit.test;
import dev.xhyrom.lighteco.api.LightEcoProvider;
import org.bukkit.plugin.java.JavaPlugin;
public class TestPlugin {
private final JavaPlugin plugin;
public TestPlugin(JavaPlugin plugin) {
this.plugin = plugin;
plugin.getLogger().info("TestPlugin loaded!");
LightEcoProvider.get().getCurrencyManager().registerCurrency(new TestCurrency());
plugin.getLogger().info("TestCurrency registered!");
LightEcoProvider.get().getCurrencyManager().getRegisteredCurrencies().forEach(currency -> {
plugin.getLogger().info("Currency: " + currency.getIdentifier() + " (" + currency.getType() + ", " + currency.getValueType() + ", " + currency.isPayable() + ")");
currency.getTopUsers(5).forEach(user -> {
plugin.getLogger().info("User: " + user.getUniqueId() + " (" + user.getUsername() + ")");
});
});
}
}

42
bukkittest/.gitignore vendored Normal file
View file

@ -0,0 +1,42 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

View file

@ -0,0 +1,15 @@
plugins {
id("lighteco.platform-logic")
}
repositories {
maven("https://repo.papermc.io/repository/maven-public/")
}
dependencies {
compileOnly(project(":lighteco-api"))
compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT")
compileOnly("org.projectlombok:lombok:1.18.28")
annotationProcessor("org.projectlombok:lombok:1.18.28")
}

View file

@ -1,4 +1,4 @@
package dev.xhyrom.lighteco.bukkit.test;
package dev.xhyrom.lighteco.bukkittest;
import dev.xhyrom.lighteco.api.model.currency.Currency;
@ -21,4 +21,9 @@ public class TestCurrency extends Currency<Integer> {
public boolean isPayable() {
return true;
}
@Override
public Integer getDefaultBalance() {
return 0;
}
}

View file

@ -0,0 +1,61 @@
package dev.xhyrom.lighteco.bukkittest;
import dev.xhyrom.lighteco.api.LightEco;
import dev.xhyrom.lighteco.api.LightEcoProvider;
import dev.xhyrom.lighteco.api.managers.CurrencyManager;
import dev.xhyrom.lighteco.api.model.currency.Currency;
import dev.xhyrom.lighteco.api.model.user.User;
import io.papermc.paper.event.player.AsyncChatEvent;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class TestPlugin extends JavaPlugin implements Listener {
@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
getLogger().info("TestPlugin loaded!");
LightEco provider = LightEcoProvider.get();
CurrencyManager currencyManager = provider.getCurrencyManager();
currencyManager.registerCurrency(new TestCurrency());
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() + ")");
});*/
});
}
@EventHandler
public void onWalk(AsyncPlayerChatEvent event) {
Player player = event.getPlayer();
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()) {
case "balance": {
player.sendMessage(user.getBalance(currency).toString());
break;
}
case "add": {
user.setBalance(currency, user.getBalance(currency) + 1);
provider.getUserManager().saveUser(user).thenAccept(aVoid -> player.sendMessage("Saved!"));
break;
}
}
}
}

View file

@ -0,0 +1,10 @@
name: ${name}
description: ${description}
version: ${version}
main: dev.xhyrom.lighteco.bukkittest.TestPlugin
author: ${author}
api-version: 1.20
softdepend:
- Vault
- lighteco-bukkit

View file

@ -1,35 +0,0 @@
package dev.xhyrom.lighteco.common.api.impl;
import dev.xhyrom.lighteco.api.model.currency.Currency;
import dev.xhyrom.lighteco.api.model.user.User;
import java.util.List;
public class ApiCurrency extends Currency {
private final dev.xhyrom.lighteco.common.model.currency.Currency<?> handler;
public ApiCurrency(dev.xhyrom.lighteco.common.model.currency.Currency<?> handler) {
super(handler.getValueType());
this.handler = handler;
}
public String getIdentifier() {
return this.handler.getIdentifier();
}
public Currency.Type getType() {
return this.handler.getType();
}
public boolean isPayable() {
return this.handler.isPayable();
}
public List<User> getTopUsers(int length) {
return this.handler.getTopUsers(length)
.stream()
.map(ApiUserManager::wrap)
.toList();
}
}

View file

@ -2,10 +2,12 @@ package dev.xhyrom.lighteco.common.api.impl;
import dev.xhyrom.lighteco.api.managers.CurrencyManager;
import dev.xhyrom.lighteco.api.model.currency.Currency;
import dev.xhyrom.lighteco.api.model.user.User;
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
public class ApiCurrencyManager extends ApiAbstractManager<dev.xhyrom.lighteco.common.managers.currency.CurrencyManager> implements CurrencyManager {
@ -14,7 +16,7 @@ public class ApiCurrencyManager extends ApiAbstractManager<dev.xhyrom.lighteco.c
}
private Currency<?> wrap(dev.xhyrom.lighteco.common.model.currency.Currency<?> handler) {
return new ApiCurrency(handler);
return handler.getProxy();
}
@Override
@ -25,8 +27,8 @@ public class ApiCurrencyManager extends ApiAbstractManager<dev.xhyrom.lighteco.c
}
@Override
public Currency<?> getCurrency(@NonNull String identifier) {
return wrap(this.handler.getIfLoaded(identifier));
public <T> Currency<T> getCurrency(@NonNull String identifier) {
return (Currency<T>) wrap(this.handler.getIfLoaded(identifier));
}
@Override
@ -34,4 +36,12 @@ public class ApiCurrencyManager extends ApiAbstractManager<dev.xhyrom.lighteco.c
dev.xhyrom.lighteco.common.model.currency.Currency<?> internal = new dev.xhyrom.lighteco.common.model.currency.Currency<>(plugin, currency);
this.handler.registerCurrency(internal);
}
@Override
public List<User> getTopUsers(@NonNull Currency<?> currency, int length) {
dev.xhyrom.lighteco.common.model.currency.Currency<?> internal = this.handler.getIfLoaded(currency.getIdentifier());
return this.handler.getTopUsers(internal, length)
.stream().map(dev.xhyrom.lighteco.common.model.user.User::getProxy)
.collect(Collectors.toList());
}
}

View file

@ -25,6 +25,19 @@ public class ApiUser implements User {
@Override
public <T> T getBalance(@NonNull Currency<T> currency) {
return this.handler.getBalance(currency);
dev.xhyrom.lighteco.common.model.currency.Currency<T> internal = (dev.xhyrom.lighteco.common.model.currency.Currency<T>) this.handler.getPlugin()
.getCurrencyManager()
.getIfLoaded(currency.getIdentifier());
return this.handler.getBalance(internal);
}
@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()
.getCurrencyManager()
.getIfLoaded(currency.getIdentifier());
this.handler.setBalance(internal, balance);
}
}

View file

@ -19,7 +19,7 @@ public abstract class AbstractManager<I, T> implements Manager<I, T> {
@Override
public T getOrMake(I identifier) {
return this.map.getOrDefault(identifier, this.apply(identifier));
return this.map.computeIfAbsent(identifier, this::apply);
}
@Override

View file

@ -2,12 +2,16 @@ package dev.xhyrom.lighteco.common.managers.currency;
import dev.xhyrom.lighteco.common.managers.Manager;
import dev.xhyrom.lighteco.common.model.currency.Currency;
import dev.xhyrom.lighteco.common.model.user.User;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Collection;
import java.util.List;
public interface CurrencyManager extends Manager<String, Currency<?>> {
@NonNull Collection<Currency<?>> getRegisteredCurrencies();
void registerCurrency(@NonNull Currency<?> currency);
List<User> getTopUsers(@NonNull Currency<?> currency, int length);
}

View file

@ -2,10 +2,12 @@ package dev.xhyrom.lighteco.common.managers.currency;
import dev.xhyrom.lighteco.common.managers.AbstractManager;
import dev.xhyrom.lighteco.common.model.currency.Currency;
import dev.xhyrom.lighteco.common.model.user.User;
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Collection;
import java.util.List;
import static dev.xhyrom.lighteco.api.model.currency.Currency.Type;
@ -44,4 +46,10 @@ public class StandardCurrencyManager extends AbstractManager<String, Currency<?>
this.map.put(currency.getIdentifier(), currency);
}
// TODO: finish
@Override
public List<User> getTopUsers(@NonNull Currency<?> currency, int length) {
return null;
}
}

View file

@ -31,6 +31,10 @@ public class Currency<T> {
return proxy.getValueType();
}
public T getDefaultBalance() {
return proxy.getDefaultBalance();
}
public boolean isPayable() {
return proxy.isPayable();
}

View file

@ -1,11 +1,13 @@
package dev.xhyrom.lighteco.common.model.user;
import dev.xhyrom.lighteco.api.model.currency.Currency;
import dev.xhyrom.lighteco.common.api.impl.ApiUser;
import dev.xhyrom.lighteco.common.model.currency.Currency;
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import lombok.Getter;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class User {
@ -16,15 +18,19 @@ public class User {
@Getter
private final UUID uniqueId;
private final Map<Currency<?>, Number> balances = new HashMap<>();
public User(LightEcoPlugin plugin, UUID uniqueId) {
this.plugin = plugin;
this.uniqueId = uniqueId;
}
// TODO: finish
public <T> T getBalance(@NonNull Currency<T> currency) {
this.plugin.getCurrencyManager().getIfLoaded(currency.getIdentifier());
return null;
T balance = (T) balances.get(currency);
return balance == null ? currency.getDefaultBalance() : balance;
}
public <T> void setBalance(@NonNull Currency<T> currency, @NonNull T balance) {
balances.put(currency, (Number) balance);
}
}

View file

@ -21,7 +21,7 @@ public class StorageFactory {
private StorageProvider createProvider(String provider) {
switch (provider.toLowerCase()) {
case "memory":
return new MemoryStorageProvider();
return new MemoryStorageProvider(this.plugin);
default:
throw new IllegalArgumentException("Unknown storage provider: " + provider);
}

View file

@ -2,6 +2,7 @@ package dev.xhyrom.lighteco.common.storage.provider.memory;
import dev.xhyrom.lighteco.api.model.user.User;
import dev.xhyrom.lighteco.api.storage.StorageProvider;
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.HashMap;
@ -10,6 +11,11 @@ import java.util.UUID;
public class MemoryStorageProvider implements StorageProvider {
private final HashMap<UUID, User> userDatabase = new HashMap<>();
private final LightEcoPlugin plugin;
public MemoryStorageProvider(LightEcoPlugin plugin) {
this.plugin = plugin;
}
@Override
public @NonNull String[] getIdentifiers() {
return new String[] {"memory"};
@ -17,11 +23,17 @@ public class MemoryStorageProvider implements StorageProvider {
@Override
public @NonNull User loadUser(@NonNull UUID uniqueId) {
return userDatabase.get(uniqueId);
return createUser(uniqueId, userDatabase.get(uniqueId));
}
@Override
public void saveUser(@NonNull User user) {
userDatabase.put(user.getUniqueId(), user);
}
private User createUser(UUID uniqueId, User data) {
dev.xhyrom.lighteco.common.model.user.User user = this.plugin.getUserManager().getOrMake(uniqueId);
return user.getProxy();
}
}

View file

@ -4,6 +4,7 @@ sequenceOf(
"api",
"common",
"bukkit",
"bukkittest",
"velocity"
).forEach {
include("lighteco-$it")