forked from DerPavlov/Cannons
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create hooks + VaultHook * Add methods to CannonsAPI * Add MaxCannons handling * Add Hook config * Java 17 * Warning and disable if already using Movecraft-Cannons * Add PlaceholderAPIHook * Add hook custom chart * Fix event priority * Extract MovecraftCombatHook
- Loading branch information
Showing
28 changed files
with
1,045 additions
and
204 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
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,18 @@ | ||
<?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>at.pavlov</groupId> | ||
<artifactId>cannons</artifactId> | ||
<version>1.0.0</version> | ||
</parent> | ||
|
||
<artifactId>api-internal</artifactId> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package at.pavlov.internal; | ||
|
||
public interface Hook<H> { | ||
H hook(); | ||
default boolean active() { | ||
return hook() != null; | ||
} | ||
|
||
void onEnable(); | ||
void onDisable(); | ||
Class<? extends Hook<?>> getTypeClass(); | ||
|
||
default String enabledMessage() { | ||
return getTypeClass().getSimpleName() + " Enabled."; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
api-internal/src/main/java/at/pavlov/internal/HookManager.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,66 @@ | ||
package at.pavlov.internal; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class HookManager { | ||
private final Map<Class<? extends Hook<?>>, Hook<?>> hooks = new HashMap<>(); | ||
|
||
/** | ||
* @return A defensive copy of the hook map | ||
*/ | ||
public Map<Class<? extends Hook<?>>, Hook<?>> hookMap() { | ||
return Collections.unmodifiableMap(hooks); | ||
} | ||
|
||
public boolean isRegistered(Class<? extends Hook<?>> type) { | ||
if (this.hooks.containsKey(type)) { | ||
return true; | ||
} | ||
|
||
for (Class<? extends Hook<?>> clazz : hooks.keySet()) { | ||
if (type.isAssignableFrom(clazz)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public <T extends Hook<?>> T getHook(Class<T> type) { | ||
Hook<?> hook = this.hooks.get(type); | ||
if (hook != null) { | ||
return type.cast(hook); | ||
} | ||
|
||
for (Class<? extends Hook<?>> clazz : hooks.keySet()) { | ||
if (type.isAssignableFrom(clazz)) { | ||
hook = hooks.get(clazz); | ||
} | ||
} | ||
|
||
if (hook == null) { | ||
throw new IllegalArgumentException("No registered hook of type " + type.getName() + "!"); | ||
} | ||
return type.cast(hook); | ||
} | ||
|
||
/** | ||
* @return true if at least one hook in the manager is working | ||
*/ | ||
public boolean isActive() { | ||
return hooks.values().stream().anyMatch(Hook::active); | ||
} | ||
|
||
public void registerHook(Hook<?> hook) { | ||
hook.onEnable(); | ||
this.hooks.put(hook.getTypeClass(), hook); | ||
} | ||
|
||
public void disableHooks() { | ||
for (Hook<?> hook : hooks.values()) { | ||
hook.onDisable(); | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.