Skip to content

Commit

Permalink
Include datapacks info in sampler proto
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Sep 5, 2024
1 parent ed33fd5 commit 8986c71
Show file tree
Hide file tree
Showing 33 changed files with 290 additions and 281 deletions.
6 changes: 5 additions & 1 deletion spark-bukkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ plugins {
id 'com.gradleup.shadow' version '8.3.0'
}

java {
disableAutoTargetJvm()
}

dependencies {
implementation project(':spark-common')
implementation 'net.kyori:adventure-platform-bukkit:4.3.3'
compileOnly 'com.destroystokyo.paper:paper-api:1.16.4-R0.1-SNAPSHOT'
compileOnly 'io.papermc.paper:paper-api:1.21.1-R0.1-SNAPSHOT'

// placeholders
compileOnly 'me.clip:placeholderapi:2.10.3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ public Collection<SourceMetadata> getKnownSources() {
Arrays.asList(getServer().getPluginManager().getPlugins()),
Plugin::getName,
plugin -> plugin.getDescription().getVersion(),
plugin -> String.join(", ", plugin.getDescription().getAuthors())
plugin -> String.join(", ", plugin.getDescription().getAuthors()),
plugin -> plugin.getDescription().getDescription()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@
import org.bukkit.entity.EntityType;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Collectors;

public class BukkitWorldInfoProvider implements WorldInfoProvider {
private static final boolean SUPPORTS_PAPER_COUNT_METHODS;
private static final boolean SUPPORTS_GAMERULES;
private static final boolean SUPPORTS_DATAPACKS;

static {
boolean supportsPaperCountMethods = false;
Expand All @@ -59,8 +63,17 @@ public class BukkitWorldInfoProvider implements WorldInfoProvider {
// ignored
}

boolean supportsDataPacks = false;
try {
Server.class.getMethod("getDataPackManager");
supportsDataPacks = true;
} catch (Exception e) {
// ignored
}

SUPPORTS_PAPER_COUNT_METHODS = supportsPaperCountMethods;
SUPPORTS_GAMERULES = supportsGameRules;
SUPPORTS_DATAPACKS = supportsDataPacks;
}

private final Server server;
Expand Down Expand Up @@ -155,6 +168,22 @@ public GameRulesResult pollGameRules() {
return data;
}

@SuppressWarnings("removal")
@Override
public Collection<DataPackInfo> pollDataPacks() {
if (!SUPPORTS_DATAPACKS) {
return null;
}

return this.server.getDataPackManager().getDataPacks().stream()
.map(pack -> new DataPackInfo(
pack.getTitle(),
pack.getDescription(),
pack.getSource().name().toLowerCase(Locale.ROOT).replace("_", "")
))
.collect(Collectors.toList());
}

static final class BukkitChunkInfo extends AbstractChunkInfo<EntityType> {
private final CountMap<EntityType> entityCounts;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public Collection<SourceMetadata> getKnownSources() {
getProxy().getPluginManager().getPlugins(),
plugin -> plugin.getDescription().getName(),
plugin -> plugin.getDescription().getVersion(),
plugin -> plugin.getDescription().getAuthor()
plugin -> plugin.getDescription().getAuthor(),
plugin -> plugin.getDescription().getDescription()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import me.lucko.spark.common.SparkPlatform;
import me.lucko.spark.common.SparkPlugin;

import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -84,6 +85,10 @@ public CompletableFuture<WorldInfoProvider.GameRulesResult> pollGameRules() {
return async(WorldInfoProvider::pollGameRules);
}

public CompletableFuture<Collection<WorldInfoProvider.DataPackInfo>> pollDataPacks() {
return async(WorldInfoProvider::pollDataPacks);
}

public WorldInfoProvider.CountsResult getCounts() {
return get(pollCounts());
}
Expand All @@ -95,4 +100,8 @@ public WorldInfoProvider.ChunksResult<? extends ChunkInfo<?>> getChunks() {
public WorldInfoProvider.GameRulesResult getGameRules() {
return get(pollGameRules());
}

public Collection<WorldInfoProvider.DataPackInfo> getDataPacks() {
return get(pollDataPacks());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package me.lucko.spark.common.platform.world;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -44,6 +45,11 @@ public ChunksResult<? extends ChunkInfo<?>> pollChunks() {
public GameRulesResult pollGameRules() {
return null;
}

@Override
public Collection<DataPackInfo> pollDataPacks() {
return null;
}
};

/**
Expand All @@ -67,6 +73,13 @@ public GameRulesResult pollGameRules() {
*/
GameRulesResult pollGameRules();

/**
* Polls for data packs.
*
* @return the data packs
*/
Collection<DataPackInfo> pollDataPacks();

default boolean mustCallSync() {
return true;
}
Expand Down Expand Up @@ -146,4 +159,28 @@ public Map<String, String> getWorldValues() {
}
}

final class DataPackInfo {
private final String name;
private final String description;
private final String source;

public DataPackInfo(String name, String description, String source) {
this.name = name;
this.description = description;
this.source = source;
}

public String name() {
return this.name;
}

public String description() {
return this.description;
}

public String source() {
return this.source;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import me.lucko.spark.proto.SparkProtos.WorldStatistics;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -80,6 +81,16 @@ public WorldStatistics getWorldStatistics() {
));
}

Collection<WorldInfoProvider.DataPackInfo> dataPacks = this.provider.getDataPacks();
if (dataPacks != null) {
dataPacks.forEach(dataPack -> stats.addDataPacks(WorldStatistics.DataPack.newBuilder()
.setName(dataPack.name())
.setDescription(dataPack.description())
.setSource(dataPack.source())
.build()
));
}

return stats.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@
*/
public class SourceMetadata {

public static <T> List<SourceMetadata> gather(Collection<T> sources, Function<? super T, String> nameFunction, Function<? super T, String> versionFunction, Function<? super T, String> authorFunction) {
public static <T> List<SourceMetadata> gather(Collection<T> sources, Function<? super T, String> name, Function<? super T, String> version, Function<? super T, String> author, Function<? super T, String> description) {
ImmutableList.Builder<SourceMetadata> builder = ImmutableList.builder();

for (T source : sources) {
String name = nameFunction.apply(source);
String version = versionFunction.apply(source);
String author = authorFunction.apply(source);

SourceMetadata metadata = new SourceMetadata(name, version, author);
SourceMetadata metadata = new SourceMetadata(
name.apply(source),
version.apply(source),
author.apply(source),
description.apply(source)
);
builder.add(metadata);
}

Expand All @@ -51,11 +52,13 @@ public static <T> List<SourceMetadata> gather(Collection<T> sources, Function<?
private final String name;
private final String version;
private final String author;
private final String description;

public SourceMetadata(String name, String version, String author) {
public SourceMetadata(String name, String version, String author, String description) {
this.name = name;
this.version = version;
this.author = author;
this.description = description;
}

public String getName() {
Expand All @@ -78,6 +81,9 @@ public PluginOrModMetadata toProto() {
if (this.author != null) {
builder.setAuthor(this.author);
}
if (this.description != null) {
builder.setDescription(this.description);
}
return builder.build();
}

Expand Down
8 changes: 8 additions & 0 deletions spark-common/src/main/proto/spark/spark.proto
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ message WorldStatistics {
map<string, int32> entity_counts = 2;
repeated World worlds = 3;
repeated GameRule game_rules = 4;
repeated DataPack data_packs = 5;

message World {
string name = 1;
Expand All @@ -182,6 +183,12 @@ message WorldStatistics {
string default_value = 2;
map<string, string> world_values = 3;
}

message DataPack {
string name = 1;
string description = 2;
string source = 3;
}
}

message WindowStatistics {
Expand Down Expand Up @@ -227,6 +234,7 @@ message PluginOrModMetadata {
string name = 1;
string version = 2;
string author = 3;
string description = 4;
}

message HealthData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void testBasicLog() {
}

@Test
public void testToProto() {
public void testExport() {
ThreadNode threadNode = new ThreadNode("Test Thread");
threadNode.log(STACK_TRACE_DESCRIBER, STACK_1, TimeUnit.SECONDS.toMicros(1), WINDOW);
threadNode.log(STACK_TRACE_DESCRIBER, STACK_1, TimeUnit.SECONDS.toMicros(1), WINDOW + 1);
Expand Down

This file was deleted.

Loading

0 comments on commit 8986c71

Please sign in to comment.