Add basic Datapack API (#5653) (#5653)

This commit is contained in:
Connor Linfoot 2021-05-16 21:09:46 +01:00 committed by GitHub
parent 99c1d9da6d
commit c639a52a69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 281 additions and 0 deletions

View File

@ -0,0 +1,106 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Connor Linfoot <connorlinfoot@me.com>
Date: Sun, 16 May 2021 15:07:34 +0100
Subject: [PATCH] Add basic Datapack API
diff --git a/src/main/java/io/papermc/paper/resource/Datapack.java b/src/main/java/io/papermc/paper/resource/Datapack.java
new file mode 100644
index 0000000000000000000000000000000000000000..5233057bf1ee10da28d7c3b9c36665616650b1ea
--- /dev/null
+++ b/src/main/java/io/papermc/paper/resource/Datapack.java
@@ -0,0 +1,32 @@
+package io.papermc.paper.resource;
+
+import org.checkerframework.checker.nullness.qual.NonNull;
+
+public interface Datapack {
+
+ /**
+ * @return the name of the pack
+ */
+ @NonNull
+ String getName();
+
+ /**
+ * @return the compatibility of the pack
+ */
+ @NonNull
+ Compatibility getCompatibility();
+
+ /**
+ * @return whether or not the pack is currently enabled
+ */
+ boolean isEnabled();
+
+ void setEnabled(boolean enabled);
+
+ enum Compatibility {
+ TOO_OLD,
+ TOO_NEW,
+ COMPATIBLE,
+ }
+
+}
diff --git a/src/main/java/io/papermc/paper/resource/DatapackManager.java b/src/main/java/io/papermc/paper/resource/DatapackManager.java
new file mode 100644
index 0000000000000000000000000000000000000000..b404e1a0d0afbf6297cc0c2623696fac26bcbc08
--- /dev/null
+++ b/src/main/java/io/papermc/paper/resource/DatapackManager.java
@@ -0,0 +1,21 @@
+package io.papermc.paper.resource;
+
+import org.checkerframework.checker.nullness.qual.NonNull;
+
+import java.util.Collection;
+
+public interface DatapackManager {
+
+ /**
+ * @return all the packs known to the server
+ */
+ @NonNull
+ Collection<Datapack> getPacks();
+
+ /**
+ * @return all the packs which are currently enabled
+ */
+ @NonNull
+ Collection<Datapack> getEnabledPacks();
+
+}
diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
index 5f7208196684d9c8373df28b7cfb5f9e21baa41e..bd834cd9b1d05327da195b76719e90ac14943dbb 100644
--- a/src/main/java/org/bukkit/Bukkit.java
+++ b/src/main/java/org/bukkit/Bukkit.java
@@ -1937,6 +1937,14 @@ public final class Bukkit {
public static com.destroystokyo.paper.entity.ai.MobGoals getMobGoals() {
return server.getMobGoals();
}
+
+ /**
+ * @return the resource pack manager
+ */
+ @NotNull
+ public static io.papermc.paper.resource.DatapackManager getDatapackManager() {
+ return server.getDatapackManager();
+ }
// Paper end
@NotNull
diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java
index f3e27d2d02a9407bb1b091b8c1125ad5abf99e55..5f2b9a91b03a3e8b2dc5ce7743e31bea882500e4 100644
--- a/src/main/java/org/bukkit/Server.java
+++ b/src/main/java/org/bukkit/Server.java
@@ -1698,5 +1698,11 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi
*/
@NotNull
com.destroystokyo.paper.entity.ai.MobGoals getMobGoals();
+
+ /**
+ * @return the resource pack manager
+ */
+ @NotNull
+ io.papermc.paper.resource.DatapackManager getDatapackManager();
// Paper end
}

View File

@ -0,0 +1,175 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Connor Linfoot <connorlinfoot@me.com>
Date: Sun, 16 May 2021 15:07:34 +0100
Subject: [PATCH] Add basic Datapack API
diff --git a/src/main/java/io/papermc/paper/resource/PaperDatapack.java b/src/main/java/io/papermc/paper/resource/PaperDatapack.java
new file mode 100644
index 0000000000000000000000000000000000000000..3108efce8657db84329eceae43a10532f3d1f8d9
--- /dev/null
+++ b/src/main/java/io/papermc/paper/resource/PaperDatapack.java
@@ -0,0 +1,51 @@
+package io.papermc.paper.resource;
+
+import io.papermc.paper.event.server.ServerResourcesReloadedEvent;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.packs.repository.ResourcePackLoader;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class PaperDatapack implements Datapack {
+ private final String name;
+ private final Compatibility compatibility;
+ private final boolean enabled;
+
+ PaperDatapack(ResourcePackLoader loader, boolean enabled) {
+ this.name = loader.getName();
+ this.compatibility = Compatibility.valueOf(loader.getVersion().name());
+ this.enabled = enabled;
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public Compatibility getCompatibility() {
+ return compatibility;
+ }
+
+ @Override
+ public boolean isEnabled() {
+ return enabled;
+ }
+
+ @Override
+ public void setEnabled(boolean enabled) {
+ if (enabled == this.enabled) {
+ return;
+ }
+
+ MinecraftServer server = MinecraftServer.getServer();
+ List<String> enabledKeys = server.getResourcePackRepository().getEnabledPacks().stream().map(ResourcePackLoader::getName).collect(Collectors.toList());
+ if (enabled) {
+ enabledKeys.add(this.name);
+ } else {
+ enabledKeys.remove(this.name);
+ }
+ server.reloadServerResources(enabledKeys, ServerResourcesReloadedEvent.Cause.PLUGIN);
+ }
+}
diff --git a/src/main/java/io/papermc/paper/resource/PaperDatapackManager.java b/src/main/java/io/papermc/paper/resource/PaperDatapackManager.java
new file mode 100644
index 0000000000000000000000000000000000000000..5c4858dfc5a416c88c1dedca5d59ee72dd8aa0ca
--- /dev/null
+++ b/src/main/java/io/papermc/paper/resource/PaperDatapackManager.java
@@ -0,0 +1,26 @@
+package io.papermc.paper.resource;
+
+import net.minecraft.server.packs.repository.ResourcePackLoader;
+import net.minecraft.server.packs.repository.ResourcePackRepository;
+
+import java.util.Collection;
+import java.util.stream.Collectors;
+
+public class PaperDatapackManager implements DatapackManager {
+ private final ResourcePackRepository repository;
+
+ public PaperDatapackManager(ResourcePackRepository repository) {
+ this.repository = repository;
+ }
+
+ @Override
+ public Collection<Datapack> getPacks() {
+ Collection<ResourcePackLoader> enabledPacks = repository.getEnabledPacks();
+ return repository.getPacks().stream().map(loader -> new PaperDatapack(loader, enabledPacks.contains(loader))).collect(Collectors.toList());
+ }
+
+ @Override
+ public Collection<Datapack> getEnabledPacks() {
+ return repository.getEnabledPacks().stream().map(loader -> new PaperDatapack(loader, true)).collect(Collectors.toList());
+ }
+}
diff --git a/src/main/java/net/minecraft/server/packs/repository/ResourcePackLoader.java b/src/main/java/net/minecraft/server/packs/repository/ResourcePackLoader.java
index 5c038c02955d8517db0686f3b73c8573d3770466..01c4fda33a138b2d1f6b7c1335378445423a66a9 100644
--- a/src/main/java/net/minecraft/server/packs/repository/ResourcePackLoader.java
+++ b/src/main/java/net/minecraft/server/packs/repository/ResourcePackLoader.java
@@ -102,6 +102,7 @@ public class ResourcePackLoader implements AutoCloseable {
});
}
+ public final EnumResourcePackVersion getVersion() { return this.c(); } // Paper - OBFHELPER
public EnumResourcePackVersion c() {
return this.g;
}
@@ -110,6 +111,7 @@ public class ResourcePackLoader implements AutoCloseable {
return (IResourcePack) this.d.get();
}
+ public final String getName() { return this.e(); } // Paper - OBFHELPER
public String e() {
return this.c;
}
diff --git a/src/main/java/net/minecraft/server/packs/repository/ResourcePackRepository.java b/src/main/java/net/minecraft/server/packs/repository/ResourcePackRepository.java
index e87523612d0423d71eab7b9af851c1c268cdf84f..568da9686c41a41e43ede3fe15e0ca53c9688f8b 100644
--- a/src/main/java/net/minecraft/server/packs/repository/ResourcePackRepository.java
+++ b/src/main/java/net/minecraft/server/packs/repository/ResourcePackRepository.java
@@ -88,6 +88,7 @@ public class ResourcePackRepository implements AutoCloseable {
return this.b.keySet();
}
+ public final Collection<ResourcePackLoader> getPacks() { return this.c(); } // Paper - OBFHELPER
public Collection<ResourcePackLoader> c() {
return this.b.values();
}
@@ -96,6 +97,7 @@ public class ResourcePackRepository implements AutoCloseable {
return (Collection) this.c.stream().map(ResourcePackLoader::e).collect(ImmutableSet.toImmutableSet());
}
+ public final Collection<ResourcePackLoader> getEnabledPacks() { return this.e(); } // Paper - OBFHELPER
public Collection<ResourcePackLoader> e() {
return this.c;
}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index 6cc8eb04f42592aa12f76bb4a0a863ea509741b2..554d13d65952af1b98db074ebc4a2d90474029f6 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -18,6 +18,7 @@ import com.mojang.serialization.Lifecycle;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.buffer.Unpooled;
+import io.papermc.paper.resource.PaperDatapackManager; // Paper
import io.papermc.paper.util.TraceUtil;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import java.awt.image.BufferedImage;
@@ -268,6 +269,7 @@ public final class CraftServer implements Server {
public boolean ignoreVanillaPermissions = false;
private final List<CraftPlayer> playerView;
public int reloadCount;
+ private final PaperDatapackManager datapackManager; // Paper
public static Exception excessiveVelEx; // Paper - Velocity warnings
static {
@@ -350,6 +352,7 @@ public final class CraftServer implements Server {
TicketType.PLUGIN.loadPeriod = Math.min(20, configuration.getInt("chunk-gc.period-in-ticks")); // Paper - cap plugin loads to 1 second
minimumAPI = configuration.getString("settings.minimum-api");
loadIcon();
+ datapackManager = new PaperDatapackManager(console.getResourcePackRepository()); // Paper
}
public boolean getCommandBlockOverride(String command) {
@@ -2498,5 +2501,11 @@ public final class CraftServer implements Server {
public com.destroystokyo.paper.entity.ai.MobGoals getMobGoals() {
return mobGoals;
}
+
+ @Override
+ public PaperDatapackManager getDatapackManager() {
+ return datapackManager;
+ }
+
// Paper end
}