Skip to content

Commit

Permalink
Merge branch '1.21.2' into 1.21.4
Browse files Browse the repository at this point in the history
  • Loading branch information
AViewFromTheTop committed Dec 31, 2024
2 parents ce16776 + ea554f1 commit 4ccceb5
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 472 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
min_loader_version=0.16.9

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

Expand Down
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
Expand Up @@ -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) {
Expand All @@ -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());
}

Expand All @@ -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)) {
Expand Down
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
Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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
Expand All @@ -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
Expand Up @@ -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();
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

47 changes: 6 additions & 41 deletions src/main/java/net/frozenblock/lib/gravity/mixin/EntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,65 +17,30 @@

package net.frozenblock.lib.gravity.mixin;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import net.frozenblock.lib.gravity.api.GravityAPI;
import net.frozenblock.lib.gravity.impl.EntityGravityInterface;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Entity.class)
public abstract class EntityMixin implements EntityGravityInterface {
public abstract class EntityMixin {

@Shadow
public float fallDistance;

// TODO: convert to directional
@WrapOperation(method = "getGravity", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;getDefaultGravity()D"))
private double getGravity(Entity instance, Operation<Double> original) {
return original.call(instance) * GravityAPI.calculateGravity(instance).length();
}

@Inject(method = "checkFallDamage", at = @At("TAIL"))
private void frozenLib$checkFallDamage(double y, boolean onGround, BlockState state, BlockPos pos, CallbackInfo info) {
Vec3 gravity = GravityAPI.calculateGravity(Entity.class.cast(this));
double gravityDistance = gravity.length();
this.fallDistance *= (float) gravityDistance;
}

@WrapOperation(
method = "applyGravity",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/phys/Vec3;add(DDD)Lnet/minecraft/world/phys/Vec3;"
)
)
public Vec3 frozenLib$applyGravity(Vec3 instance, double x, double y, double z, Operation<Vec3> original, @Local(ordinal = 0) double originalGravity) {
Vec3 gravityVec = GravityAPI.calculateGravity(Entity.class.cast(this)).scale(originalGravity);
Vec3 directional = new Vec3(x, y + originalGravity, z).subtract(gravityVec);

return original.call(instance, directional.x, directional.y, directional.z);
}

@Unique
@Override
public double frozenLib$getGravity() {
return 0.04D;
this.fallDistance *= (float) GravityAPI.calculateGravity(Entity.class.cast(this));
}

@Unique
@Override
public Vec3 frozenLib$getEffectiveGravity() {
Entity entity = Entity.class.cast(this);
return GravityAPI.calculateGravity(entity).scale(this.frozenLib$getGravity());
@ModifyReturnValue(method = "getGravity", at = @At("RETURN"))
public double frozenLib$modifyGravity(double original) {
return original * GravityAPI.calculateGravity(Entity.class.cast(this));
}
}
Loading

0 comments on commit 4ccceb5

Please sign in to comment.