Skip to content

Commit

Permalink
Merge branch 'master' into 1.21.2
Browse files Browse the repository at this point in the history
  • Loading branch information
AViewFromTheTop committed Dec 31, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 98e79dd + b8a8509 commit ea554f1
Showing 18 changed files with 33 additions and 639 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
min_loader_version=0.16.8

# Mod Properties
mod_version = 1.9.8
mod_version = 1.9.9
maven_group = net.frozenblock
archives_base_name = FrozenLib

35 changes: 10 additions & 25 deletions src/main/java/net/frozenblock/lib/gravity/api/GravityAPI.java
Original file line number Diff line number Diff line change
@@ -28,20 +28,15 @@
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;

public final class GravityAPI {
private GravityAPI() {}

public static final Vec3 DEFAULT_GRAVITY = new Vec3(0.0, 1.0, 0.0);

public static final Event<GravityModification> MODIFICATIONS = FrozenEvents.createEnvironmentEvent(GravityModification.class, callbacks -> context -> {
for (GravityModification callback : callbacks) {
callback.modifyGravity(context);
}
});

public static final double DEFAULT_GRAVITY = 0.45D;
public static final Event<GravityModification> MODIFICATIONS = FrozenEvents.createEnvironmentEvent(GravityModification.class, callbacks -> context -> {
for (GravityModification callback : callbacks) {
callback.modifyGravity(context);
}
});
private static final Map<ResourceKey<Level>, List<GravityBelt<?>>> GRAVITY_BELTS = new HashMap<>();

public static void register(ResourceKey<Level> dimension, GravityBelt<?> gravityBelt) {
@@ -53,7 +48,7 @@ public static List<GravityBelt<?>> getAllBelts(ResourceKey<Level> dimension) {
return GRAVITY_BELTS.computeIfAbsent(dimension, dimension1 -> new ArrayList<>());
}

public static List<GravityBelt<?>> getAllBelts(Level level) {
public static @NotNull List<GravityBelt<?>> getAllBelts(@NotNull Level level) {
return getAllBelts(level.dimension());
}

@@ -69,25 +64,15 @@ public static List<GravityBelt<?>> getAllBelts(Level level) {
});
}

public static Vec3 calculateGravity(ResourceKey<Level> dimension, double y) {
GravityContext context = new GravityContext(dimension, y, null);
MODIFICATIONS.invoker().modifyGravity(context);
return context.gravity;
}

public static Vec3 calculateGravity(Level level, double y) {
return calculateGravity(level.dimension(), y);
}

public static Vec3 calculateGravity(Entity entity) {
public static double calculateGravity(@NotNull Entity entity) {
ResourceKey<Level> dimension = entity.level().dimension();
double y = entity.getY();
GravityContext context = new GravityContext(dimension, y, entity);
GravityContext context = new GravityContext(dimension, y, entity, entity.getInBlockState());
MODIFICATIONS.invoker().modifyGravity(context);
return context.gravity;
}

public static Optional<GravityBelt<?>> getAffectingGravityBelt(List<GravityBelt<?>> belts, double y) {
public static Optional<GravityBelt<?>> getAffectingGravityBelt(@NotNull List<GravityBelt<?>> belts, double y) {
Optional<GravityBelt<?>> optionalGravityBelt = Optional.empty();
for (GravityBelt<?> belt : belts) {
if (belt.affectsPosition(y)) {
12 changes: 4 additions & 8 deletions src/main/java/net/frozenblock/lib/gravity/api/GravityBelt.java
Original file line number Diff line number Diff line change
@@ -20,20 +20,16 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public record GravityBelt<T extends GravityFunction>(double minY, double maxY, boolean renderBottom, boolean renderTop,
T function) {
public GravityBelt(double minY, double maxY, T function) {
this(minY, maxY, false, false, function);
}
public record GravityBelt<T extends GravityFunction>(double minY, double maxY, T function) {

public boolean affectsPosition(double y) {
return y >= minY && y < maxY;
}

public Vec3 getGravity(@Nullable Entity entity, double y) {
public double getGravity(@Nullable Entity entity, double y) {
if (this.affectsPosition(y)) {
return this.function.get(entity, y, this.minY, this.maxY);
}
@@ -51,7 +47,7 @@ public static <T extends SerializableGravityFunction<T>> Codec<GravityBelt<T>> c
}

@Nullable
public static <T extends SerializableGravityFunction<T>> Codec<GravityBelt<T>> codec(T gravityFunction) {
public static <T extends SerializableGravityFunction<T>> Codec<GravityBelt<T>> codec(@NotNull T gravityFunction) {
Codec<T> codec = gravityFunction.codec();
if (codec == null) return null;
return codec(codec);
Original file line number Diff line number Diff line change
@@ -20,31 +20,29 @@
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;

public class GravityContext {

public final ResourceKey<Level> dimension;

public final double y;

/**
* A mutable property that will determine the outputting gravity
*/
public Vec3 gravity;

public double gravity;
@Nullable
public final Entity entity;
public final BlockState state;

public GravityContext(ResourceKey<Level> dimension, double y, @Nullable Entity entity) {
this(dimension, y, GravityAPI.DEFAULT_GRAVITY, entity);
public GravityContext(ResourceKey<Level> dimension, double y, @Nullable Entity entity, BlockState state) {
this(dimension, y, GravityAPI.DEFAULT_GRAVITY, entity, state);
}

public GravityContext(ResourceKey<Level> dimension, double y, Vec3 gravity, @Nullable Entity entity) {
public GravityContext(ResourceKey<Level> dimension, double y, double gravity, @Nullable Entity entity, BlockState state) {
this.dimension = dimension;
this.y = y;
this.gravity = gravity;
this.entity = entity;
this.state = state;
}
}
Original file line number Diff line number Diff line change
@@ -18,7 +18,6 @@
package net.frozenblock.lib.gravity.api;

import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Nullable;

@FunctionalInterface
@@ -30,5 +29,5 @@ public interface GravityFunction {
* @param maxY The maximum Y position of the gravity belt
* @return The gravity value
*/
Vec3 get(@Nullable Entity entity, double y, double minY, double maxY);
double get(@Nullable Entity entity, double y, double minY, double maxY);
}
Original file line number Diff line number Diff line change
@@ -25,18 +25,17 @@
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Nullable;

public record AbsoluteGravityFunction(Vec3 gravity) implements SerializableGravityFunction<AbsoluteGravityFunction> {

public record AbsoluteGravityFunction(double gravity) implements SerializableGravityFunction<AbsoluteGravityFunction> {
public static final Codec<AbsoluteGravityFunction> CODEC = RecordCodecBuilder.create(instance ->
instance.group(
Vec3.CODEC.fieldOf("gravity").forGetter(AbsoluteGravityFunction::gravity)
Codec.DOUBLE.fieldOf("gravity").forGetter(AbsoluteGravityFunction::gravity)
).apply(instance, AbsoluteGravityFunction::new)
);

public static final Codec<GravityBelt<AbsoluteGravityFunction>> BELT_CODEC = GravityBelt.codec(CODEC);

@Override
public Vec3 get(@Nullable Entity entity, double y, double minY, double maxY) {
public double get(@Nullable Entity entity, double y, double minY, double maxY) {
return gravity();
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit ea554f1

Please sign in to comment.