Skip to content

Commit bb45d2f

Browse files
committed
Add client-side keybind for toggling the movement system on/off
- Unbound by default - The setting will persist between save/quits (saved to the config) Closes #11 and closes #17
1 parent bca6db8 commit bb45d2f

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

java/squeek/quakemovement/ModConfig.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import net.minecraftforge.common.config.Configuration;
5+
import net.minecraftforge.common.config.Property;
56

67
public class ModConfig
78
{
@@ -59,6 +60,11 @@ public class ModConfig
5960
private static final String MAX_AIR_ACCEL_PER_TICK_NAME = "maxAirAccelerationPerTick";
6061
private static final double MAX_AIR_ACCEL_PER_TICK_DEFAULT = 0.045D;
6162

63+
public static boolean ENABLED;
64+
private static Property ENABLED_PROPERTY;
65+
private static final String ENABLED_NAME = "enabled";
66+
private static final boolean ENABLED_DEFAULT = true;
67+
6268
private static Configuration config;
6369

6470
public static void init(File file)
@@ -84,6 +90,16 @@ public static void init(File file)
8490

8591
INCREASED_FALL_DISTANCE = (float) (config.get(CATEGORY_MOVEMENT, INCREASED_FALL_DISTANCE_NAME, INCREASED_FALL_DISTANCE_DEFAULT, "increases the distance needed to fall in order to take fall damage; this is a server-side setting").getDouble(INCREASED_FALL_DISTANCE_DEFAULT));
8692

93+
ENABLED_PROPERTY = config.get(CATEGORY_MOVEMENT, ENABLED_NAME, ENABLED_DEFAULT, "turns off/on the quake-style movement for the client (essentially the saved value of the ingame toggle keybind)");
94+
ENABLED = ENABLED_PROPERTY.getBoolean(ENABLED_DEFAULT);
95+
96+
save();
97+
}
98+
99+
public static void setEnabled(boolean enabled)
100+
{
101+
ModConfig.ENABLED = enabled;
102+
ENABLED_PROPERTY.set(enabled);
87103
save();
88104
}
89105

java/squeek/quakemovement/ModQuakeMovement.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public void preInit(FMLPreInitializationEvent event)
2626
{
2727
ModConfig.init(event.getSuggestedConfigurationFile());
2828
MinecraftForge.EVENT_BUS.register(this);
29+
if (event.getSide() == Side.CLIENT)
30+
{
31+
MinecraftForge.EVENT_BUS.register(new ToggleKeyHandler());
32+
}
2933
}
3034

3135
@EventHandler

java/squeek/quakemovement/QuakeClientPlayer.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public QuakeClientPlayer(ClientPlayerAPI playerapi)
4848
@Override
4949
public void moveEntityWithHeading(float sidemove, float forwardmove)
5050
{
51+
if (!ModConfig.ENABLED)
52+
{
53+
super.moveEntityWithHeading(sidemove, forwardmove);
54+
return;
55+
}
56+
5157
double d0 = this.player.posX;
5258
double d1 = this.player.posY;
5359
double d2 = this.player.posZ;
@@ -103,6 +109,12 @@ public void onLivingUpdate()
103109
@Override
104110
public void moveFlying(float sidemove, float forwardmove, float wishspeed)
105111
{
112+
if (!ModConfig.ENABLED)
113+
{
114+
super.moveFlying(sidemove, forwardmove, wishspeed);
115+
return;
116+
}
117+
106118
if ((this.player.capabilities.isFlying && this.player.getRidingEntity() == null) || this.player.isInWater() || this.player.isInLava() || this.player.isOnLadder())
107119
{
108120
super.moveFlying(sidemove, forwardmove, wishspeed);
@@ -121,6 +133,9 @@ public void jump()
121133
{
122134
super.jump();
123135

136+
if (!ModConfig.ENABLED)
137+
return;
138+
124139
// undo this dumb thing
125140
if (this.player.isSprinting())
126141
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package squeek.quakemovement;
2+
3+
import net.minecraft.client.resources.I18n;
4+
import net.minecraft.client.settings.KeyBinding;
5+
import net.minecraft.entity.player.EntityPlayer;
6+
import net.minecraft.util.text.TextComponentString;
7+
import net.minecraftforge.fml.client.FMLClientHandler;
8+
import net.minecraftforge.fml.client.registry.ClientRegistry;
9+
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
10+
import net.minecraftforge.fml.common.gameevent.InputEvent;
11+
import net.minecraftforge.fml.relauncher.Side;
12+
import net.minecraftforge.fml.relauncher.SideOnly;
13+
import org.lwjgl.input.Keyboard;
14+
15+
@SideOnly(Side.CLIENT)
16+
public class ToggleKeyHandler
17+
{
18+
private static final KeyBinding TOGGLE_KEY = new KeyBinding("squake.key.toggle", Keyboard.CHAR_NONE, ModInfo.MODID);
19+
20+
static
21+
{
22+
ClientRegistry.registerKeyBinding(TOGGLE_KEY);
23+
}
24+
25+
@SubscribeEvent
26+
public void onKeyEvent(InputEvent.KeyInputEvent event)
27+
{
28+
if (TOGGLE_KEY.isPressed())
29+
{
30+
ModConfig.setEnabled(!ModConfig.ENABLED);
31+
32+
EntityPlayer player = FMLClientHandler.instance().getClientPlayerEntity();
33+
String feedback = ModConfig.ENABLED ? I18n.format("squake.key.toggle.enabled") : I18n.format("squake.key.toggle.disabled");
34+
player.addChatMessage(new TextComponentString("[" + ModInfo.MODID + "] " + feedback));
35+
}
36+
}
37+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Keybinds
2+
squake.key.toggle=Toggle Movement System On/Off
3+
squake.key.toggle.enabled=Movement system enabled
4+
squake.key.toggle.disabled=Movement system disabled

0 commit comments

Comments
 (0)