Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Hooks #6

Merged
merged 39 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
f416920
Create hooks + VaultHook
Intybyte Nov 27, 2024
731a6b2
Fix dependency
Intybyte Nov 27, 2024
e09c2d1
Yk maybe don't shade the whole thing
Intybyte Nov 27, 2024
64ba188
Add methods to CannonsAPI
Intybyte Nov 27, 2024
1495492
Add MaxCannons handling
Intybyte Nov 27, 2024
0fb22d6
Add Listeners
Intybyte Nov 27, 2024
973dc67
ProjectileImpactDamage class
Intybyte Nov 27, 2024
986c9cd
Add Hook config
Intybyte Nov 27, 2024
9b84813
Java 17
Intybyte Nov 30, 2024
473207d
Fix hook warnings
Intybyte Nov 30, 2024
8814fae
Java 17 refactor
Intybyte Nov 30, 2024
ec85200
Small change
Intybyte Nov 30, 2024
c1a3912
Fix error and fire it only once
Intybyte Nov 30, 2024
919bda4
Hooks again
Intybyte Nov 30, 2024
e4ed66a
Warning and disable if already using Movecraft-Cannons
Intybyte Nov 30, 2024
3c7871f
Cleanup Relocations
Intybyte Nov 30, 2024
07482b3
Add PlaceholderAPIHook
Intybyte Nov 30, 2024
3128ec0
Hook fix again
Intybyte Nov 30, 2024
382c352
Cleanup PlaceholderAPIHook
Intybyte Nov 30, 2024
89d9916
Fix hook working method
Intybyte Nov 30, 2024
70ff567
Fix DirectHit not getting called
Intybyte Nov 30, 2024
bf97d07
Unused import
Intybyte Nov 30, 2024
27f410c
Add hook custom chart
Intybyte Dec 1, 2024
650746c
Register MaxCannonsProperty
Intybyte Dec 1, 2024
fd721d2
Fix event priority
Intybyte Dec 1, 2024
f814b8c
Add None to AdvancedPie
Intybyte Dec 1, 2024
4a344c6
Active is more fitting
Intybyte Dec 1, 2024
aba2c60
Fixup README.md
Intybyte Dec 1, 2024
50d863b
Oh it was already there
Intybyte Dec 1, 2024
97aca0a
Move movecraft to hook package
Intybyte Dec 3, 2024
a426fc1
Cleanup configs
Intybyte Dec 3, 2024
b0fc7dd
Rename key to avoid conflicts
Intybyte Dec 3, 2024
64e8232
Extract MovecraftCombatHook
Intybyte Dec 3, 2024
e2efea4
Add enable message
Intybyte Dec 4, 2024
017f834
Show enabled message
Intybyte Dec 4, 2024
7be515f
Use simple name
Intybyte Dec 4, 2024
e4dcb3b
Green color
Intybyte Dec 4, 2024
bead9ce
Fix damn movecraft being petty
Intybyte Dec 4, 2024
da45985
Cleanup Cannon
Intybyte Dec 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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