2021-06-15 04:59:31 +02:00
|
|
|
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
|
|
|
|
|
2024-09-22 23:28:45 +02:00
|
|
|
Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com>
|
2021-06-15 04:59:31 +02:00
|
|
|
|
|
|
|
diff --git a/src/main/java/io/papermc/paper/datapack/PaperDatapack.java b/src/main/java/io/papermc/paper/datapack/PaperDatapack.java
|
|
|
|
new file mode 100644
|
2024-09-22 23:28:45 +02:00
|
|
|
index 0000000000000000000000000000000000000000..8bd8263b51fb2bb364353565b1ba26b3b0d1d55e
|
2021-06-15 04:59:31 +02:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/main/java/io/papermc/paper/datapack/PaperDatapack.java
|
2024-09-22 23:28:45 +02:00
|
|
|
@@ -0,0 +1,103 @@
|
2021-06-15 04:59:31 +02:00
|
|
|
+package io.papermc.paper.datapack;
|
|
|
|
+
|
2024-09-22 23:28:45 +02:00
|
|
|
+import io.papermc.paper.adventure.PaperAdventure;
|
2021-06-15 04:59:31 +02:00
|
|
|
+import io.papermc.paper.event.server.ServerResourcesReloadedEvent;
|
2024-09-22 23:28:45 +02:00
|
|
|
+import io.papermc.paper.world.flag.PaperFeatureFlagProviderImpl;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.Set;
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
+import net.kyori.adventure.text.Component;
|
2021-06-15 04:59:31 +02:00
|
|
|
+import net.minecraft.server.MinecraftServer;
|
|
|
|
+import net.minecraft.server.packs.repository.Pack;
|
2024-09-22 23:28:45 +02:00
|
|
|
+import net.minecraft.server.packs.repository.PackSource;
|
|
|
|
+import org.bukkit.FeatureFlag;
|
|
|
|
+import org.checkerframework.checker.nullness.qual.NonNull;
|
|
|
|
+import org.checkerframework.checker.nullness.qual.Nullable;
|
|
|
|
+import org.checkerframework.framework.qual.DefaultQualifier;
|
2021-06-15 04:59:31 +02:00
|
|
|
+
|
2024-09-22 23:28:45 +02:00
|
|
|
+@DefaultQualifier(NonNull.class)
|
2021-06-15 04:59:31 +02:00
|
|
|
+public class PaperDatapack implements Datapack {
|
2024-09-22 23:28:45 +02:00
|
|
|
+
|
|
|
|
+ private static final Map<PackSource, DatapackSource> PACK_SOURCES = new ConcurrentHashMap<>();
|
|
|
|
+ static {
|
|
|
|
+ PACK_SOURCES.put(PackSource.DEFAULT, DatapackSource.DEFAULT);
|
|
|
|
+ PACK_SOURCES.put(PackSource.BUILT_IN, DatapackSource.BUILT_IN);
|
|
|
|
+ PACK_SOURCES.put(PackSource.FEATURE, DatapackSource.FEATURE);
|
|
|
|
+ PACK_SOURCES.put(PackSource.WORLD, DatapackSource.WORLD);
|
|
|
|
+ PACK_SOURCES.put(PackSource.SERVER, DatapackSource.SERVER);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private final Pack pack;
|
2021-06-15 04:59:31 +02:00
|
|
|
+ private final boolean enabled;
|
|
|
|
+
|
2024-09-22 23:28:45 +02:00
|
|
|
+ PaperDatapack(final Pack pack, final boolean enabled) {
|
|
|
|
+ this.pack = pack;
|
2021-06-15 04:59:31 +02:00
|
|
|
+ this.enabled = enabled;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String getName() {
|
2024-09-22 23:28:45 +02:00
|
|
|
+ return this.pack.getId();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Component getTitle() {
|
|
|
|
+ return PaperAdventure.asAdventure(this.pack.getTitle());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Component getDescription() {
|
|
|
|
+ return PaperAdventure.asAdventure(this.pack.getDescription());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean isRequired() {
|
|
|
|
+ return this.pack.isRequired();
|
2021-06-15 04:59:31 +02:00
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Compatibility getCompatibility() {
|
2024-09-22 23:28:45 +02:00
|
|
|
+ return Datapack.Compatibility.valueOf(this.pack.getCompatibility().name());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Set<FeatureFlag> getRequiredFeatures() {
|
|
|
|
+ return PaperFeatureFlagProviderImpl.fromNms(this.pack.getRequestedFeatures());
|
2021-06-15 04:59:31 +02:00
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean isEnabled() {
|
2024-09-22 23:28:45 +02:00
|
|
|
+ return this.enabled;
|
2021-06-15 04:59:31 +02:00
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
2024-09-22 23:28:45 +02:00
|
|
|
+ public void setEnabled(final boolean enabled) {
|
|
|
|
+ final MinecraftServer server = MinecraftServer.getServer();
|
|
|
|
+ final List<Pack> enabledPacks = new ArrayList<>(server.getPackRepository().getSelectedPacks());
|
|
|
|
+ final @Nullable Pack packToChange = server.getPackRepository().getPack(this.getName());
|
|
|
|
+ if (packToChange == null) {
|
|
|
|
+ throw new IllegalStateException("Cannot toggle state of pack that doesn't exist: " + this.getName());
|
|
|
|
+ }
|
|
|
|
+ if (enabled == enabledPacks.contains(packToChange)) {
|
2021-06-15 04:59:31 +02:00
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if (enabled) {
|
2024-09-22 23:28:45 +02:00
|
|
|
+ packToChange.getDefaultPosition().insert(enabledPacks, packToChange, Pack::selectionConfig, false); // modeled off the default /datapack enable logic
|
2021-06-15 04:59:31 +02:00
|
|
|
+ } else {
|
2024-09-22 23:28:45 +02:00
|
|
|
+ enabledPacks.remove(packToChange);
|
2021-06-15 04:59:31 +02:00
|
|
|
+ }
|
2024-09-22 23:28:45 +02:00
|
|
|
+ server.reloadResources(enabledPacks.stream().map(Pack::getId).toList(), ServerResourcesReloadedEvent.Cause.PLUGIN);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public DatapackSource getSource() {
|
|
|
|
+ return PACK_SOURCES.computeIfAbsent(this.pack.location().source(), source -> new DatapackSourceImpl(source.toString()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Component computeDisplayName() {
|
|
|
|
+ return PaperAdventure.asAdventure(this.pack.getChatLink(this.enabled));
|
2021-06-15 04:59:31 +02:00
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
diff --git a/src/main/java/io/papermc/paper/datapack/PaperDatapackManager.java b/src/main/java/io/papermc/paper/datapack/PaperDatapackManager.java
|
|
|
|
new file mode 100644
|
2024-09-22 23:28:45 +02:00
|
|
|
index 0000000000000000000000000000000000000000..caa41c525d2b36b5a9f9942380f06c97d5781a93
|
2021-06-15 04:59:31 +02:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/main/java/io/papermc/paper/datapack/PaperDatapackManager.java
|
2024-09-22 23:28:45 +02:00
|
|
|
@@ -0,0 +1,55 @@
|
2021-06-15 04:59:31 +02:00
|
|
|
+package io.papermc.paper.datapack;
|
|
|
|
+
|
2024-09-22 23:28:45 +02:00
|
|
|
+import com.google.common.collect.Collections2;
|
2021-06-15 04:59:31 +02:00
|
|
|
+import java.util.Collection;
|
2024-09-22 23:28:45 +02:00
|
|
|
+import java.util.Collections;
|
|
|
|
+import java.util.function.Predicate;
|
2021-06-15 04:59:31 +02:00
|
|
|
+import net.minecraft.server.packs.repository.Pack;
|
|
|
|
+import net.minecraft.server.packs.repository.PackRepository;
|
2024-09-22 23:28:45 +02:00
|
|
|
+import org.checkerframework.checker.nullness.qual.NonNull;
|
|
|
|
+import org.checkerframework.checker.nullness.qual.Nullable;
|
|
|
|
+import org.checkerframework.framework.qual.DefaultQualifier;
|
2021-06-15 04:59:31 +02:00
|
|
|
+
|
2024-09-22 23:28:45 +02:00
|
|
|
+@DefaultQualifier(NonNull.class)
|
2021-06-15 04:59:31 +02:00
|
|
|
+public class PaperDatapackManager implements DatapackManager {
|
2024-09-22 23:28:45 +02:00
|
|
|
+
|
2021-06-15 04:59:31 +02:00
|
|
|
+ private final PackRepository repository;
|
|
|
|
+
|
2024-09-22 23:28:45 +02:00
|
|
|
+ public PaperDatapackManager(final PackRepository repository) {
|
2021-06-15 04:59:31 +02:00
|
|
|
+ this.repository = repository;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
2024-09-22 23:28:45 +02:00
|
|
|
+ public void refreshPacks() {
|
|
|
|
+ this.repository.reload();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public @Nullable Datapack getPack(final @NonNull String name) {
|
|
|
|
+ final @Nullable Pack pack = this.repository.getPack(name);
|
|
|
|
+ if (pack == null) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ return new PaperDatapack(pack, this.repository.getSelectedPacks().contains(pack));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
2021-06-15 04:59:31 +02:00
|
|
|
+ public Collection<Datapack> getPacks() {
|
2024-09-22 23:28:45 +02:00
|
|
|
+ final Collection<Pack> enabledPacks = this.repository.getSelectedPacks();
|
|
|
|
+ return this.transformPacks(this.repository.getAvailablePacks(), enabledPacks::contains);
|
2021-06-15 04:59:31 +02:00
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Collection<Datapack> getEnabledPacks() {
|
2024-09-22 23:28:45 +02:00
|
|
|
+ return this.transformPacks(this.repository.getSelectedPacks(), pack -> true);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private Collection<Datapack> transformPacks(final Collection<Pack> packs, final Predicate<Pack> enabled) {
|
|
|
|
+ return Collections.unmodifiableCollection(
|
|
|
|
+ Collections2.transform(
|
|
|
|
+ packs,
|
|
|
|
+ pack -> new PaperDatapack(pack, enabled.test(pack))
|
|
|
|
+ )
|
|
|
|
+ );
|
2021-06-15 04:59:31 +02:00
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
2024-11-09 17:01:35 +01:00
|
|
|
index 617eb11d17ef8791a8a8b0fddc19b6a3a8d0af36..10d7186b96038b03de8684c0ad09732a2f82e46f 100644
|
2021-06-15 04:59:31 +02:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
2024-10-23 20:15:25 +02:00
|
|
|
@@ -308,6 +308,7 @@ public final class CraftServer implements Server {
|
2021-06-15 04:59:31 +02:00
|
|
|
private final List<CraftPlayer> playerView;
|
|
|
|
public int reloadCount;
|
2024-06-13 16:45:27 +02:00
|
|
|
public Set<String> activeCompatibilities = Collections.emptySet();
|
2021-06-15 04:59:31 +02:00
|
|
|
+ private final io.papermc.paper.datapack.PaperDatapackManager datapackManager; // Paper
|
|
|
|
public static Exception excessiveVelEx; // Paper - Velocity warnings
|
|
|
|
|
|
|
|
static {
|
2024-10-27 18:11:15 +01:00
|
|
|
@@ -392,6 +393,7 @@ public final class CraftServer implements Server {
|
2022-06-20 19:12:05 +02:00
|
|
|
if (this.configuration.getBoolean("settings.use-map-color-cache")) {
|
|
|
|
MapPalette.setMapColorCache(new CraftMapColorCache(this.logger));
|
|
|
|
}
|
2021-06-15 04:59:31 +02:00
|
|
|
+ datapackManager = new io.papermc.paper.datapack.PaperDatapackManager(console.getPackRepository()); // Paper
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getCommandBlockOverride(String command) {
|
2024-11-09 17:01:35 +01:00
|
|
|
@@ -3036,5 +3038,11 @@ public final class CraftServer implements Server {
|
2021-06-15 04:59:31 +02:00
|
|
|
public com.destroystokyo.paper.entity.ai.MobGoals getMobGoals() {
|
|
|
|
return mobGoals;
|
|
|
|
}
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public io.papermc.paper.datapack.PaperDatapackManager getDatapackManager() {
|
|
|
|
+ return datapackManager;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
// Paper end
|
|
|
|
}
|