Skip to content

Commit

Permalink
Merge pull request #50 from IllusionTheDev/2.0-menus
Browse files Browse the repository at this point in the history
2.0 menus
  • Loading branch information
IllusionTheDev authored Jan 2, 2024
2 parents 39ca1d4 + 46a7314 commit 451012b
Show file tree
Hide file tree
Showing 64 changed files with 1,469 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import me.illusion.skyblockcore.common.database.registry.SkyblockDatabaseRegistry;
import me.illusion.skyblockcore.common.event.manager.SkyblockEventManager;
import me.illusion.skyblockcore.common.event.manager.SkyblockEventManagerImpl;
import me.illusion.skyblockcore.common.platform.SkyblockPlatformProvider;
import me.illusion.skyblockcore.common.registry.Registries;
import me.illusion.skyblockcore.common.scheduler.SkyblockScheduler;
import me.illusion.skyblockcore.common.utilities.file.IOUtils;
import me.illusion.skyblockcore.proxy.SkyblockProxyPlatform;
import me.illusion.skyblockcore.proxy.command.PlaySkyblockCommand;
Expand All @@ -23,6 +26,7 @@
import me.illusion.skyblockcore.proxy.matchmaking.data.SkyblockServerMatchmaker;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.api.plugin.PluginManager;

/**
* Bungee implementation of {@link SkyblockProxyPlatform}.
Expand All @@ -34,6 +38,7 @@ public class SkyblockBungeePlugin extends Plugin implements SkyblockProxyPlatfor

private SkyblockDatabaseRegistry databaseRegistry;
private SkyblockEventManager eventManager;
private SkyblockScheduler scheduler;

private SkyblockServerMatchmaker matchmaker;
private SkyblockServerComparatorRegistry matchmakerComparatorRegistry;
Expand All @@ -43,8 +48,11 @@ public class SkyblockBungeePlugin extends Plugin implements SkyblockProxyPlatfor
private SkyblockCommandManager<SkyblockAudience> commandManager;
private SkyblockMessagesFile messagesFile;

private Registries registries;

@Override
public void onEnable() {
registries = new Registries();
configurationProvider = new BungeeConfigurationProvider(this);

commandManager = new BungeeSkyblockCommandManager(this);
Expand All @@ -62,6 +70,11 @@ public void onEnable() {
ProxyServer.getInstance().getScheduler().schedule(this, this::loadDatabase, 1, TimeUnit.SECONDS);
}

@Override
public void onLoad() {
SkyblockPlatformProvider.setPlatform(this);
}

private void finishEnable() {
matchmaker = new BungeeSkyblockMatchmaker(this);
}
Expand Down Expand Up @@ -103,7 +116,9 @@ public void onDisable() {

@Override
public void disableExceptionally() {
ProxyServer.getInstance().getPluginManager().unregisterListeners(this);
ProxyServer.getInstance().getPluginManager().unregisterCommands(this);
PluginManager pluginManager = ProxyServer.getInstance().getPluginManager();

pluginManager.unregisterListeners(this);
pluginManager.unregisterCommands(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void registerRoot(String name) {

@Override
public void syncCommands() {

// BungeeCord does not need to sync commands
}

private class BungeeCommand extends Command implements TabExecutor {
Expand Down
1 change: 1 addition & 0 deletions SkyblockCore-Common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
implementation 'redis.clients:jedis:5.0.1'

compileOnly 'org.mongodb:mongodb-driver-sync:4.10.2'
compileOnly "com.influxdb:influxdb-client-java:6.10.0"
}

def targetJavaVersion = 17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ protected <T extends SkyblockAudience> void handle(SkyblockAudience audience, St
return;
}

if (!result.isSuccess()) {
messages.sendMessage(audience, "invalid-argument");
return;
}

CommandContext context = result.getContext();
SkyblockCommand<T> command = (SkyblockCommand<T>) argumentNode.getCommand();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public TargetResult getTargetNode(String fullInput) {
}

if (node.getChildren().isEmpty() && fullInput.equalsIgnoreCase(node.getName())) {
return new TargetResult(node, new MutatingCommandContext(fullInput));
return new TargetResult(node, new MutatingCommandContext(fullInput), true);
}

CommandNode target = node;
Expand All @@ -67,7 +67,7 @@ public TargetResult getTargetNode(String fullInput) {
boolean isLast = index == split.length - 1;

if (children == null) {
return null;
return new TargetResult(target, context, false);
}

CommandNode child = null;
Expand All @@ -86,17 +86,13 @@ public TargetResult getTargetNode(String fullInput) {
}

if (child == null) {
return null;
return new TargetResult(target, context, false);
}

target = child;
}

if (!valid) {
return null;
}

return new TargetResult(target, context);
return new TargetResult(target, context, valid);
}

/**
Expand Down Expand Up @@ -235,10 +231,13 @@ public static class TargetResult {

private final CommandNode node;
private final CommandContext context;
private final boolean success;


public TargetResult(CommandNode node, CommandContext context) {
public TargetResult(CommandNode node, CommandContext context, boolean success) {
this.node = node;
this.context = context;
this.success = success;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ public class IslandData implements Serializable {
private final UUID islandId;
private final UUID ownerId; // Profile ID, not player

// TODO: Add more data here

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package me.illusion.skyblockcore.common.database.metrics.influx;

import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.WriteApi;
import com.influxdb.client.WriteOptions;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.Point;
import com.influxdb.client.write.events.WriteSuccessEvent;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import me.illusion.skyblockcore.common.config.section.ConfigurationSection;
import me.illusion.skyblockcore.common.database.AbstractSkyblockDatabase;
import me.illusion.skyblockcore.common.metrics.SkyblockMetric;
import me.illusion.skyblockcore.common.metrics.SkyblockMetricBroker;
import me.illusion.skyblockcore.common.metrics.data.InternalSkyblockMetric;
import me.illusion.skyblockcore.common.metrics.data.MetricTimestamp;
import me.illusion.skyblockcore.common.metrics.provider.SkyblockMetricProvider;
import me.illusion.skyblockcore.common.platform.SkyblockPlatform;
import me.illusion.skyblockcore.common.scheduler.ThreadContext;
import me.illusion.skyblockcore.common.utilities.time.Time;
import me.illusion.skyblockcore.common.utilities.time.TimeParser;

public class InfluxMetricsDatabase extends AbstractSkyblockDatabase implements SkyblockMetricBroker {

private final Map<String, SkyblockMetricProvider> providers = new ConcurrentHashMap<>();

private CompletableFuture<Void> currentWrite = new CompletableFuture<>();

private InfluxDBClient client;
private WriteApi writeApi;

@Override
public String getName() {
return "influx";
}

@Override
public CompletableFuture<Boolean> enable(SkyblockPlatform platform, ConfigurationSection properties) {
setProperties(properties);

return associate(() -> {
String host = properties.getString("host");
int port = properties.getInt("port");
String bucket = properties.getString("bucket");
String token = properties.getString("token");
String org = properties.getString("org");

client = InfluxDBClientFactory.create(host + ":" + port, token.toCharArray(), org, bucket);

if (!client.ping()) {
return false;
}

Time updateTime = TimeParser.parse(properties.getString("update-interval", "5 seconds"));

platform.getScheduler().scheduleRepeating(ThreadContext.SYNC, () -> run(ThreadContext.SYNC), updateTime, updateTime);
platform.getScheduler().scheduleRepeating(ThreadContext.ASYNC, () -> run(ThreadContext.ASYNC), updateTime, updateTime);

Time batchTime = TimeParser.parse(properties.getString("batch-time", "1 second"));

writeApi = client.makeWriteApi(
WriteOptions.builder()
.flushInterval(batchTime.as(TimeUnit.MILLISECONDS))
.build()
);

writeApi.listenEvents(WriteSuccessEvent.class, event -> {
currentWrite.complete(null);
currentWrite = new CompletableFuture<>();
});

return true;
});
}

@Override
public CompletableFuture<Void> wipe() {
// no-op
// TODO: Use Offsets and stuff
return CompletableFuture.completedFuture(null);
}

@Override
public SkyblockMetric createMetric(String name) {
MetricTimestamp timestamp = MetricTimestamp.currentMillis();

return new InternalSkyblockMetric(this, name, timestamp);
}

@Override
public void registerProvider(SkyblockMetricProvider provider) {
providers.put(provider.getName(), provider);
}

@Override
public CompletableFuture<Void> submit(SkyblockMetric metric) {
InternalSkyblockMetric internalMetric = (InternalSkyblockMetric) metric;
MetricTimestamp timestamp = internalMetric.getTimestamp();

long ns = timestamp.getTimeUnit().convert(timestamp.getTime(), TimeUnit.NANOSECONDS);

Point point = Point.measurement(internalMetric.getName())
.addFields(internalMetric.getData())
.time(ns, WritePrecision.NS);

writeApi.writePoint(point);

return currentWrite;
}

public void run(ThreadContext context) {
for (SkyblockMetricProvider provider : providers.values()) {

if (provider.getThreadContext() != context) {
continue;
}

SkyblockMetric metric = createMetric(provider.getName());
provider.fillData(metric);
submit(metric);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public abstract class AbstractRemoteSQLPersistenceDatabase extends AbstractSQLPe
protected String username;
protected String password;

public AbstractRemoteSQLPersistenceDatabase() {
protected AbstractRemoteSQLPersistenceDatabase() {
addTag(SkyblockDatabaseTag.REMOTE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,47 +41,31 @@ public CompletableFuture<Boolean> enable(ConfigurationSection properties) {
});
}

protected ResultSet runQuery(String query, List<StatementObject> list) {
protected <T> T runQuery(String query, List<StatementObject> list, ResultSetFunction<T> function) {
try (PreparedStatement statement = getConnection().prepareStatement(query)) {
for (int index = 0; index < list.size(); index++) {
list.get(index).applyTo(statement, index + 1);
}

return statement.executeQuery();
} catch (Exception ex) {
ex.printStackTrace();
}

return null;
}

protected <T> CompletableFuture<T> runQueryAsync(String query, List<StatementObject> list, ResultSetFunction<T> function) {
return associate(() -> {
ResultSet set = runQuery(query, list);

if (set == null) {
return null;
}
ResultSet set = statement.executeQuery(); // This will be auto-closed by the try-with-resources

try {
return function.apply(set);
} catch (Exception e) {
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
set.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
});
} catch (SQLException ex) {
ex.printStackTrace();
}

return null;
}

protected void runUpdate(String query, Consumer<PreparedStatement> consumer) {
try (PreparedStatement statement = getConnection().prepareStatement(query)) {
consumer.accept(statement);
statement.executeUpdate();
} catch (Exception ex) {
} catch (SQLException ex) {
ex.printStackTrace();
}
}
Expand All @@ -103,15 +87,12 @@ protected void runUpdate(String query) {
});
}

protected <T> CompletableFuture<T> runQueryAsync(String query, List<StatementObject> list, ResultSetFunction<T> function) {
return associate(() -> runQuery(query, list, function));
}

protected CompletableFuture<Void> runUpdateAsync(String query, Consumer<PreparedStatement> consumer) {
return associate(() -> {
try (PreparedStatement statement = getConnection().prepareStatement(query)) {
consumer.accept(statement);
statement.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
});
return associate(() -> runUpdate(query, consumer));
}

protected CompletableFuture<Void> runUpdateAsync(String query, StatementObject... objects) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import me.illusion.skyblockcore.common.config.section.WritableConfigurationSection;
import me.illusion.skyblockcore.common.database.SkyblockDatabase;
import me.illusion.skyblockcore.common.database.cache.SkyblockCache;
import me.illusion.skyblockcore.common.database.metrics.influx.InfluxMetricsDatabase;
import me.illusion.skyblockcore.common.platform.SkyblockPlatform;
import me.illusion.skyblockcore.common.storage.SkyblockStorage;
import me.illusion.skyblockcore.common.storage.cache.redis.MemorySkyblockIslandCache;
Expand Down Expand Up @@ -55,6 +56,10 @@ private void registerDefaults() {
new RedisSkyblockIslandCache(),
new MemorySkyblockIslandCache()
));

register("metrics", SkyblockDatabaseProvider.of(
new InfluxMetricsDatabase()
));
}

// --- CORE LOGIC ---
Expand Down Expand Up @@ -125,11 +130,10 @@ public CompletableFuture<Void> loadPossible(ConfigurationSection section) {
continue;
}

if(keys.contains(registeredDatabase.getName())) {
if (keys.contains(registeredDatabase.getName())) {
registeredDatabase.setAttemptedLoad(true);
}


temp.add(tryLoad(registeredDatabase));
}

Expand Down
Loading

0 comments on commit 451012b

Please sign in to comment.