Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibility with Linkart mod #23

Closed
wants to merge 4 commits into from
Closed
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
22 changes: 22 additions & 0 deletions src/main/java/audaki/cart_engine/AudakiCartEngine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package audaki.cart_engine;

import net.fabricmc.api.ModInitializer;
import net.minecraft.entity.vehicle.AbstractMinecartEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class AudakiCartEngine implements ModInitializer {
public static List<Function<AbstractMinecartEntity, Boolean>> CART_CHECK_MODIFIED_ENGINE = new ArrayList<>();

// Function should take one AbstractMinecartEntity. Return true to force
// modified speed logic, false to force vanilla speed logic, and null
// otherwise.
public static void registerModifiedEngineCheck(Function<AbstractMinecartEntity, Boolean> cartChecker) {
CART_CHECK_MODIFIED_ENGINE.add(cartChecker);
}

public void onInitialize() {
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package audaki.cart_engine.mixin;

import audaki.cart_engine.AudakiCartEngine;
import com.mojang.datafixers.util.Pair;
import net.minecraft.block.AbstractRailBlock;
import net.minecraft.block.BlockState;
Expand Down Expand Up @@ -28,6 +29,7 @@
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

@Mixin(AbstractMinecartEntity.class) // lower value, higher priority - apply first so other mods can still mixin
Expand Down Expand Up @@ -69,16 +71,34 @@ private static RailShape getRailShape(BlockState state) {

@Inject(at = @At("HEAD"), method = "moveOnRail", cancellable = true)
protected void moveOnRailOverwrite(BlockPos pos, BlockState state, CallbackInfo ci) {
// Loop through checks registered by other mods
Boolean shouldUseModifiedEngine = null;
for (Function<AbstractMinecartEntity, Boolean> cartChecker : AudakiCartEngine.CART_CHECK_MODIFIED_ENGINE) {
shouldUseModifiedEngine = cartChecker.apply((AbstractMinecartEntity) (Object) this);
// Check if the modified engine is required/suppressed
if (shouldUseModifiedEngine != null) {
break;
}
}

// We only change logic for rideable minecarts so we don't break hopper/chest minecart creations
if (this.getMinecartType() != Type.RIDEABLE) {
// Check if modified engine was suppressed
if (!shouldUseModifiedEngine) {
return;
}

// We only change logic when the minecart is currently being ridden by a living entity (player/villager/mob)
boolean hasLivingRider = this.getFirstPassenger() instanceof LivingEntity;
if (!hasLivingRider) {
return;
// If no mods required modified engine, we do our own logic here
if (shouldUseModifiedEngine == null) {

// We only change logic for rideable minecarts so we don't break hopper/chest minecart creations
if (this.getMinecartType() != Type.RIDEABLE) {
return;
}

// We only change logic when the minecart is currently being ridden by a living entity (player/villager/mob)
boolean hasLivingRider = this.getFirstPassenger() instanceof LivingEntity;
if (!hasLivingRider) {
return;
}
}

this.modifiedMoveOnRail(pos, state);
Expand Down Expand Up @@ -148,9 +168,6 @@ protected void modifiedMoveOnRail(BlockPos pos, BlockState state) {
Supplier<Double> calculateMaxHorizontalMovementPerTick = () -> {
double fallback = this.getMaxSpeed();

if (!this.hasPassengers())
return fallback;

if (horizontalMomentumPerTick < vanillaMaxHorizontalMovementPerTick)
return fallback;

Expand Down Expand Up @@ -291,7 +308,7 @@ protected void modifiedMoveOnRail(BlockPos pos, BlockState state) {
d = p + h * x;
f = q + i * x;
this.setPosition(d, e, f);
v = this.hasPassengers() ? 0.75D : 1.0D;
v = 1.0D;

w = maxHorizontalMovementPerTick;

Expand Down Expand Up @@ -338,30 +355,25 @@ protected void modifiedMoveOnRail(BlockPos pos, BlockState state) {
final double basisAccelerationPerTick = 0.021D;
if (momentum > 0.01D) {

if (this.hasPassengers()) {
// Based on a 10 ticks per second basis spent per powered block we calculate a fair acceleration per tick
// due to spending less ticks per powered block on higher speeds (and even skipping blocks)
final double basisTicksPerSecond = 10.0D;
// Tps = Ticks per second
final double tickMovementForBasisTps = 1.0D / basisTicksPerSecond;
final double maxSkippedBlocksToConsider = 3.0D;
// Based on a 10 ticks per second basis spent per powered block we calculate a fair acceleration per tick
// due to spending less ticks per powered block on higher speeds (and even skipping blocks)
final double basisTicksPerSecond = 10.0D;
// Tps = Ticks per second
final double tickMovementForBasisTps = 1.0D / basisTicksPerSecond;
final double maxSkippedBlocksToConsider = 3.0D;


double acceleration = basisAccelerationPerTick;
final double distanceMovedHorizontally = movement.horizontalLength();
double acceleration = basisAccelerationPerTick;
final double distanceMovedHorizontally = movement.horizontalLength();

if (distanceMovedHorizontally > tickMovementForBasisTps) {
acceleration *= Math.min((1.0D + maxSkippedBlocksToConsider) * basisTicksPerSecond, distanceMovedHorizontally / tickMovementForBasisTps);
if (distanceMovedHorizontally > tickMovementForBasisTps) {
acceleration *= Math.min((1.0D + maxSkippedBlocksToConsider) * basisTicksPerSecond, distanceMovedHorizontally / tickMovementForBasisTps);

// Add progressively slower (or faster) acceleration for higher speeds;
double highspeedFactor = 1.0D + MathHelper.clamp(-0.45D * (distanceMovedHorizontally / tickMovementForBasisTps / basisTicksPerSecond), -0.7D, 2.0D);
acceleration *= highspeedFactor;
}
this.setVelocity(vec3d7.add(acceleration * (vec3d7.x / momentum), 0.0D, acceleration * (vec3d7.z / momentum)));
}
else {
this.setVelocity(vec3d7.add(vec3d7.x / momentum * 0.06D, 0.0D, vec3d7.z / momentum * 0.06D));
// Add progressively slower (or faster) acceleration for higher speeds;
double highspeedFactor = 1.0D + MathHelper.clamp(-0.45D * (distanceMovedHorizontally / tickMovementForBasisTps / basisTicksPerSecond), -0.7D, 2.0D);
acceleration *= highspeedFactor;
}
this.setVelocity(vec3d7.add(acceleration * (vec3d7.x / momentum), 0.0D, acceleration * (vec3d7.z / momentum)));


} else {
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
"icon": "assets/audaki_cart_engine/icon.png",

"environment": "*",
"entrypoints": {
"main": [
"audaki.cart_engine.AudakiCartEngine"
]
},
"mixins": [
"audaki_cart_engine.mixins.json"
],
Expand Down
Loading