Plugin.java 3 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
3
package emu.grasscutter.plugin;

import emu.grasscutter.Grasscutter;
4
import emu.grasscutter.plugin.api.ServerHook;
KingRainbow44's avatar
KingRainbow44 committed
5
6
import emu.grasscutter.server.game.GameServer;

KingRainbow44's avatar
KingRainbow44 committed
7
import java.io.File;
8
9
10
import java.io.InputStream;
import java.net.URLClassLoader;

11
12
import static emu.grasscutter.Configuration.*;

KingRainbow44's avatar
KingRainbow44 committed
13
14
15
16
/**
 * The base class for all plugins to extend.
 */
public abstract class Plugin {
17
18
    private final ServerHook server = ServerHook.getInstance();
    
KingRainbow44's avatar
KingRainbow44 committed
19
    private PluginIdentifier identifier;
20
    private URLClassLoader classLoader;
KingRainbow44's avatar
KingRainbow44 committed
21
    private File dataFolder;
KingRainbow44's avatar
KingRainbow44 committed
22
23

    /**
KingRainbow44's avatar
KingRainbow44 committed
24
25
26
     * This method is reflected into.
     * 
     * Set plugin variables.
KingRainbow44's avatar
KingRainbow44 committed
27
28
     * @param identifier The plugin's identifier.
     */
29
    private void initializePlugin(PluginIdentifier identifier, URLClassLoader classLoader) {
KingRainbow44's avatar
KingRainbow44 committed
30
31
32
33
34
35
36
        if(this.identifier != null) {
            Grasscutter.getLogger().warn(this.identifier.name + " had a reinitialization attempt.");
            return;
        }
        
        this.identifier = identifier;
        this.classLoader = classLoader;
37
        this.dataFolder = new File(PLUGINS_FOLDER, identifier.name);
KingRainbow44's avatar
KingRainbow44 committed
38
39
40
41
42
        
        if(!this.dataFolder.exists() && !this.dataFolder.mkdirs()) {
            Grasscutter.getLogger().warn("Failed to create plugin data folder for " + this.identifier.name);
            return;
        }
KingRainbow44's avatar
KingRainbow44 committed
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
    }

    /**
     * The plugin's identifier instance.
     * @return An instance of {@link PluginIdentifier}.
     */
    public final PluginIdentifier getIdentifier(){
        return this.identifier;
    }

    /**
     * Get the plugin's name.
     */
    public final String getName() {
        return this.identifier.name;
    }

    /**
     * Get the plugin's description.
     */
    public final String getDescription() {
        return this.identifier.description;
    }

    /**
     * Get the plugin's version.
     */
    public final String getVersion() {
        return this.identifier.version;
    }

    /**
     * Returns the server that initialized the plugin.
     * @return A server instance.
     */
    public final GameServer getServer() {
79
80
81
82
83
84
85
86
87
88
89
90
        return this.server.getGameServer();
    }

    /**
     * Returns an input stream for a resource in the JAR file.
     * @param resourceName The name of the resource.
     * @return An input stream.
     */
    public final InputStream getResource(String resourceName) {
        return this.classLoader.getResourceAsStream(resourceName);
    }

KingRainbow44's avatar
KingRainbow44 committed
91
92
93
94
95
96
97
98
    /**
     * Returns a directory where plugins can store data files.
     * @return A directory on the file system.
     */
    public final File getDataFolder() {
        return this.dataFolder;
    }

99
100
101
102
103
104
    /**
     * Returns the server hook.
     * @return A server hook singleton.
     */
    public final ServerHook getHandle() {
        return this.server;
KingRainbow44's avatar
KingRainbow44 committed
105
106
107
108
109
110
111
112
113
    }
    
    /* Called when the plugin is first loaded. */
    public void onLoad() { }
    /* Called after (most of) the server enables. */
    public void onEnable() { }
    /* Called before the server disables. */
    public void onDisable() { }
}