Skip to content

Commit

Permalink
Add API to force modified speed logic on carts
Browse files Browse the repository at this point in the history
Other mods can use AudakiCartEngine.registerModifiedEngineCheck to
register a cart checker function. Return true to force modified speed
logic, false to force vanilla speed logic.
  • Loading branch information
GeeTransit committed Sep 17, 2023
1 parent 1f7056c commit 54c13dd
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
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
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

0 comments on commit 54c13dd

Please sign in to comment.