From 5d39091321d4deddbc85b626881be0fad5314c3e Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 18 Jun 2025 19:45:53 +0200 Subject: [PATCH 1/2] Create registries and handling --- .../pavlov/internal/registries/KeyHolder.java | 7 +++ .../internal/registries/Registries.java | 4 ++ .../pavlov/internal/registries/Registry.java | 49 +++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 api-internal/src/main/java/at/pavlov/internal/registries/KeyHolder.java create mode 100644 api-internal/src/main/java/at/pavlov/internal/registries/Registries.java create mode 100644 api-internal/src/main/java/at/pavlov/internal/registries/Registry.java diff --git a/api-internal/src/main/java/at/pavlov/internal/registries/KeyHolder.java b/api-internal/src/main/java/at/pavlov/internal/registries/KeyHolder.java new file mode 100644 index 00000000..aafbc688 --- /dev/null +++ b/api-internal/src/main/java/at/pavlov/internal/registries/KeyHolder.java @@ -0,0 +1,7 @@ +package at.pavlov.internal.registries; + +import at.pavlov.internal.Key; + +public interface KeyHolder { + Key key(); +} diff --git a/api-internal/src/main/java/at/pavlov/internal/registries/Registries.java b/api-internal/src/main/java/at/pavlov/internal/registries/Registries.java new file mode 100644 index 00000000..58f16b67 --- /dev/null +++ b/api-internal/src/main/java/at/pavlov/internal/registries/Registries.java @@ -0,0 +1,4 @@ +package at.pavlov.internal.registries; + +public class Registries { +} diff --git a/api-internal/src/main/java/at/pavlov/internal/registries/Registry.java b/api-internal/src/main/java/at/pavlov/internal/registries/Registry.java new file mode 100644 index 00000000..4b4967f9 --- /dev/null +++ b/api-internal/src/main/java/at/pavlov/internal/registries/Registry.java @@ -0,0 +1,49 @@ +package at.pavlov.internal.registries; + +import at.pavlov.internal.Key; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Supplier; + +public class Registry { + private final Map map = new HashMap<>(); + + public Registry(Supplier> args) { + Collection<@NotNull T> arguments = args.get(); + for (T arg : arguments) { + register(arg); + } + } + + public @Nullable T of(@Nullable Key key) { + if (key == null) { + return null; + } + + return map.get(key); + } + + public @Nullable T of(@Nullable String string) { + if (string == null) { + return null; + } + + return of(Key.from(string)); + } + + @SafeVarargs + public final void register(@NotNull T... entries) { + for (T entry : entries) { + map.put(entry.key(), entry); + } + } + + public void register(Supplier<@NotNull T> entry) { + T result = entry.get(); + map.put(result.key(), result); + } +} From e6a6ee732d257d6d8597b594c0fed65be00db58d Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 18 Jun 2025 20:18:54 +0200 Subject: [PATCH 2/2] Add getConstantAccelerationPower --- .../cannons/container/MovingObject.java | 50 ++++++++++++++++--- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/cannons-bukkit/src/main/java/at/pavlov/cannons/container/MovingObject.java b/cannons-bukkit/src/main/java/at/pavlov/cannons/container/MovingObject.java index abcd75b8..fb8beacb 100644 --- a/cannons-bukkit/src/main/java/at/pavlov/cannons/container/MovingObject.java +++ b/cannons-bukkit/src/main/java/at/pavlov/cannons/container/MovingObject.java @@ -32,12 +32,36 @@ public MovingObject(Location loc, Vector vel, Key entityType) { public void updateProjectileLocation(boolean inWater) { double drag = getDrag(inWater); double gravity = getGravity(); - //update location + + // 1. Move based on current velocity this.loc.add(this.vel); - //slow down projectile + + // 2. Apply drag to velocity this.vel.multiply(drag); - //apply gravity + + // 3. Apply gravity this.vel.subtract(new Vector(0, gravity, 0)); + + // 4. Apply constant acceleration if present + Double accelerationPower = getConstantAccelerationPower(); + if (accelerationPower != null) { + Vector acceleration = vel.clone().normalize().multiply(accelerationPower); + this.vel.add(acceleration); + } + } + + /** + * Returns the constant acceleration power for projectiles that use it (e.g., fireballs). + * Returns null if the entity does not use constant acceleration. + * + * @return the acceleration power (e.g., 0.1) or null if not applicable + */ + public Double getConstantAccelerationPower() { + return switch (entityType.full()) { + case "minecraft:fireball", "minecraft:small_fireball", "minecraft:dragon_fireball", + "minecraft:wither_skull", "minecraft:shulker_bullet" -> 0.1; // vanilla default + default -> null; + }; } /** @@ -91,18 +115,28 @@ public double getDrag(boolean inWater) { } /** - * reverts and update of the projectile position + * Reverts an update of the projectile position. * - * @param inWater the projectile is in water + * @param inWater true if the projectile is in water */ public void revertProjectileLocation(boolean inWater) { double drag = getDrag(inWater); double gravity = getGravity(); - //apply gravity + + // 1. Revert constant acceleration if present + Double accelerationPower = getConstantAccelerationPower(); + if (accelerationPower != null) { + Vector acceleration = vel.clone().normalize().multiply(accelerationPower); + this.vel.subtract(acceleration); + } + + // 2. Revert gravity this.vel.add(new Vector(0, gravity, 0)); - //slow down projectile + + // 3. Revert drag this.vel.multiply(1.0 / drag); - //update location + + // 4. Revert movement this.loc.subtract(this.vel); }