Plugin.java 3.29 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
import emu.grasscutter.server.game.GameServer;
KingRainbow44's avatar
KingRainbow44 committed
6
7
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
KingRainbow44's avatar
KingRainbow44 committed
8

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

13
14
import static emu.grasscutter.Configuration.*;

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

    /**
KingRainbow44's avatar
KingRainbow44 committed
27
28
29
     * This method is reflected into.
     * 
     * Set plugin variables.
KingRainbow44's avatar
KingRainbow44 committed
30
31
     * @param identifier The plugin's identifier.
     */
32
    private void initializePlugin(PluginIdentifier identifier, URLClassLoader classLoader) {
KingRainbow44's avatar
KingRainbow44 committed
33
34
35
36
37
38
39
        if(this.identifier != null) {
            Grasscutter.getLogger().warn(this.identifier.name + " had a reinitialization attempt.");
            return;
        }
        
        this.identifier = identifier;
        this.classLoader = classLoader;
40
        this.dataFolder = new File(PLUGIN(), identifier.name);
KingRainbow44's avatar
KingRainbow44 committed
41
        this.logger = LoggerFactory.getLogger(identifier.name);
KingRainbow44's avatar
KingRainbow44 committed
42
43
44
45
46
        
        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
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
    }

    /**
     * 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() {
83
84
85
86
87
88
89
90
91
92
93
94
        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
95
96
97
98
99
100
101
102
    /**
     * Returns a directory where plugins can store data files.
     * @return A directory on the file system.
     */
    public final File getDataFolder() {
        return this.dataFolder;
    }

103
104
105
106
107
108
    /**
     * Returns the server hook.
     * @return A server hook singleton.
     */
    public final ServerHook getHandle() {
        return this.server;
KingRainbow44's avatar
KingRainbow44 committed
109
    }
KingRainbow44's avatar
KingRainbow44 committed
110
111
112
113
114
115
116
117

    /**
     * Returns the plugin's logger.
     * @return A SLF4J logger.
     */
    public final Logger getLogger() {
        return this.logger;
    }
KingRainbow44's avatar
KingRainbow44 committed
118
119
120
121
122
123
124
125
    
    /* 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() { }
}