Paper/patches/server/0524-Add-basic-Datapack-API.patch

210 lines
7.9 KiB
Diff
Raw Permalink Normal View History

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
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
index 0000000000000000000000000000000000000000..8bd8263b51fb2bb364353565b1ba26b3b0d1d55e
2021-06-15 04:59:31 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/datapack/PaperDatapack.java
@@ -0,0 +1,103 @@
2021-06-15 04:59:31 +02:00
+package io.papermc.paper.datapack;
+
+import io.papermc.paper.adventure.PaperAdventure;
2021-06-15 04:59:31 +02:00
+import io.papermc.paper.event.server.ServerResourcesReloadedEvent;
+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;
+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
+
+@DefaultQualifier(NonNull.class)
2021-06-15 04:59:31 +02:00
+public class PaperDatapack implements Datapack {
+
+ 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;
+
+ 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() {
+ 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() {
+ 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() {
+ return this.enabled;
2021-06-15 04:59:31 +02:00
+ }
+
+ @Override
+ 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) {
+ packToChange.getDefaultPosition().insert(enabledPacks, packToChange, Pack::selectionConfig, false); // modeled off the default /datapack enable logic
2021-06-15 04:59:31 +02:00
+ } else {
+ enabledPacks.remove(packToChange);
2021-06-15 04:59:31 +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
index 0000000000000000000000000000000000000000..caa41c525d2b36b5a9f9942380f06c97d5781a93
2021-06-15 04:59:31 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/datapack/PaperDatapackManager.java
@@ -0,0 +1,55 @@
2021-06-15 04:59:31 +02:00
+package io.papermc.paper.datapack;
+
+import com.google.common.collect.Collections2;
2021-06-15 04:59:31 +02:00
+import java.util.Collection;
+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;
+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
+
+@DefaultQualifier(NonNull.class)
2021-06-15 04:59:31 +02:00
+public class PaperDatapackManager implements DatapackManager {
+
2021-06-15 04:59:31 +02:00
+ private final PackRepository repository;
+
+ public PaperDatapackManager(final PackRepository repository) {
2021-06-15 04:59:31 +02:00
+ this.repository = repository;
+ }
+
+ @Override
+ 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() {
+ 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() {
+ 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
index 3cfacacd1d8fd17ec4b54936afd8124823b1a00b..b4af066a21e4893b5ec146d109b5146b6996a0cf 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;
Update upstream (Bukkit/CraftBukkit/Spigot) (#10875) Upstream has released updates that appear to apply and compile correctly. This update has not been tested by PaperMC and as with ANY update, please do your own testing Bukkit Changes: 376e37db SPIGOT-7677: Update which entities are marked as spawnable 06c4add3 SPIGOT-7737: Add separate TreeType.MEGA_PINE 19b7caaa SPIGOT-7731: Spawn eggs cannot have damage e585297e PR-1022: Add force option to Player#spawnParticle d26e0094 PR-1018: Add methods to get players seeing specific chunks 8df1ed18 PR-978: Add Material#isCompostable and Material#getCompostChance 4b9b59c7 SPIGOT-7676: Enforce locale parameter in toLowerCase and toUpperCase method calls and always use root locale 8d1e700a PR-1020: Cast instead of using #typed when getting BlockType and ItemType to better work with testing / mocks fa28607a PR-1016: Fix incorrect assumption of Fireball having constant speed 4c6c8586 PR-1015: Add a tool component to ItemMeta 6f6b2123 PR-1014: Add PotionEffectTypeCategory to distinguish between beneficial and harmful effects f511cfe1 PR-1013, SPIGOT-4288, SPIGOT-6202: Add material rerouting in preparation for the switch to ItemType and BlockType def44cbf SPIGOT-7669: Fix typo in ProjectileHitEvent#getHitBlockFace documentation 53fa4f72 PR-1011: Throw an exception if a RecipeChoice is ever supplied air CraftBukkit Changes: ee95e171a SPIGOT-7737: Add separate TreeType.MEGA_PINE 0dae4c62c Fix spawn egg equality check and copy constructor ab59e847c Fix spawn eggs with no entity creating invalid stacks and disconnect creative clients 3b6093b28 SPIGOT-7736: Creative spawn egg use loses components c6b4d5a87 SPIGOT-7731: Spawn eggs cannot have damage 340ccd57f SPIGOT-7735: Fix serialization of player heads with note block sound fd2f41834 SPIGOT-7734: Can't register a custom advancement using unsafe() 02456e2a5 PR-1413: Add force option to Player#spawnParticle 6a61f38b2 SPIGOT-7680: Per-world weather command 58c41cebb PR-1409: Add methods to get players seeing specific chunks 16c976797 PR-1412: Fix shipwreck loot tables not being set for BlockTransformers 7189ba636 PR-1360: Add Material#isCompostable and Material#getCompostChance 900384556 SPIGOT-7676: Enforce locale parameter in toLowerCase and toUpperCase method calls and always use root locale bdb40c5f1 Increase outdated build delay d6607c7dd SPIGOT-7675: Fix FoodComponent config deserialization b148ed332 PR-1406: Fix incorrect assumption of Fireball having constant speed 3ec31ca75 PR-1405: Add a tool component to ItemMeta 5d7d675b9 PR-1404: Add PotionEffectTypeCategory to distinguish between beneficial and harmful effects 960827981 PR-1403, SPIGOT-4288, SPIGOT-6202: Add material rerouting in preparation for the switch to ItemType and BlockType 94e44ec93 PR-1401: Add a config option to accept old keys in registry get calls a43701920 PR-1402: Fix ChunkSnapshot#isSectionEmpty() is always false 87d0a3368 SPIGOT-7668: Move NONE Registry updater to FieldRename to avoid some class loader issues 2ea1e7ac2 PR-1399: Fix regression preventing positive .setDamage value from causing knockback for 0 damage events ba2d49d21 Increase outdated build delay Spigot Changes: fcd94e21 Rebuild patches 342f4939 SPIGOT-7661: Add experimental unload-frozen-chunks option
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 {
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) {
@@ -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
}