Skip to content

Commit

Permalink
Add Hooks (#6)
Browse files Browse the repository at this point in the history
* 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
Intybyte authored Dec 4, 2024
1 parent 62defdd commit fdf0628
Show file tree
Hide file tree
Showing 28 changed files with 1,045 additions and 204 deletions.
13 changes: 9 additions & 4 deletions FEATURES.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
New features/fixes:
---------------
- 1.20.6 + Support
- Upgraded to Java 14 language level
- Requires Java 21
- Requires Java 17
- Added Netherite and newer armor protection support
- Simplified Chinese Translation by [SnowCutieOwO](https://github.com/SnowCutieOwO)
- New area commands

Hooks:
---------------
- Vault hook to buy cannons (was there even before fork)
- Movecraft-Cannons support is now integrated
- PlaceholderAPI hook

Optimizations:
---------------
- Better FlyingProjectile lookup
- UserMessage Optimization
- Some CannonManager Optimization
- Random Optimization (Original created a random number generator every time it needed to be used, now each object has its own Random)
- RNG Optimization (Original created a random number generator every time it needed to be used, now each object has its own Random)
- Distance optimization by using `Location#distanceSquared()` over `Location#distance` when possible
- Aiming shot simulation Optimization
- Simulating aim is less resource expensive (after testing, this feature requires about 50% less CPU usage)
- `CannonAPI#getCannon` should now not create massive lag when there are a lot of designs
- `CannonAPI#getCannon` should now not create massive lag when there are a lot of designs, and is way faster (some owners stated it was up to x6 faster)
- `/cannons claim` and commands executed in a radius won't deadlock your server anymore, and it is executed on a separate thread

API Changes/New Events:
Expand Down
18 changes: 18 additions & 0 deletions api-internal/pom.xml
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>
16 changes: 16 additions & 0 deletions api-internal/src/main/java/at/pavlov/internal/Hook.java
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 api-internal/src/main/java/at/pavlov/internal/HookManager.java
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();
}
}
}
55 changes: 42 additions & 13 deletions cannons-bukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,18 @@
<id>enginehub-maven</id>
<url>https://maven.enginehub.org/repo/</url>
</repository>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
<repository>
<id>fawe-repo</id>
<url>https://ci.athion.net/job/FastAsyncWorldEdit-1.13/ws/mvn</url>
</repository>
<repository>
<id>CodeMC</id>
<url>https://repo.codemc.org/repository/maven-public</url>
</repository>
<repository>
<id>placeholderapi</id>
<url>https://repo.extendedclip.com/releases/</url>
</repository>
<repository>
<id>vault-repo</id>
<url>https://jitpack.io</url>
Expand All @@ -63,9 +59,24 @@
<id>aikar</id>
<url>https://repo.aikar.co/content/groups/aikar/</url>
</repository>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/apdevteam/movecraft</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>at.pavlov</groupId>
<artifactId>api-internal</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down Expand Up @@ -106,6 +117,24 @@
<version>1.18.32</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.countercraft</groupId>
<artifactId>movecraft</artifactId>
<version>8.0.0_beta-5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.countercraft.movecraft.combat</groupId>
<artifactId>movecraft-combat</artifactId>
<version>2.0.0_beta-6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.11.6</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -132,8 +161,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>21</source>
<target>21</target>
<source>${java.source}</source>
<target>${java.target}</target>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -164,15 +193,15 @@
<relocations>
<relocation>
<pattern>org.bstats</pattern>
<shadedPattern>at.pavlov.cannons</shadedPattern>
<shadedPattern>at.pavlov.cannons.shaded</shadedPattern>
</relocation>
<relocation>
<pattern>co.aikar.commands</pattern>
<shadedPattern>me.vaan.acf</shadedPattern> <!-- Replace this -->
<shadedPattern>at.pavlov.cannons.shaded.acf</shadedPattern>
</relocation>
<relocation>
<pattern>co.aikar.locales</pattern>
<shadedPattern>me.vaan.locales</shadedPattern> <!-- Replace this -->
<shadedPattern>at.pavlov.cannons.shaded.locales</shadedPattern>
</relocation>
</relocations>
</configuration>
Expand Down
47 changes: 47 additions & 0 deletions cannons-bukkit/src/main/java/at/pavlov/cannons/API/CannonsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
import at.pavlov.cannons.Enum.MessageEnum;
import at.pavlov.cannons.cannon.Cannon;
import at.pavlov.cannons.cannon.CannonManager;
import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.craft.Craft;
import net.countercraft.movecraft.craft.PilotedCraft;
import net.countercraft.movecraft.craft.SubCraft;
import org.bukkit.Location;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

public class CannonsAPI {
Expand Down Expand Up @@ -114,6 +120,47 @@ public HashSet<Cannon> getCannons(List<Location> locations, UUID playerUID)
return plugin.getCannonManager().getCannons(locations, playerUID, true);
}

/**
* In case you want to use Movecraft + Cannons
* use this method to get all the cannons that are present on a craft
*
* @param craft Movecraft craft to scan for cannons
* @return cannons presents on craft
*/
public Set<Cannon> getCannons(Craft craft) {
List<Location> shipLocations = new ArrayList<>();
for (MovecraftLocation loc : craft.getHitBox()) {
shipLocations.add(loc.toBukkit(craft.getWorld()));
}
return this.getCannons(shipLocations, getPlayerFromCraft(craft), true);
}

/**
* This method tries to get the player that is piloting the craft, or if the craft
* is a subcraft, the pilot of the parent craft.
*
* @param craft Movecraft craft to search for its pilot
* @return UUID of the pilot
*/
public UUID getPlayerFromCraft(Craft craft) {
if (craft instanceof PilotedCraft pilotedCraft) {
// If this is a piloted craft, return the pilot's UUID
return pilotedCraft.getPilot().getUniqueId();
}

if (craft instanceof SubCraft subCraft) {
// If this is a subcraft, look for a parent
Craft parent = subCraft.getParent();
if (parent != null) {
// If the parent is not null, recursively check it for a UUID
return getPlayerFromCraft(parent);
}
}

// Return null if all else fails
return null;
}

/**
* returns the cannon from the storage
* @param uid UUID of the cannon
Expand Down
14 changes: 13 additions & 1 deletion cannons-bukkit/src/main/java/at/pavlov/cannons/Aiming.java
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,19 @@ public Cannon getCannonInAimingMode(Player player) {
if (player == null)
return null;
//return the cannon of the player if he is in aiming mode
return CannonManager.getCannon(inAimingMode.get(player.getUniqueId()));
return getCannonInAimingMode(player.getUniqueId());
}
/**
* returns the cannon of the player if he is in aiming mode
*
* @param player the player who is in aiming mode
* @return the cannon which is in aiming mode by the given player
*/
public Cannon getCannonInAimingMode(UUID player) {
if (player == null)
return null;
//return the cannon of the player if he is in aiming mode
return CannonManager.getCannon(inAimingMode.get(player));
}


Expand Down
Loading

0 comments on commit fdf0628

Please sign in to comment.