Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package at.pavlov.internal.registries;

import at.pavlov.internal.Key;

public interface KeyHolder {
Key key();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package at.pavlov.internal.registries;

public class Registries {
}
Original file line number Diff line number Diff line change
@@ -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<T extends KeyHolder> {
private final Map<Key, T> map = new HashMap<>();

public Registry(Supplier<Collection<@NotNull T>> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}

/**
Expand Down Expand Up @@ -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);
}

Expand Down