Skip to content

Commit

Permalink
Merge pull request #17 from Tofpu/v1.0.5-patch
Browse files Browse the repository at this point in the history
v1.0.5 patch
  • Loading branch information
Tofpu authored Mar 16, 2022
2 parents 985c651 + 153968f commit 10e52a7
Show file tree
Hide file tree
Showing 15 changed files with 299 additions and 131 deletions.
9 changes: 7 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "io.tofpu.speedbridge2"
version = "1.0.4"
version = "1.0.5"

tasks {
compileJava {
Expand All @@ -24,6 +24,8 @@ tasks {
relocate("net.kyori.adventure", "io.tofpu.speedbridge2.lib.adventure")
relocate("com.zaxxer.HikariCP", "io.tofpu.speedbridge2.lib.hikaricp")
relocate("org.bstats", "io.tofpu.speedbridge2.lib.bstats")
relocate("com.github.benmanes.caffeine", "io.tofpu.speedbridge2.lib.caffeine")
relocate("org.apache.commons", "io.tofpu.speedbridge2.lib.commons")
}

exclude("META-INF/**")
Expand Down Expand Up @@ -57,7 +59,7 @@ repositories {
dependencies {
compileOnly("org.spigotmc:spigot-api:1.8.8-R0.1-SNAPSHOT")

implementation("org.xerial:sqlite-jdbc:3.36.0.3")
compileOnly("org.xerial:sqlite-jdbc:3.36.0.3")

compileOnly("com.sk89q:worldedit:6.0.0-SNAPSHOT")

Expand All @@ -74,11 +76,14 @@ dependencies {

implementation("org.spongepowered:configurate-hocon:4.1.2")
implementation("commons-lang:commons-lang:2.6")
implementation("commons-io:commons-io:2.11.0")

implementation("org.bstats:bstats-bukkit:3.0.0")

compileOnly("me.clip:placeholderapi:2.10.10")

implementation("com.github.ben-manes.caffeine:caffeine:3.0.5")

implementation("com.github.cryptomorin:XSeries:8.6.1")
implementation("com.github.tofpu.MultiWorldEdit:multiworldedit-api:f9ad4ce832") {
exclude("de.schlichtherle", "truezip")
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/io/tofpu/speedbridge2/SpeedBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public SpeedBridge(final JavaPlugin javaPlugin) {
}

public void load() {
// reset the world, in-case it does exist
SchematicManager.INSTANCE.resetWorld();
}

public void enable() {
adventure = BukkitAudiences.create(javaPlugin);

new Metrics(javaPlugin, 14597);
Expand All @@ -61,7 +66,7 @@ public void load() {
}

log("Loading the `speedbridge2` world...");
SchematicManager.INSTANCE.load(javaPlugin);
SchematicManager.INSTANCE.load();

IslandSetupManager.INSTANCE.load();

Expand All @@ -80,7 +85,7 @@ public void load() {
BridgeUtil.whenComplete(islandService.load(), () -> {

log("Loading the global/session leaderboard...");
BridgeUtil.whenComplete(Leaderboard.INSTANCE.load(), () -> {
BridgeUtil.whenComplete(Leaderboard.INSTANCE.loadAsync(), () -> {

log("Loading the island leaderboard...");
// when the global leaderboard is complete, load the per-island
Expand Down Expand Up @@ -132,7 +137,8 @@ public void shutdown() {
PlayerService.INSTANCE.shutdown();

log("Unloading the `speedbridge2` world...");
Bukkit.unloadWorld("speedbridge2", false);
SchematicManager.INSTANCE.unloadWorld();
SchematicManager.INSTANCE.resetWorld();

log("Complete.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void onPlayerReset(final CommonBridgePlayer<?> bridgePlayer, final @Argum
BridgePlayer target = PlayerService.INSTANCE.get(uuidResult);
if (target == null) {
try {
PlayerService.INSTANCE.load(uuidResult)
PlayerService.INSTANCE.loadAsync(uuidResult)
.get();
} catch (InterruptedException | ExecutionException e) {
BridgeUtil.sendMessage(bridgePlayer, INSTANCE.somethingWentWrong);
Expand Down Expand Up @@ -349,7 +349,6 @@ public void pluginReload(final CommonBridgePlayer<?> player) {

@CommandMethod("speedbridge|sb")
@CommandDescription("Shows a list of commands")
@CommandPermission("speedbridge.help")
@Hidden
public void onNoArgument(final CommonBridgePlayer<?> bridgePlayer) {
final CommandSender player = bridgePlayer.getPlayer();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.tofpu.speedbridge2.domain.extra.leaderboard;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.LoadingCache;
import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
import com.github.benmanes.caffeine.cache.Caffeine;
import io.tofpu.speedbridge2.domain.common.PluginExecutor;
import io.tofpu.speedbridge2.domain.common.config.manager.ConfigurationManager;
import io.tofpu.speedbridge2.domain.common.database.wrapper.DatabaseQuery;
Expand All @@ -17,6 +17,7 @@
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public final class Leaderboard {
Expand All @@ -25,31 +26,30 @@ public final class Leaderboard {
private final LeaderboardMap globalMap;
private final Map<Integer, BoardPlayer> sessionalMap;

private final LoadingCache<UUID, BoardPlayer> playerCache;
private final LoadingCache<UUID, IslandBoardPlayer> islandPositionMap;
private final AsyncLoadingCache<UUID, BoardPlayer> playerCache;
private final AsyncLoadingCache<UUID, IslandBoardPlayer> islandPositionMap;

private Leaderboard() {
this.globalMap = new LeaderboardMap();
this.sessionalMap = new ConcurrentHashMap<>();

// player's personal global position
this.playerCache = CacheBuilder.newBuilder()
this.playerCache = Caffeine.newBuilder()
.expireAfterAccess(5, TimeUnit.SECONDS)
.build(PersonalBoardLoader.INSTANCE);
.buildAsync(PersonalBoardLoader.INSTANCE);

// player's global position that based on an island
this.islandPositionMap = CacheBuilder.newBuilder()
this.islandPositionMap = Caffeine.newBuilder()
.expireAfterAccess(5, TimeUnit.SECONDS)
.build(IslandLoader.INSTANCE);
.buildAsync(IslandLoader.INSTANCE);
}

/**
* Load the leaderboard from the database
*
* @param javaPlugin The plugin that is calling this method.
* @return Nothing.
*/
public CompletableFuture<Void> load() {
public CompletableFuture<Void> loadAsync() {
final CompletableFuture<Void> loadFuture = new CompletableFuture<>();

BridgeUtil.runBukkitAsync(() -> {
Expand Down Expand Up @@ -107,7 +107,7 @@ public CompletableFuture<Void> load() {
// per-player based position operation
for (final UUID uuid : playerCache.asMap()
.keySet()) {
this.playerCache.refresh(uuid);
this.playerCache.synchronous().refresh(uuid);
}

// update the global leaderboard
Expand Down Expand Up @@ -161,16 +161,15 @@ public CompletableFuture<Void> load() {
* @return A CompletableFuture<BoardPlayer>
*/
public CompletableFuture<BoardPlayer> retrieve(final UUID uniqueId) {
final BoardPlayer player = playerCache.asMap()
final CompletableFuture<BoardPlayer> completableFuture = playerCache.asMap()
.get(uniqueId);

// if the board player is found, return the completed value
if (player != null) {
return CompletableFuture.completedFuture(player);
if (completableFuture == null) {
// loading the board player
return playerCache.get(uniqueId);
}

// otherwise, attempt to load the player board async
return CompletableFuture.supplyAsync(() -> playerCache.getUnchecked(uniqueId));
return completableFuture;
}

/**
Expand Down Expand Up @@ -200,8 +199,8 @@ public BoardPlayer retrieve(final LeaderboardRetrieveType leaderboardRetrieveTyp
* @return The IslandBoard object.
*/
public CompletableFuture<IslandBoardPlayer.IslandBoard> retrieve(final UUID uniqueId, final int islandSlot) {
final IslandBoardPlayer player = islandPositionMap.asMap()
.get(uniqueId);
final IslandBoardPlayer player =
islandPositionMap.synchronous().getIfPresent(uniqueId);
final IslandBoardPlayer.IslandBoard islandBoard =
player == null ? null : player.findDefault(islandSlot);

Expand All @@ -211,8 +210,13 @@ public CompletableFuture<IslandBoardPlayer.IslandBoard> retrieve(final UUID uniq
}

// otherwise, attempt to retrieve the board async
return PluginExecutor.supply(() -> islandPositionMap.getUnchecked(uniqueId)
.retrieve(islandSlot));
return PluginExecutor.supply(() -> {
try {
return islandPositionMap.get(uniqueId).get().retrieve(islandSlot);
} catch (InterruptedException | ExecutionException e) {
throw new IllegalStateException(e);
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package io.tofpu.speedbridge2.domain.extra.leaderboard.loader;

import com.google.common.cache.CacheLoader;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.github.benmanes.caffeine.cache.CacheLoader;
import io.tofpu.speedbridge2.domain.common.util.BridgeUtil;
import io.tofpu.speedbridge2.domain.extra.leaderboard.meta.BoardRetrieve;
import io.tofpu.speedbridge2.domain.extra.leaderboard.wrapper.IslandBoardPlayer;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

public final class IslandLoader extends CacheLoader<UUID, IslandBoardPlayer> implements BoardRetrieve<IslandBoardPlayer> {
public final class IslandLoader implements CacheLoader<UUID, IslandBoardPlayer>,
BoardRetrieve<IslandBoardPlayer> {
public static final IslandLoader INSTANCE = new IslandLoader();

private IslandLoader() {}
Expand All @@ -22,14 +23,20 @@ private IslandLoader() {}
}

@Override
public ListenableFuture<IslandBoardPlayer> reload(final @NotNull UUID key, final @NotNull IslandBoardPlayer oldValue) {
public CompletableFuture<? extends IslandBoardPlayer> asyncReload(final UUID key, final IslandBoardPlayer oldValue, final Executor executor) throws Exception {
BridgeUtil.debug("attempting to reload " + key);
return Futures.immediateFuture(retrieve(key));
return retrieveAsync(key, executor);
}

@Override
public @NotNull IslandBoardPlayer retrieve(final @NotNull UUID key) {
public IslandBoardPlayer retrieve(final @NotNull UUID uniqueId) {
return new IslandBoardPlayer(uniqueId);
}

@Override
public @NotNull CompletableFuture<IslandBoardPlayer> retrieveAsync(final @NotNull UUID key,
final @NotNull Executor executor) {
BridgeUtil.debug("retrieving " + key);
return new IslandBoardPlayer(key);
return CompletableFuture.supplyAsync(() -> retrieve(key), executor);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package io.tofpu.speedbridge2.domain.extra.leaderboard.loader;

import com.google.common.cache.CacheLoader;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.github.benmanes.caffeine.cache.CacheLoader;
import io.tofpu.speedbridge2.domain.common.PlayerNameCache;
import io.tofpu.speedbridge2.domain.common.PluginExecutor;
import io.tofpu.speedbridge2.domain.common.database.wrapper.DatabaseQuery;
import io.tofpu.speedbridge2.domain.common.database.wrapper.DatabaseSet;
import io.tofpu.speedbridge2.domain.common.util.BridgeUtil;
Expand All @@ -16,60 +15,86 @@
import org.jetbrains.annotations.Nullable;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicReference;

public final class PersonalBoardLoader extends CacheLoader<UUID, BoardPlayer> implements BoardRetrieve<BoardPlayer> {
public final class PersonalBoardLoader implements CacheLoader<UUID, BoardPlayer>, BoardRetrieve<BoardPlayer> {
public static final PersonalBoardLoader INSTANCE = new PersonalBoardLoader();
private static final String GLOBAL_POSITION = "SELECT DISTINCT 1 + COUNT(*) AS " +
"position FROM scores WHERE score < (SELECT score FROM scores WHERE uid = ?)";

private PersonalBoardLoader() {}


@Override
public @Nullable BoardPlayer load(final @NotNull UUID key) throws Exception {
public @Nullable BoardPlayer load(final UUID key) throws Exception {
return retrieve(key);
}

@Override
public ListenableFuture<BoardPlayer> reload(final @NotNull UUID key, final @NotNull BoardPlayer oldValue) {
return Futures.immediateFuture(retrieve(key));
public CompletableFuture<? extends BoardPlayer> asyncLoad(final UUID key, final Executor executor) {
return retrieveAsync(key, executor);
}

@Override
public @Nullable BoardPlayer retrieve(final @NotNull UUID key) {
BridgeUtil.debug("PersonalBoardLoader#retrieve(): key: " + key);
try (final DatabaseQuery databaseQuery = DatabaseQuery.query(GLOBAL_POSITION)) {
databaseQuery.setString(key.toString());

final AtomicReference<BoardPlayer> boardPlayer = new AtomicReference<>();
databaseQuery.executeQuery(resultSet -> {
BridgeUtil.debug("PersonalBoardLoader#retrieve(): executeQuery:");
if (!resultSet.next()) {
System.out.println("PersonalBoardLoader#retrieve(): next: " + "false");
return;
}
public CompletableFuture<? extends BoardPlayer> asyncReload(final UUID key, final BoardPlayer oldValue, final Executor executor) throws Exception {
return retrieveAsync(key, executor);
}

BridgeUtil.debug("PersonalBoardLoader#retrieve(): next: " + "true");
BoardPlayer value = toBoardPlayer(key, resultSet);
final BridgePlayer player = PlayerService.INSTANCE.get(key);
if (player != null && player.getScores().isEmpty()) {
value = new BoardPlayer(value.getName(), 0, key, value.getScore());
}
@Override
public BoardPlayer retrieve(final @NotNull UUID uniqueId) {
final CompletableFuture<BoardPlayer> future = retrieveAsync(uniqueId, PluginExecutor.INSTANCE);
if (!future.isDone()) {
return null;
}

boardPlayer.set(value);
});
try {
return future.get();
} catch (InterruptedException | ExecutionException e) {
throw new IllegalStateException(e);
}
}

final BoardPlayer player = boardPlayer.get();
@Override
public @NotNull CompletableFuture<BoardPlayer> retrieveAsync(final @NotNull UUID key,
final @NotNull Executor executor) {
return CompletableFuture.supplyAsync(() -> {
BridgeUtil.debug("PersonalBoardLoader#retrieve(): key: " + key);
try (final DatabaseQuery databaseQuery = DatabaseQuery.query(GLOBAL_POSITION)) {
databaseQuery.setString(key.toString());

final AtomicReference<BoardPlayer> boardPlayer = new AtomicReference<>();
databaseQuery.executeQuery(resultSet -> {
BridgeUtil.debug("PersonalBoardLoader#retrieve(): executeQuery:");
if (!resultSet.next()) {
System.out.println("PersonalBoardLoader#retrieve(): next: " + "false");
return;
}

BridgeUtil.debug("PersonalBoardLoader#retrieve(): next: " + "true");
BoardPlayer value = toBoardPlayer(key, resultSet);
final BridgePlayer player = PlayerService.INSTANCE.get(key);
if (player != null && player.getScores().isEmpty()) {
value = new BoardPlayer(value.getName(), 0, key, value.getScore());
}

boardPlayer.set(value);
});

BridgeUtil.debug("PersonalBoardLoader#retrieve(): player: " + player);
if (player == null) {
return new BoardPlayer(PlayerNameCache.INSTANCE.getOrDefault(key),
0, key, new Score(-1, -1));
final BoardPlayer player = boardPlayer.get();

BridgeUtil.debug("PersonalBoardLoader#retrieve(): player: " + player);
if (player == null) {
return new BoardPlayer(PlayerNameCache.INSTANCE.getOrDefault(key),
0, key, new Score(-1, -1));
}
return player;
} catch (final Exception e) {
throw new IllegalStateException(e);
}
return player;
} catch (final Exception e) {
throw new IllegalStateException(e);
}
}, executor);
}

public BoardPlayer toBoardPlayer(final UUID uid, final DatabaseSet databaseSet) {
Expand Down
Loading

0 comments on commit 10e52a7

Please sign in to comment.