1
0
mirror of https://github.com/chylex/Better-Controls.git synced 2025-05-05 10:34:06 +02:00

Implement option to fly on ground in creative mode

This commit is contained in:
chylex 2020-10-17 15:19:17 +02:00
parent bf13fb002e
commit d7151afd0e
6 changed files with 65 additions and 0 deletions

View File

@ -11,6 +11,7 @@ public final class BetterControlsConfig{
public boolean doubleTapForwardToSprint = true;
public boolean flyOnGroundInCreative = false;
public float flightSpeedMpCreativeDefault = 1F;
public float flightSpeedMpCreativeSprinting = 2F;
public float flightSpeedMpSpectatorDefault = 1F;

View File

@ -32,6 +32,7 @@ final class ConfigSerializer implements JsonSerializer<BetterControlsConfig>, Js
Json.setBool(obj, "Sprint.DoubleTapForward", cfg.doubleTapForwardToSprint);
Json.setBool(obj, "Flight.FlyOnGround.Creative", cfg.flyOnGroundInCreative);
Json.setFloat(obj, "Flight.SpeedMp.Creative.Default", cfg.flightSpeedMpCreativeDefault);
Json.setFloat(obj, "Flight.SpeedMp.Creative.Sprinting", cfg.flightSpeedMpCreativeSprinting);
Json.setFloat(obj, "Flight.SpeedMp.Spectator.Default", cfg.flightSpeedMpSpectatorDefault);
@ -47,6 +48,7 @@ final class ConfigSerializer implements JsonSerializer<BetterControlsConfig>, Js
cfg.doubleTapForwardToSprint = Json.getBool(obj, "Sprint.DoubleTapForward", cfg.doubleTapForwardToSprint);
cfg.flyOnGroundInCreative = Json.getBool(obj, "Flight.FlyOnGround.Creative", cfg.flyOnGroundInCreative);
cfg.flightSpeedMpCreativeDefault = Json.getFloat(obj, "Flight.SpeedMp.Creative.Default", cfg.flightSpeedMpCreativeDefault);
cfg.flightSpeedMpCreativeSprinting = Json.getFloat(obj, "Flight.SpeedMp.Creative.Sprinting", cfg.flightSpeedMpCreativeSprinting);
cfg.flightSpeedMpSpectatorDefault = Json.getFloat(obj, "Flight.SpeedMp.Spectator.Default", cfg.flightSpeedMpSpectatorDefault);

View File

@ -63,6 +63,11 @@ public class BetterControlsScreen extends GameOptionsScreen{
new Option<>(Float.valueOf(8.00F), Text.of("8x"))
);
generateLeftSideText(y, elements, Text.of("Fly On Ground (Creative Mode)"));
elements.add(new BooleanValueWidget(col2(1), y, COL2_W, cfg.flyOnGroundInCreative, value -> cfg.flyOnGroundInCreative = value));
y += ROW_HEIGHT * 4 / 3;
generateLeftSideText(y, elements, Text.of("Speed Multiplier (Creative)"));
elements.add(new DiscreteValueSliderWidget<>(col2(1), y, COL2_W, flightSpeedOptions, cfg.flightSpeedMpCreativeDefault, value -> cfg.flightSpeedMpCreativeDefault = value));

View File

@ -27,4 +27,10 @@ public abstract class HookClientPlayerTick extends AbstractClientPlayerEntity{
final ClientPlayerEntity player = (ClientPlayerEntity)(Object)this;
PlayerTicker.get(player).afterInputTick(player);
}
@Inject(method = "tickMovement()V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/AbstractClientPlayerEntity;tickMovement()V", ordinal = 0, shift = AFTER))
private void afterSuperCall(final CallbackInfo info){
final ClientPlayerEntity player = (ClientPlayerEntity)(Object)this;
PlayerTicker.get(player).afterSuperCall(player);
}
}

View File

@ -13,6 +13,10 @@ final class FlightHelper{
return BetterControlsMod.config;
}
static boolean shouldFlyOnGround(final ClientPlayerEntity player){
return cfg().flyOnGroundInCreative && player.isCreative() && player.abilities.flying;
}
static float getFlightSpeed(final ClientPlayerEntity player){
if (player.isCreative()){
if (player.isSprinting()){

View File

@ -33,7 +33,14 @@ public final class PlayerTicker{
// Logic
private boolean wasSneakingBeforeTouchingGround = false;
private boolean holdingSneakWhileTouchingGround = false;
public void atHead(final ClientPlayerEntity player){
if (FlightHelper.shouldFlyOnGround(player)){
player.setOnGround(false);
}
if (!cfg().doubleTapForwardToSprint){
((AccessClientPlayerFields)player).setTicksLeftToDoubleTapSprint(0);
}
@ -46,4 +53,44 @@ public final class PlayerTicker{
player.abilities.setFlySpeed(flightSpeed);
}
}
public void afterSuperCall(final ClientPlayerEntity player){
if (FlightHelper.shouldFlyOnGround(player)){
final boolean isSneaking = player.isSneaking();
final boolean isOnGround = player.isOnGround();
if (!isSneaking){
wasSneakingBeforeTouchingGround = false;
}
else if (!isOnGround){
wasSneakingBeforeTouchingGround = true;
}
if (!isOnGround){
holdingSneakWhileTouchingGround = false;
}
else{
boolean cancelLanding = true;
if (!wasSneakingBeforeTouchingGround){
if (isSneaking){
holdingSneakWhileTouchingGround = true;
}
else if (holdingSneakWhileTouchingGround){
player.abilities.flying = false;
player.sendAbilitiesUpdate();
cancelLanding = false;
}
}
if (cancelLanding){
player.setOnGround(false);
}
}
}
else{
wasSneakingBeforeTouchingGround = false;
holdingSneakWhileTouchingGround = false;
}
}
}