aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/removed/1.19.3-paper-plugins/api/0367-Update-Folder-Uses-Plugin-Name.patch
blob: 823069413c20a4c3fa78d0f3a4972e9b4089d39c (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
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Xemorr <31805746+Xemorr@users.noreply.github.com>
Date: Fri, 1 Apr 2022 19:57:40 +0100
Subject: [PATCH] Update Folder Uses Plugin Name


diff --git a/src/main/java/org/bukkit/plugin/SimplePluginManager.java b/src/main/java/org/bukkit/plugin/SimplePluginManager.java
index 758faf990ba96cbcd0203e9184bcad234b4cb728..fb19ab3bd929045b4d3e227ed961a24b16b8b2bd 100644
--- a/src/main/java/org/bukkit/plugin/SimplePluginManager.java
+++ b/src/main/java/org/bukkit/plugin/SimplePluginManager.java
@@ -412,7 +412,7 @@ public final class SimplePluginManager implements PluginManager {
         }
         // Paper end
 
-        checkUpdate(file);
+        file = checkUpdate(file); // Paper - update the reference in case checkUpdate renamed it
 
         Set<Pattern> filters = fileAssociations.keySet();
         Plugin result = null;
@@ -439,16 +439,61 @@ public final class SimplePluginManager implements PluginManager {
         return result;
     }
 
-    private void checkUpdate(@NotNull File file) {
+    // Paper start - Update Folder Uses Plugin Name to replace
+    /**
+     * Replaces a plugin with a plugin of the same plugin name in the update folder.
+     * @param file
+     * @throws InvalidPluginException
+     */
+    private File checkUpdate(@NotNull File file) throws InvalidPluginException {
         if (updateDirectory == null || !updateDirectory.isDirectory()) {
-            return;
+            return file;
         }
+        PluginLoader pluginLoader = getPluginLoader(file);
+        try {
+            String pluginName = pluginLoader.getPluginDescription(file).getName();
+            for (File updateFile : updateDirectory.listFiles()) {
+                if (!updateFile.isFile()) continue;
+                PluginLoader updatePluginLoader = getPluginLoader(updateFile);
+                if (updatePluginLoader == null) continue;
+                String updatePluginName;
+                try {
+                     updatePluginName = updatePluginLoader.getPluginDescription(updateFile).getName();
+                     // We failed to load this data for some reason, so, we'll skip over this
+                } catch (InvalidDescriptionException ex) {
+                    continue;
+                }
+                if (!pluginName.equals(updatePluginName)) continue;
+                try {
+                    java.nio.file.Files.copy(updateFile.toPath(), file.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
+                } catch (java.io.IOException exception) {
+                    server.getLogger().log(Level.SEVERE, "Could not copy '" + updateFile.getPath() + "' to '" + file.getPath() + "' in update plugin process", exception);
+                    continue;
+                }
+                File newName = new File(file.getParentFile(), updateFile.getName());
+                file.renameTo(newName);
+                updateFile.delete();
+                return newName;
+            }
+        }
+        catch (InvalidDescriptionException e) {
+            throw new InvalidPluginException(e);
+        }
+        return file;
+    }
 
-        File updateFile = new File(updateDirectory, file.getName());
-        if (updateFile.isFile() && FileUtil.copy(updateFile, file)) {
-            updateFile.delete();
+    @Nullable
+    private PluginLoader getPluginLoader(File file) {
+        Set<Pattern> filters = fileAssociations.keySet();
+        for (Pattern filter : filters) {
+            Matcher match = filter.matcher(file.getName());
+            if (match.find()) {
+                return fileAssociations.get(filter);
+            }
         }
+        return null;
     }
+    // Paper end
 
     /**
      * Checks if the given plugin is loaded and returns it when applicable