Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
G
Grasscutter
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
ziqian zhang
Grasscutter
Commits
d4f9820e
Commit
d4f9820e
authored
3 years ago
by
KingRainbow44
Browse files
Options
Downloads
Patches
Plain Diff
Added getting resources from the JAR
parent
f3061a36
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/emu/grasscutter/plugin/Plugin.java
+28
-2
28 additions, 2 deletions
src/main/java/emu/grasscutter/plugin/Plugin.java
src/main/java/emu/grasscutter/plugin/PluginManager.java
+4
-4
4 additions, 4 deletions
src/main/java/emu/grasscutter/plugin/PluginManager.java
with
32 additions
and
6 deletions
src/main/java/emu/grasscutter/plugin/Plugin.java
+
28
−
2
View file @
d4f9820e
package
emu.grasscutter.plugin
;
import
emu.grasscutter.Grasscutter
;
import
emu.grasscutter.plugin.api.ServerHook
;
import
emu.grasscutter.server.game.GameServer
;
import
java.io.InputStream
;
import
java.net.URLClassLoader
;
/**
* The base class for all plugins to extend.
*/
public
abstract
class
Plugin
{
private
final
ServerHook
server
=
ServerHook
.
getInstance
();
private
PluginIdentifier
identifier
;
private
URLClassLoader
classLoader
;
/**
* This method is reflected into.
...
...
@@ -15,9 +22,11 @@ public abstract class Plugin {
* Set plugin variables.
* @param identifier The plugin's identifier.
*/
private
void
initializePlugin
(
PluginIdentifier
identifier
)
{
private
void
initializePlugin
(
PluginIdentifier
identifier
,
URLClassLoader
classLoader
)
{
if
(
this
.
identifier
==
null
)
this
.
identifier
=
identifier
;
if
(
this
.
classLoader
==
null
)
this
.
classLoader
=
classLoader
;
else
Grasscutter
.
getLogger
().
warn
(
this
.
identifier
.
name
+
" had a reinitialization attempt."
);
}
...
...
@@ -55,7 +64,24 @@ public abstract class Plugin {
* @return A server instance.
*/
public
final
GameServer
getServer
()
{
return
Grasscutter
.
getGameServer
();
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
);
}
/**
* Returns the server hook.
* @return A server hook singleton.
*/
public
final
ServerHook
getHandle
()
{
return
this
.
server
;
}
/* Called when the plugin is first loaded. */
...
...
This diff is collapsed.
Click to expand it.
src/main/java/emu/grasscutter/plugin/PluginManager.java
+
4
−
4
View file @
d4f9820e
...
...
@@ -73,7 +73,7 @@ public final class PluginManager {
Class
<?>
pluginClass
=
loader
.
loadClass
(
pluginConfig
.
mainClass
);
Plugin
pluginInstance
=
(
Plugin
)
pluginClass
.
getDeclaredConstructor
().
newInstance
();
this
.
loadPlugin
(
pluginInstance
,
PluginIdentifier
.
fromPluginConfig
(
pluginConfig
));
this
.
loadPlugin
(
pluginInstance
,
PluginIdentifier
.
fromPluginConfig
(
pluginConfig
)
,
loader
);
fileReader
.
close
();
// Close the file reader.
}
catch
(
ClassNotFoundException
ignored
)
{
...
...
@@ -89,14 +89,14 @@ public final class PluginManager {
* Load the specified plugin.
* @param plugin The plugin instance.
*/
private
void
loadPlugin
(
Plugin
plugin
,
PluginIdentifier
identifier
)
{
private
void
loadPlugin
(
Plugin
plugin
,
PluginIdentifier
identifier
,
URLClassLoader
classLoader
)
{
Grasscutter
.
getLogger
().
info
(
"Loading plugin: "
+
identifier
.
name
);
// Add the plugin's identifier.
try
{
Class
<
Plugin
>
pluginClass
=
Plugin
.
class
;
Method
method
=
pluginClass
.
getDeclaredMethod
(
"initializePlugin"
,
PluginIdentifier
.
class
);
method
.
setAccessible
(
true
);
method
.
invoke
(
plugin
,
identifier
);
method
.
setAccessible
(
false
);
Method
method
=
pluginClass
.
getDeclaredMethod
(
"initializePlugin"
,
PluginIdentifier
.
class
,
URLClassLoader
.
class
);
method
.
setAccessible
(
true
);
method
.
invoke
(
plugin
,
identifier
,
classLoader
);
method
.
setAccessible
(
false
);
}
catch
(
Exception
ignored
)
{
Grasscutter
.
getLogger
().
warn
(
"Failed to add plugin identifier: "
+
identifier
.
name
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment