Skip to content

Commit

Permalink
do not trigger actual collision when checking for collision range
Browse files Browse the repository at this point in the history
  • Loading branch information
btwonion committed Oct 7, 2024
1 parent 111120e commit 4845015
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 15 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"boostOnIce": true, // Toggles, whether a boat, which is on an ice block should be boosted upwards when running against an elevation
"boostOnWater": true, // Toggles, whether a boat, which is on water should be boosted upwards when floating against an elevation
"onlyForPlayers": true, // Toggles, whether a boat should only be boosted when carrying a player,
"extraCollisionDetectionRange": 0.0 // Changes the detection range of a collision. Increasing this will boost a boat x blocks before actually touching the block it approaches. You may encounter weird behaviour when changing this value to big numbers.
"extraCollisionDetectionRange": 0.5 // Changes the detection range of a collision. Increasing this will boost a boat x blocks before actually touching the block it approaches.
}
}
```
Expand All @@ -38,6 +38,4 @@ Please use v1.1.1 or higher to be on the safe side.

### Other

Currently supported versions are: 1.20.1, 1.20.4, 1.20.6 and 1.21. This can change in the future!

If you need help with any of my mods just join my [discord server](https://nyon.dev/discord).
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- add option to change the detection range of collisions
- add option to change the detection range of collisions
- automatically set to 0.5 blocks
21 changes: 18 additions & 3 deletions src/main/java/dev/nyon/bbm/asm/BoatMixin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.nyon.bbm.asm;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import dev.nyon.bbm.BbmBoat;
import dev.nyon.bbm.config.Config;
import dev.nyon.bbm.config.ConfigKt;
import net.minecraft.core.BlockPos;
Expand All @@ -24,11 +25,15 @@
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("AddedMixinMembersNamePattern")
@Mixin(Boat.class)
abstract class BoatMixin extends Entity {
abstract class BoatMixin extends Entity implements BbmBoat {
@Shadow
private Boat.Status status;

@Unique
private boolean jumpCollision = false;

public BoatMixin(
EntityType<?> entityType,
Level level
Expand Down Expand Up @@ -101,12 +106,12 @@ private Vec3 changeMovement(Vec3 original) {
if (carryingBlocks.stream()
.noneMatch(state -> state.is(BlockTags.ICE))) return original;
}
if (!horizontalCollision) return original;
if (!jumpCollision && !horizontalCollision) return original;
}
case IN_WATER -> {
if (!ConfigKt.getActiveConfig()
.getBoostOnWater()) return original;
if (!horizontalCollision) return original;
if (!jumpCollision && !horizontalCollision) return original;
}
case UNDER_WATER, UNDER_FLOWING_WATER -> {
if (!ConfigKt.getActiveConfig()
Expand Down Expand Up @@ -147,4 +152,14 @@ private boolean failsPlayerCondition() {
return getPassengers().stream()
.noneMatch(entity -> entity instanceof Player);
}

@Override
public void setJumpCollision(boolean b) {
jumpCollision = b;
}

@Override
public boolean getJumpCollision() {
return jumpCollision;
}
}
47 changes: 44 additions & 3 deletions src/main/java/dev/nyon/bbm/asm/EntityMixin.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package dev.nyon.bbm.asm;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import dev.nyon.bbm.BbmBoat;
import dev.nyon.bbm.config.Config;
import dev.nyon.bbm.config.ConfigKt;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.vehicle.Boat;
Expand All @@ -10,28 +13,41 @@
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.VoxelShape;
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.ModifyArg;

import java.util.List;

@Mixin(Entity.class)
public class EntityMixin {
public abstract class EntityMixin {
@Shadow
protected abstract Vec3 collide(Vec3 movement);

@Unique
private boolean expandCollision = false;

@Unique
private Entity instance = (Entity) (Object) this;

@ModifyArg(
method = "collide",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/entity/Entity;collideBoundingBox(Lnet/minecraft/world/entity/Entity;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/phys/AABB;Lnet/minecraft/world/level/Level;Ljava/util/List;)Lnet/minecraft/world/phys/Vec3;"
target = "Lnet/minecraft/world/entity/Entity;collideBoundingBox(Lnet/minecraft/world/entity/Entity;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/phys/AABB;Lnet/minecraft/world/level/Level;Ljava/util/List;)Lnet/minecraft/world/phys/Vec3;",
ordinal = 0
),
index = 2
)
private AABB changeCollisionRange(
private AABB changeBoatBox(
Entity entity,
Vec3 movement,
AABB box,
Level world,
List<VoxelShape> shapes
) {
if (!expandCollision) return box;
if (!(entity instanceof Boat boat)) return box;
Config config = ConfigKt.getActiveConfig();
if (config == null) return box;
Expand All @@ -44,4 +60,29 @@ private AABB changeCollisionRange(

return box.inflate(config.getExtraCollisionDetectionRange(), 0, config.getExtraCollisionDetectionRange());
}

@ModifyExpressionValue(
method = "move",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/entity/Entity;collide(Lnet/minecraft/world/phys/Vec3;)Lnet/minecraft/world/phys/Vec3;"
)
)
private Vec3 setBoatCollision(Vec3 movement) {
Vec3 original = collide(movement);
Config config = ConfigKt.getActiveConfig();
if (config == null) return original;
if (config.getExtraCollisionDetectionRange() == 0.0) return original;
if (!(instance instanceof Boat boat && instance instanceof BbmBoat bbmBoat)) return original;
if (!boat.hasControllingPassenger()) return original;

expandCollision = true;
Vec3 withFakeBb = collide(movement);
expandCollision = false;
boolean bl = !Mth.equal(movement.x, withFakeBb.x);
boolean bl2 = !Mth.equal(movement.z, withFakeBb.z);
bbmBoat.setJumpCollision(bl || bl2);

return original;
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/dev/nyon/bbm/BbmBoat.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.nyon.bbm

interface BbmBoat {
var jumpCollision: Boolean
}
8 changes: 4 additions & 4 deletions src/main/kotlin/dev/nyon/bbm/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ data class Config(
var boostOnIce: Boolean = true,
var boostOnWater: Boolean = true,
var onlyForPlayers: Boolean = true,
var extraCollisionDetectionRange: Double = 0.0
var extraCollisionDetectionRange: Double = 0.5
) : CustomPacketPayload {
companion object {
@Transient
Expand Down Expand Up @@ -76,7 +76,7 @@ data class Config(
var boostOnIce: Boolean = true,
var boostOnWater: Boolean = true,
var onlyForPlayers: Boolean = true,
var extraCollisionDetectionRange: Double = 0.0
var extraCollisionDetectionRange: Double = 0.5
) : FabricPacket {
companion object {
@Transient
Expand Down Expand Up @@ -124,7 +124,7 @@ data class Config(
var boostOnIce: Boolean = true,
var boostOnWater: Boolean = true,
var onlyForPlayers: Boolean = true,
var extraCollisionDetectionRange: Double = 0.0
var extraCollisionDetectionRange: Double = 0.5
) : CustomPacketPayload {
companion object {
@Transient
Expand Down Expand Up @@ -166,7 +166,7 @@ data class Config(
var boostOnIce: Boolean = true,
var boostOnWater: Boolean = true,
var onlyForPlayers: Boolean = true,
var extraCollisionDetectionRange: Double = 0.0
var extraCollisionDetectionRange: Double = 0.5
) {
constructor(buf: FriendlyByteBuf) : this(
buf.readFloat(),
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/dev/nyon/bbm/config/ConfigScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fun generateYaclScreen(parent: Screen?): Screen = YetAnotherConfigLib("bbm") {
}

val extraCollisionDetectionRange by rootOptions.registering {
binding(0.0, { config.extraCollisionDetectionRange }, { config.extraCollisionDetectionRange = it })
binding(0.5, { config.extraCollisionDetectionRange }, { config.extraCollisionDetectionRange = it })
controller = numberField(0.0)
descriptionBuilder {
addDefaultText(1)
Expand Down

0 comments on commit 4845015

Please sign in to comment.