-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.github.projectunified</groupId> | ||
<artifactId>minelib-plugin</artifactId> | ||
<version>1.1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>minelib-plugin-base</artifactId> | ||
</project> |
79 changes: 79 additions & 0 deletions
79
...in/plugin-base/src/main/java/io/github/projectunified/minelib/plugin/base/BasePlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package io.github.projectunified.minelib.plugin.base; | ||
|
||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Base plugin class that provides a simple way to manage components | ||
*/ | ||
public class BasePlugin extends JavaPlugin implements Loadable { | ||
private final Map<Class<?>, Object> components; | ||
|
||
/** | ||
* Create a new plugin instance | ||
*/ | ||
public BasePlugin() { | ||
components = getComponents() | ||
.stream() | ||
.collect(Collectors.toMap(Object::getClass, o -> o)); | ||
} | ||
|
||
/** | ||
* Get the components that should be managed by the plugin. | ||
* Plugins should override this method to provide their own components. | ||
* | ||
* @return a list of components | ||
*/ | ||
protected List<Object> getComponents() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
/** | ||
* Get a component by its class. | ||
* | ||
* @param type the class of the component | ||
* @param <T> the type of the component | ||
* @return the component | ||
*/ | ||
public final <T> T get(Class<T> type) { | ||
return type.cast(components.get(type)); | ||
} | ||
|
||
/** | ||
* Call a consumer for each component of a given type. | ||
* | ||
* @param type the class of the component | ||
* @param consumer the consumer to call | ||
* @param <T> the type of the component | ||
*/ | ||
public final <T> void call(Class<T> type, Consumer<T> consumer) { | ||
for (Object component : components.values()) { | ||
if (type.isInstance(component)) { | ||
consumer.accept(type.cast(component)); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public final void onLoad() { | ||
this.call(Loadable.class, Loadable::load); | ||
this.load(); | ||
} | ||
|
||
@Override | ||
public final void onEnable() { | ||
this.call(Loadable.class, Loadable::enable); | ||
this.enable(); | ||
} | ||
|
||
@Override | ||
public final void onDisable() { | ||
this.call(Loadable.class, Loadable::disable); | ||
this.disable(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
plugin/plugin-base/src/main/java/io/github/projectunified/minelib/plugin/base/Loadable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.github.projectunified.minelib.plugin.base; | ||
|
||
/** | ||
* Interface for loadable components | ||
*/ | ||
public interface Loadable { | ||
/** | ||
* Called when the component is loaded | ||
*/ | ||
default void load() { | ||
// EMPTY | ||
} | ||
|
||
/** | ||
* Called when the component is enabled | ||
*/ | ||
default void enable() { | ||
// EMPTY | ||
} | ||
|
||
/** | ||
* Called when the component is disabled | ||
*/ | ||
default void disable() { | ||
// EMPTY | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.github.projectunified</groupId> | ||
<artifactId>minelib</artifactId> | ||
<version>1.1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>minelib-plugin</artifactId> | ||
<packaging>pom</packaging> | ||
<modules> | ||
<module>plugin-base</module> | ||
</modules> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters