Skip to content

Commit

Permalink
Leaderboard expansion and APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSzabo committed Jul 12, 2024
1 parent 56646f2 commit 41a7004
Show file tree
Hide file tree
Showing 17 changed files with 820 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ plugins {
}

group = "gg.auroramc"
version = "1.4.2"
version = "1.5.0-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_21
Expand Down
Empty file added identifier.sqlite
Empty file.
9 changes: 8 additions & 1 deletion src/main/java/gg/auroramc/aurora/Aurora.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import gg.auroramc.aurora.api.expansions.ExpansionManager;
import gg.auroramc.aurora.expansions.economy.EconomyExpansion;
import gg.auroramc.aurora.expansions.item.ItemExpansion;
import gg.auroramc.aurora.expansions.leaderboard.LeaderboardExpansion;
import gg.auroramc.aurora.expansions.numberformat.NumberFormatExpansion;
import gg.auroramc.aurora.expansions.placeholder.PlaceholderExpansion;
import gg.auroramc.aurora.expansions.region.RegionExpansion;
Expand Down Expand Up @@ -43,6 +44,12 @@ public static AuroraLogger logger() {
return l;
}

@Override
public void onLoad() {
expansionManager = new ExpansionManager();
expansionManager.preloadExpansion(LeaderboardExpansion.class);
}

@Override
public void onEnable() {
saveDefaultConfig();
Expand All @@ -67,11 +74,11 @@ public void onDisable() {
}

private void setupExpansions() {
expansionManager = new ExpansionManager();
expansionManager.loadExpansion(PlaceholderExpansion.class);
expansionManager.loadExpansion(EconomyExpansion.class);
expansionManager.loadExpansion(NumberFormatExpansion.class);
expansionManager.loadExpansion(ItemExpansion.class);
expansionManager.loadExpansion(LeaderboardExpansion.class);

if (DependencyManager.hasDep(Dep.WORLDGUARD)) {
expansionManager.loadExpansion(WorldGuardExpansion.class);
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/gg/auroramc/aurora/api/AuroraAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import gg.auroramc.aurora.expansions.economy.AuroraEconomy;
import gg.auroramc.aurora.expansions.economy.EconomyExpansion;
import gg.auroramc.aurora.expansions.item.ItemExpansion;
import gg.auroramc.aurora.expansions.leaderboard.LeaderboardExpansion;
import gg.auroramc.aurora.expansions.numberformat.NumberFormatExpansion;
import gg.auroramc.aurora.expansions.region.RegionExpansion;

Expand All @@ -31,9 +32,8 @@ public static AuroraLogger getLogger() {
/**
* Creates a custom logger with you plugin name as prefix.
*
* @param plugin the name of your plugin
* @param plugin the name of your plugin
* @param debugMode the supplier for the debug parameter
*
* @return the newly created logger
*/
public static AuroraLogger createLogger(String plugin, Supplier<Boolean> debugMode) {
Expand Down Expand Up @@ -68,6 +68,15 @@ public static AuroraEconomy getEconomy(String providerPluginName) {
return Aurora.getExpansionManager().getExpansion(EconomyExpansion.class).getEconomy(providerPluginName);
}

/**
* Get the leaderboard expansion.
*
* @return the leaderboard expansion
*/
public static LeaderboardExpansion getLeaderboards() {
return Aurora.getExpansionManager().getExpansion(LeaderboardExpansion.class);
}

/**
* Get an AuroraUser by its UUID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,51 @@

public class ExpansionManager {
private final Map<Class<? extends AuroraExpansion>, AuroraExpansion> expansions = new HashMap<>();
private final Map<Class<? extends AuroraExpansion>, AuroraExpansion> preload = new HashMap<>();

public <T extends AuroraExpansion> void loadExpansion(Class<T> clazz) {
public <T extends AuroraExpansion> void preloadExpansion(Class<T> clazz) {
try {
var expansion = clazz.getDeclaredConstructor().newInstance();
preload.put(clazz, expansion);
expansions.put(clazz, expansion);
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException |
InvocationTargetException e) {
Aurora.logger().severe("Failed to load expansion: " + clazz.getName());
}
}

public <T extends AuroraExpansion> void loadExpansion(Class<T> clazz) {
try {
if (preload.containsKey(clazz)) {
var expansion = preload.get(clazz);

if (expansion.canHook()) {
expansion.hook();
if (expansion instanceof Listener) {
Bukkit.getPluginManager().registerEvents((Listener) expansion, Aurora.getInstance());
}
}
preload.remove(clazz);
} else {
var expansion = clazz.getDeclaredConstructor().newInstance();

if(expansion.canHook()) {
expansions.put(clazz, expansion);
expansion.hook();
if(expansion instanceof Listener) {
Bukkit.getPluginManager().registerEvents((Listener) expansion, Aurora.getInstance());
if (expansion.canHook()) {
expansions.put(clazz, expansion);
expansion.hook();
if (expansion instanceof Listener) {
Bukkit.getPluginManager().registerEvents((Listener) expansion, Aurora.getInstance());
}
}
}
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException |
InvocationTargetException e) {
Aurora.logger().severe("Failed to load expansion: " + clazz.getName());
}
}

public <T extends AuroraExpansion> T getExpansion(Class<T> clazz) {
var expansion = expansions.get(clazz);
if(clazz.isInstance(expansion)) {
if (clazz.isInstance(expansion)) {
return clazz.cast(expansion);
}
return null;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/gg/auroramc/aurora/api/user/AuroraUser.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package gg.auroramc.aurora.api.user;

import com.google.common.collect.Maps;
import gg.auroramc.aurora.Aurora;
import gg.auroramc.aurora.expansions.leaderboard.model.LbEntry;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection;
Expand All @@ -23,6 +26,9 @@ public class AuroraUser {

private final AtomicBoolean loaded = new AtomicBoolean(true);

@Getter
private final Map<String, LbEntry> leaderboardEntries = Maps.newConcurrentMap();

public AuroraUser(UUID uuid) {
this.uuid = uuid;
}
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/gg/auroramc/aurora/api/user/UserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import gg.auroramc.aurora.api.user.storage.UserStorage;
import gg.auroramc.aurora.api.user.storage.YamlStorage;
import gg.auroramc.aurora.api.user.storage.sql.MySqlStorage;
import gg.auroramc.aurora.expansions.leaderboard.LeaderboardExpansion;
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import lombok.Getter;
import org.bukkit.Bukkit;
Expand All @@ -29,6 +30,7 @@
import java.util.concurrent.TimeUnit;

public class UserManager implements Listener {
@Getter
private final UserStorage storage;
private final ConcurrentHashMap<UUID, Object> playerLocks = new ConcurrentHashMap<>();
private final Set<Class<? extends UserDataHolder>> dataHolders = new HashSet<>();
Expand Down Expand Up @@ -150,21 +152,30 @@ public AuroraUser getUser(UUID uuid) {
public void loadUser(UUID uuid) {
CompletableFuture.runAsync(() -> {
synchronized (getPlayerLock(uuid)) {
var lbm = Aurora.getExpansionManager().getExpansion(LeaderboardExpansion.class);

storage.loadUser(uuid, dataHolders, user -> {
if (Bukkit.getPlayer(uuid) == null) return;

var maybeUser = cache.getIfPresent(uuid);

if (maybeUser != null && !maybeUser.isLoaded()) {
lbm.updateUser(user).join();
maybeUser.getLeaderboardEntries().putAll(lbm.loadUser(user.getUniqueId()).join());
maybeUser.loadFromUser(user);
Aurora.logger().debug("Updated user " + user.getUniqueId() + " in cache");

Bukkit.getGlobalRegionScheduler().run(Aurora.getInstance(),
(task) -> Bukkit.getPluginManager().callEvent(new AuroraUserLoadedEvent(user)));
} else {
lbm.updateUser(user).join();
user.getLeaderboardEntries().putAll(lbm.loadUser(user.getUniqueId()).join());
cache.put(uuid, user);
Aurora.logger().debug("Loaded user " + user.getUniqueId() + " into cache");
}

Bukkit.getGlobalRegionScheduler().run(Aurora.getInstance(),
(task) -> Bukkit.getPluginManager().callEvent(new AuroraUserLoadedEvent(user)));
Bukkit.getGlobalRegionScheduler().run(Aurora.getInstance(),
(task) -> Bukkit.getPluginManager().callEvent(new AuroraUserLoadedEvent(user)));
}
});
}
});
Expand Down
Loading

0 comments on commit 41a7004

Please sign in to comment.