Skip to content

Commit

Permalink
add plugin module
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Mar 4, 2024
1 parent 4942687 commit 0a17b42
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
13 changes: 13 additions & 0 deletions plugin/plugin-base/pom.xml
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>
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();
}
}
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
}
}
17 changes: 17 additions & 0 deletions plugin/pom.xml
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>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<packaging>pom</packaging>
<modules>
<module>scheduler</module>
<module>plugin</module>
</modules>

<name>MineLib</name>
Expand Down

0 comments on commit 0a17b42

Please sign in to comment.