aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0596-Add-basic-Datapack-API.patch
blob: f18457ab30bc0c2eb2db41c37952b9bd28013da3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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/datapack/PaperDatapack.java b/src/main/java/io/papermc/paper/datapack/PaperDatapack.java
new file mode 100644
index 0000000000000000000000000000000000000000..9b7dd8a0fba4547f5268b3f99e21ddbe6b5bf566
--- /dev/null
+++ b/src/main/java/io/papermc/paper/datapack/PaperDatapack.java
@@ -0,0 +1,50 @@
+package io.papermc.paper.datapack;
+
+import io.papermc.paper.event.server.ServerResourcesReloadedEvent;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.packs.repository.Pack;
+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(Pack loader, boolean enabled) {
+        this.name = loader.getId();
+        this.compatibility = Compatibility.valueOf(loader.getCompatibility().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.getPackRepository().getSelectedPacks().stream().map(Pack::getId).collect(Collectors.toList());
+        if (enabled) {
+            enabledKeys.add(this.name);
+        } else {
+            enabledKeys.remove(this.name);
+        }
+        server.reloadResources(enabledKeys, ServerResourcesReloadedEvent.Cause.PLUGIN);
+    }
+}
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..cf4374493c11057451a62a655514415cf6b298e0
--- /dev/null
+++ b/src/main/java/io/papermc/paper/datapack/PaperDatapackManager.java
@@ -0,0 +1,25 @@
+package io.papermc.paper.datapack;
+
+import java.util.Collection;
+import java.util.stream.Collectors;
+import net.minecraft.server.packs.repository.Pack;
+import net.minecraft.server.packs.repository.PackRepository;
+
+public class PaperDatapackManager implements DatapackManager {
+    private final PackRepository repository;
+
+    public PaperDatapackManager(PackRepository repository) {
+        this.repository = repository;
+    }
+
+    @Override
+    public Collection<Datapack> getPacks() {
+        Collection<Pack> enabledPacks = repository.getSelectedPacks();
+        return repository.getAvailablePacks().stream().map(loader -> new PaperDatapack(loader, enabledPacks.contains(loader))).collect(Collectors.toList());
+    }
+
+    @Override
+    public Collection<Datapack> getEnabledPacks() {
+        return repository.getSelectedPacks().stream().map(loader -> new PaperDatapack(loader, true)).collect(Collectors.toList());
+    }
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index e57af8c8f4a1ddfcf9689c8d721b2e2dc887f0be..23a433f28a7185c86c129deec32b606079c13f9c 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -303,6 +303,7 @@ public final class CraftServer implements Server {
     public boolean ignoreVanillaPermissions = false;
     private final List<CraftPlayer> playerView;
     public int reloadCount;
+    private final io.papermc.paper.datapack.PaperDatapackManager datapackManager; // Paper
     public static Exception excessiveVelEx; // Paper - Velocity warnings
 
     static {
@@ -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));
         }
+        datapackManager = new io.papermc.paper.datapack.PaperDatapackManager(console.getPackRepository()); // Paper
     }
 
     public boolean getCommandBlockOverride(String command) {
@@ -2850,5 +2852,11 @@ public final class CraftServer implements Server {
     public com.destroystokyo.paper.entity.ai.MobGoals getMobGoals() {
         return mobGoals;
     }
+
+    @Override
+    public io.papermc.paper.datapack.PaperDatapackManager getDatapackManager() {
+        return datapackManager;
+    }
+
     // Paper end
 }