Skip to content

Commit

Permalink
add option to modify detection range for collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
btwonion committed Oct 6, 2024
1 parent 277ac1e commit 111120e
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 21 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@

```json5
{
"version": 3, // just ignore that, only for migrations
"version": 4, // just ignore that, only for migrations
"config": {
"stepHeight": 0.35, // The amount of blocks you are going to be boosted when triggering a boost
"playerEjectTicks": 200.0, // The ticks the game waits before kicking you out of a boat after the player lost control
"boostUnderwater": true, // Toggles, whether a boat, which is underwater should be boosted upwards
"boostOnBlocks": false, // Toggles, whether a boat, which is on a block should be boosted upwards when running against an elevation
"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
"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.
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- update version range to also accept snapshots and higher versions without an update
- add option to change the detection range of collisions
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ mod.id=bbm
mod.name=BetterBoatMovement
mod.description=Increases boat step height to move up water and blocks
mod.beta=0
mod.major-version=2.2.3
mod.major-version=2.2.4
mod.mixins=bbm.mixins.json
mod.supported-loaders=fabric,quilt,neoforge,forge
47 changes: 47 additions & 0 deletions src/main/java/dev/nyon/bbm/asm/EntityMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package dev.nyon.bbm.asm;

import dev.nyon.bbm.config.Config;
import dev.nyon.bbm.config.ConfigKt;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.vehicle.Boat;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;

import java.util.List;

@Mixin(Entity.class)
public class EntityMixin {
@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;"
),
index = 2
)
private AABB changeCollisionRange(
Entity entity,
Vec3 movement,
AABB box,
Level world,
List<VoxelShape> shapes
) {
if (!(entity instanceof Boat boat)) return box;
Config config = ConfigKt.getActiveConfig();
if (config == null) return box;

if (boat.getPassengers()
.stream()
.filter(passenger -> passenger instanceof Player)
.toList()
.isEmpty() && config.getOnlyForPlayers()) return box;

return box.inflate(config.getExtraCollisionDetectionRange(), 0, config.getExtraCollisionDetectionRange());
}
}
8 changes: 4 additions & 4 deletions src/main/kotlin/dev/nyon/bbm/BetterBoatMovementEntrypoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ object BetterBoatMovementEntrypoint {
/^? if >=1.20.5 {^/
MOD_BUS.addListener<RegisterPayloadHandlersEvent> { event ->
val registrar = event.registrar("3")
val registrar = event.registrar("4")
registrar.playToClient(Config.packetType, Config.codec, DirectionalPayloadHandler(
{ config, _ ->
serverConfig = config
Expand All @@ -92,7 +92,7 @@ object BetterBoatMovementEntrypoint {
}
/^?} else {^/
/^MOD_BUS.addListener<RegisterPayloadHandlerEvent> { event ->
val registrar = event.registrar("3")
val registrar = event.registrar("4")
registrar.play(Config.identifier, FriendlyByteBuf.Reader{ buf -> Config(buf) }) { handler ->
handler.client { config, _ -> serverConfig = config }.server { _, _ -> }
}
Expand Down Expand Up @@ -145,7 +145,7 @@ object BetterBoatMovementEntrypoint {
init {
instantiateConfig(FMLLoader.getGamePath().resolve("config/better-boat-movement.json"))
val channel = NetworkRegistry.newSimpleChannel(resourceLocation("better-boat-movement:channel"), { "3" }, { true }, { true })
val channel = NetworkRegistry.newSimpleChannel(resourceLocation("better-boat-movement:channel"), { "4" }, { true }, { true })
channel.registerMessage(
0,
Config::class.java,
Expand Down Expand Up @@ -190,7 +190,7 @@ object BetterBoatMovementEntrypoint {
private fun instantiateConfig(path: Path) {
config(
path,
3,
4,
Config()
) { element, version -> migrate(element, version) }
config = loadConfig()
Expand Down
38 changes: 30 additions & 8 deletions src/main/kotlin/dev/nyon/bbm/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ data class Config(
var boostOnBlocks: Boolean = true,
var boostOnIce: Boolean = true,
var boostOnWater: Boolean = true,
var onlyForPlayers: Boolean = true
var onlyForPlayers: Boolean = true,
var extraCollisionDetectionRange: Double = 0.0
) : CustomPacketPayload {
companion object {
@Transient
Expand All @@ -37,7 +38,8 @@ data class Config(
buf.readBoolean(),
buf.readBoolean(),
buf.readBoolean(),
buf.readBoolean()
buf.readBoolean(),
buf.readDouble()
)
}

Expand All @@ -52,6 +54,7 @@ data class Config(
buf.writeBoolean(config.boostOnIce)
buf.writeBoolean(config.boostOnWater)
buf.writeBoolean(config.onlyForPlayers)
buf.writeDouble(config.extraCollisionDetectionRange)
}
}
}
Expand All @@ -72,7 +75,8 @@ data class Config(
var boostOnBlocks: Boolean = true,
var boostOnIce: Boolean = true,
var boostOnWater: Boolean = true,
var onlyForPlayers: Boolean = true
var onlyForPlayers: Boolean = true,
var extraCollisionDetectionRange: Double = 0.0
) : FabricPacket {
companion object {
@Transient
Expand All @@ -86,7 +90,8 @@ data class Config(
buffer.readBoolean(),
buffer.readBoolean(),
buffer.readBoolean(),
buffer.readBoolean()
buffer.readBoolean(),
buffer.readDouble()
)
}
}
Expand All @@ -99,6 +104,7 @@ data class Config(
buf.writeBoolean(boostOnIce)
buf.writeBoolean(boostOnWater)
buf.writeBoolean(onlyForPlayers)
buf.writeDouble(config.extraCollisionDetectionRange)
}
override fun getType(): PacketType<*> {
Expand All @@ -117,7 +123,8 @@ data class Config(
var boostOnBlocks: Boolean = true,
var boostOnIce: Boolean = true,
var boostOnWater: Boolean = true,
var onlyForPlayers: Boolean = true
var onlyForPlayers: Boolean = true,
var extraCollisionDetectionRange: Double = 0.0
) : CustomPacketPayload {
companion object {
@Transient
Expand All @@ -131,7 +138,8 @@ data class Config(
buf.readBoolean(),
buf.readBoolean(),
buf.readBoolean(),
buf.readBoolean()
buf.readBoolean(),
buf.readDouble()
)
override fun write(buf: FriendlyByteBuf) {
Expand All @@ -142,6 +150,7 @@ data class Config(
buf.writeBoolean(config.boostOnIce)
buf.writeBoolean(config.boostOnWater)
buf.writeBoolean(config.onlyForPlayers)
buf.writeDouble(config.extraCollisionDetectionRange)
}
override fun id(): ResourceLocation {
Expand All @@ -156,7 +165,8 @@ data class Config(
var boostOnBlocks: Boolean = true,
var boostOnIce: Boolean = true,
var boostOnWater: Boolean = true,
var onlyForPlayers: Boolean = true
var onlyForPlayers: Boolean = true,
var extraCollisionDetectionRange: Double = 0.0
) {
constructor(buf: FriendlyByteBuf) : this(
buf.readFloat(),
Expand All @@ -165,7 +175,8 @@ data class Config(
buf.readBoolean(),
buf.readBoolean(),
buf.readBoolean(),
buf.readBoolean()
buf.readBoolean(),
buf.readDouble()
)
fun write(buf: FriendlyByteBuf) {
Expand All @@ -176,6 +187,7 @@ data class Config(
buf.writeBoolean(config.boostOnIce)
buf.writeBoolean(config.boostOnWater)
buf.writeBoolean(config.onlyForPlayers)
buf.writeDouble(config.extraCollisionDetectionRange)
}
}
Expand Down Expand Up @@ -203,6 +215,16 @@ fun migrate(tree: JsonElement, version: Int?): Config? {
boostUnderwater = jsonObject["boostUnderwater"]?.jsonPrimitive?.booleanOrNull ?: return null,
onlyForPlayers = jsonObject["onlyForPlayers"]?.jsonPrimitive?.booleanOrNull ?: return null
)
3 -> Config(
jsonObject["stepHeight"]?.jsonPrimitive?.floatOrNull ?: return null,
jsonObject["playerEjectTicks"]?.jsonPrimitive?.floatOrNull ?: return null,
jsonObject["boostUnderwater"]?.jsonPrimitive?.booleanOrNull ?: return null,
jsonObject["boostOnBlocks"]?.jsonPrimitive?.booleanOrNull ?: return null,
jsonObject["boostOnIce"]?.jsonPrimitive?.booleanOrNull ?: return null,
jsonObject["boostOnWater"]?.jsonPrimitive?.booleanOrNull ?: return null,
jsonObject["onlyForPlayers"]?.jsonPrimitive?.booleanOrNull ?: return null,
jsonObject["extraCollisionDetectionRange"]?.jsonPrimitive?.doubleOrNull ?: return null
)
else -> null
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/main/kotlin/dev/nyon/bbm/config/ConfigScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import net.minecraft.client.gui.screens.Screen
fun generateYaclScreen(parent: Screen?): Screen = YetAnotherConfigLib("bbm") {
val general by categories.registering {
val stepHeight by rootOptions.registering {
binding(1f, { config.stepHeight }, { config.stepHeight = it})
binding(1f, { config.stepHeight }, { config.stepHeight = it })
controller = numberField(0f)
descriptionBuilder {
addDefaultText(1)
}
}

val playerEjectTicks by rootOptions.registering {
binding(0.2f, { config.playerEjectTicks }, { config.playerEjectTicks = it})
binding(0.2f, { config.playerEjectTicks }, { config.playerEjectTicks = it })
controller = numberField(0f, 10000f)
descriptionBuilder {
addDefaultText(1)
Expand Down Expand Up @@ -61,6 +61,14 @@ fun generateYaclScreen(parent: Screen?): Screen = YetAnotherConfigLib("bbm") {
addDefaultText(1)
}
}

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

save { saveConfig(config) }
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/assets/bbm/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
"yacl3.config.bbm.category.general.root.option.boostOnWater": "Boost on water",
"yacl3.config.bbm.category.general.root.option.boostOnWater.description": "Toggles, whether a boat, which is on water should be boosted upwards when floating against an elevation.",
"yacl3.config.bbm.category.general.root.option.onlyForPlayers": "Only for players",
"yacl3.config.bbm.category.general.root.option.onlyForPlayers.description": "Toggles, whether a boat should only be boosted when carrying a player."
"yacl3.config.bbm.category.general.root.option.onlyForPlayers.description": "Toggles, whether a boat should only be boosted when carrying a player.",
"yacl3.config.bbm.category.general.root.option.extraCollisionDetectionRange": "Extra collision detection range",
"yacl3.config.bbm.category.general.root.option.extraCollisionDetectionRange.description": "Changes the detection range of a collision. Increasing this will boost a boat x blocks before actually touching the block it approaches.\n*You may encounter weird behaviour when changing this value to big numbers.*"
}
5 changes: 3 additions & 2 deletions src/main/resources/bbm.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"package": "dev.nyon.bbm.asm",
"compatibilityLevel": "JAVA_17",
"mixins": [
"BoatMixin"
"BoatMixin",
"EntityMixin"
],
"client": [
"MinecraftMixin"
],
"injectors": {
"defaultRequire": 1
}
}
}

0 comments on commit 111120e

Please sign in to comment.