Skip to content

Commit

Permalink
Add configurable speeds for multiple types of minecarts
Browse files Browse the repository at this point in the history
  • Loading branch information
audaki committed Dec 15, 2024
1 parent ea57e11 commit 6c82c98
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.1.0] - 2024-12-15
- Add more configurable speeds via /gamerule
- minecartMaxSpeed: Generic max speed for all carts unless overridden
- minecartMaxSpeedPlayerRider: If not 0, overrides speed only for player ridden carts
- minecartMaxSpeedOtherRider: If not 0, overrides speed only for non-player ridden carts
- minecartMaxSpeedEmptyRider: If not 0, overrides speed only for rideable carts that are empty
- (Removed gamerule aceCartSpeed)

## [4.0.0] - 2024-12-15
- Rebase mod on improved minecarts experiment
- The experimental code will be activated for all carts
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ loader_version=0.16.9
fabric_api_version=0.112.0+1.21.3

# Mod Properties
mod_version = 4.0.0
mod_version = 4.1.0
maven_group = audaki.minecraft
archives_base_name = ACE
13 changes: 11 additions & 2 deletions src/main/java/audaki/cart_engine/AceGameRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@
import net.minecraft.world.level.GameRules;

public class AceGameRules {
public static GameRules.Key<GameRules.IntegerValue> ACE_CART_SPEED;
// All speeds in blocks per second
public static GameRules.Key<GameRules.IntegerValue> MINECART_MAX_SPEED_PLAYER_RIDER;
public static GameRules.Key<GameRules.IntegerValue> MINECART_MAX_SPEED_OTHER_RIDER;
public static GameRules.Key<GameRules.IntegerValue> MINECART_MAX_SPEED_EMPTY_RIDER;

public static void register() {
ACE_CART_SPEED = GameRuleRegistry.register("aceCartSpeed",
MINECART_MAX_SPEED_PLAYER_RIDER = GameRuleRegistry.register("minecartMaxSpeedPlayerRider",
GameRules.Category.PLAYER,
GameRuleFactory.createIntRule(20));
MINECART_MAX_SPEED_OTHER_RIDER = GameRuleRegistry.register("minecartMaxSpeedOtherRider",
GameRules.Category.PLAYER,
GameRuleFactory.createIntRule(0));
MINECART_MAX_SPEED_EMPTY_RIDER = GameRuleRegistry.register("minecartMaxSpeedEmptyRider",
GameRules.Category.PLAYER,
GameRuleFactory.createIntRule(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

import audaki.cart_engine.AceGameRules;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.vehicle.AbstractMinecart;
import net.minecraft.world.entity.vehicle.MinecartBehavior;
import net.minecraft.world.entity.vehicle.NewMinecartBehavior;
import net.minecraft.world.level.GameRules;
import org.spongepowered.asm.mixin.Mixin;
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.CallbackInfoReturnable;

import java.util.function.Consumer;
import java.util.function.IntConsumer;

@Mixin(NewMinecartBehavior.class)
public abstract class NewMinecartBehaviorMixin extends MinecartBehavior {

Expand All @@ -19,10 +27,29 @@ protected NewMinecartBehaviorMixin(AbstractMinecart abstractMinecart) {

@Inject(at = @At("HEAD"), method = "getMaxSpeed", cancellable = true)
public void _getMaxSpeed(ServerLevel level, CallbackInfoReturnable<Double> cir) {
if (minecart.isRideable()) {
int speedBlocksPerSecond = level.getGameRules().getInt(AceGameRules.ACE_CART_SPEED);
cir.setReturnValue(speedBlocksPerSecond * (this.minecart.isInWater() ? 0.5 : 1.0) / 20.0);
if (!minecart.isRideable()) {
return;
}

IntConsumer setSpeed = (speed) -> {
if (speed == 0) {
return;
}
cir.setReturnValue(speed * (this.minecart.isInWater() ? 0.5 : 1.0) / 20.0);
cir.cancel();
};

Entity passenger = minecart.getFirstPassenger();
if (passenger == null) {
setSpeed.accept(level.getGameRules().getInt(AceGameRules.MINECART_MAX_SPEED_EMPTY_RIDER));
return;
}

if (passenger instanceof Player) {
setSpeed.accept(level.getGameRules().getInt(AceGameRules.MINECART_MAX_SPEED_PLAYER_RIDER));
return;
}

setSpeed.accept(level.getGameRules().getInt(AceGameRules.MINECART_MAX_SPEED_OTHER_RIDER));
}
}

0 comments on commit 6c82c98

Please sign in to comment.