Archive incari-client (client, 26.3, clean)
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
# togglesprint: Toggle Sprint
|
||||
|
||||
Latch sprint (or always sprint) and optionally sneak, with a HUD line saying what's active. Whether you can sprint is still the game's call. The framework's first module. ([ADR 0046](../../docs/adr/0046-module-framework.md))
|
||||
|
||||
| Setting | Kind | Default |
|
||||
| --- | --- | --- |
|
||||
| Sprint key | Hold, Toggle, Always | Toggle |
|
||||
| Sneak key | Hold, Toggle | Hold |
|
||||
| Show status | on/off | on |
|
||||
| Custom text color | on/off (shown with the status) | off: the theme's text color |
|
||||
| Text color | color (shown with a custom color) | white |
|
||||
| HUD: Status | position and scale | top left, 1x |
|
||||
|
||||
```sh
|
||||
./gradlew :features:togglesprint:check
|
||||
```
|
||||
@@ -0,0 +1,16 @@
|
||||
plugins {
|
||||
`java-library`
|
||||
}
|
||||
|
||||
// Toggle Sprint (the framework's first module, ADR 0046): the sprint and sneak keys latch, and the HUD says so.
|
||||
// A feature module: it sees the module framework and nothing else.
|
||||
dependencies {
|
||||
api(project(":framework"))
|
||||
|
||||
testImplementation(testFixtures(project(":framework")))
|
||||
testImplementation(platform(libs.junit.bom))
|
||||
testImplementation(libs.junit.jupiter)
|
||||
testImplementation(libs.assertj)
|
||||
testImplementation(libs.archunit)
|
||||
testRuntimeOnly(libs.junit.launcher)
|
||||
}
|
||||
+236
@@ -0,0 +1,236 @@
|
||||
package incari.features.togglesprint;
|
||||
|
||||
import incari.framework.Category;
|
||||
import incari.framework.Module;
|
||||
import incari.framework.event.Events;
|
||||
import incari.framework.event.MovementInput;
|
||||
import incari.framework.game.PlayerView;
|
||||
import incari.framework.hud.Anchor;
|
||||
import incari.framework.hud.HudCanvas;
|
||||
import incari.framework.hud.HudElement;
|
||||
import incari.framework.hud.Tone;
|
||||
import incari.framework.setting.BooleanSetting;
|
||||
import incari.framework.setting.ChoiceSetting;
|
||||
import incari.framework.setting.ColorSetting;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Toggle Sprint: a press of the sprint key latches sprinting on until the next press, or the player
|
||||
* always sprints; the sneak key can latch the same way. A line on the HUD says what's latched.
|
||||
*
|
||||
* <p>It changes only what the movement keys say ({@link Events#MOVEMENT}): whether the player can
|
||||
* sprint (hunger, blindness, walking backwards) is still the game's call, as it is for the key.
|
||||
*/
|
||||
public final class ToggleSprint extends Module {
|
||||
|
||||
/** How the sprint key works. */
|
||||
public enum SprintMode {
|
||||
/** As the game has it: sprint while held. */
|
||||
HOLD("Hold"),
|
||||
/** A press latches sprinting on, the next turns it off. */
|
||||
TOGGLE("Toggle"),
|
||||
/** Always sprint; the key does nothing more. */
|
||||
ALWAYS("Always");
|
||||
|
||||
private final String label;
|
||||
|
||||
SprintMode(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
/** What the settings screen calls it. */
|
||||
public String label() {
|
||||
return label;
|
||||
}
|
||||
}
|
||||
|
||||
/** How the sneak key works. */
|
||||
public enum SneakMode {
|
||||
/** As the game has it: sneak while held. */
|
||||
HOLD("Hold"),
|
||||
/** A press latches sneaking on, the next turns it off. */
|
||||
TOGGLE("Toggle");
|
||||
|
||||
private final String label;
|
||||
|
||||
SneakMode(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
/** What the settings screen calls it. */
|
||||
public String label() {
|
||||
return label;
|
||||
}
|
||||
}
|
||||
|
||||
// Made once: the HUD asks for its text a few dozen times a second.
|
||||
static final String SPRINTING_TOGGLED = "[Sprinting (Toggled)]";
|
||||
static final String SPRINTING_ALWAYS = "[Sprinting (Always)]";
|
||||
static final String SPRINTING_HELD = "[Sprinting (Key Held)]";
|
||||
static final String SNEAKING_TOGGLED = "[Sneaking (Toggled)]";
|
||||
static final String SNEAKING_HELD = "[Sneaking (Key Held)]";
|
||||
|
||||
private final ChoiceSetting<SprintMode> sprint = settings()
|
||||
.choice("sprint", "Sprint key", SprintMode.values(), SprintMode.TOGGLE, SprintMode::label)
|
||||
.describedAs("Toggle: press once to sprint, again to stop. Always: sprint whenever you can.");
|
||||
private final ChoiceSetting<SneakMode> sneak = settings()
|
||||
.choice("sneak", "Sneak key", SneakMode.values(), SneakMode.HOLD, SneakMode::label)
|
||||
.describedAs("Toggle: press once to sneak, again to stop.");
|
||||
private final BooleanSetting showStatus = settings()
|
||||
.bool("show-status", "Show status", true)
|
||||
.describedAs("Shows on the HUD when sprint or sneak is toggled or held.");
|
||||
private final BooleanSetting customColor = settings()
|
||||
.bool("custom-color", "Custom text color", false)
|
||||
.describedAs("Off: the status follows the client's theme.")
|
||||
.visibleWhen(showStatus::get);
|
||||
private final ColorSetting color =
|
||||
settings().color("color", "Text color", 0xFFFFFF).visibleWhen(() -> showStatus.get() && customColor.get());
|
||||
|
||||
private boolean sprintLatched;
|
||||
private boolean sneakLatched;
|
||||
/** What the HUD says, as of the last tick; null for nothing. */
|
||||
private @Nullable String status;
|
||||
|
||||
/** Toggle Sprint, off until the player turns it on. */
|
||||
public ToggleSprint() {
|
||||
super("togglesprint", "Toggle Sprint", "Sprint and sneak without holding the keys.", Category.MOVEMENT);
|
||||
hud(new Status());
|
||||
on(Events.MOVEMENT, this::onMovementInput);
|
||||
on(Events.LEAVE, this::unlatch);
|
||||
on(Events.TICK, this::onTick);
|
||||
sprint.onChange(this::unlatch);
|
||||
sneak.onChange(this::unlatch);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDisable() {
|
||||
unlatch();
|
||||
}
|
||||
|
||||
private void unlatch() {
|
||||
sprintLatched = false;
|
||||
sneakLatched = false;
|
||||
status = null;
|
||||
}
|
||||
|
||||
private void onMovementInput(MovementInput input) {
|
||||
// An odd number of presses since the last tick flips a latch; an even number leaves it.
|
||||
// If the game's own toggle option already latches the key, leave it to the game: two
|
||||
// latches would fight once the game resets its own (on respawn).
|
||||
boolean sprintHeld = input.sprint();
|
||||
boolean sprintToggled = false;
|
||||
switch (sprint.get()) {
|
||||
case HOLD -> sprintLatched = false;
|
||||
case TOGGLE -> {
|
||||
if (input.sprintToggledByGame()) {
|
||||
sprintLatched = false;
|
||||
sprintToggled = sprintHeld;
|
||||
} else {
|
||||
if (input.sprintPresses() % 2 == 1) {
|
||||
sprintLatched = !sprintLatched;
|
||||
}
|
||||
if (sprintLatched) {
|
||||
input.setSprint(true);
|
||||
}
|
||||
sprintToggled = sprintLatched;
|
||||
}
|
||||
}
|
||||
case ALWAYS -> input.setSprint(true);
|
||||
}
|
||||
boolean sneakHeld = input.sneak();
|
||||
boolean sneakToggled;
|
||||
if (sneak.get() == SneakMode.TOGGLE && !input.sneakToggledByGame()) {
|
||||
if (input.sneakPresses() % 2 == 1) {
|
||||
sneakLatched = !sneakLatched;
|
||||
}
|
||||
if (sneakLatched) {
|
||||
input.setSneak(true);
|
||||
}
|
||||
sneakToggled = sneakLatched;
|
||||
} else {
|
||||
sneakLatched = false;
|
||||
sneakToggled = sneak.get() == SneakMode.TOGGLE && sneakHeld;
|
||||
}
|
||||
status = statusFor(sprintHeld, sprintToggled, sneakHeld, sneakToggled);
|
||||
}
|
||||
|
||||
/** Death lets go of the latches, as the game lets go of its own on respawn. */
|
||||
private void onTick() {
|
||||
PlayerView player = system().game().player();
|
||||
if (player != null && player.isDead()) {
|
||||
unlatch();
|
||||
}
|
||||
}
|
||||
|
||||
private @Nullable String statusFor(
|
||||
boolean sprintHeld, boolean sprintToggled, boolean sneakHeld, boolean sneakToggled) {
|
||||
// Sneaking first: it stops sprinting, so it's what the player is doing.
|
||||
if (sneakToggled) {
|
||||
return SNEAKING_TOGGLED;
|
||||
}
|
||||
if (sneakHeld) {
|
||||
return SNEAKING_HELD;
|
||||
}
|
||||
if (sprint.get() == SprintMode.ALWAYS) {
|
||||
return SPRINTING_ALWAYS;
|
||||
}
|
||||
if (sprintToggled) {
|
||||
return SPRINTING_TOGGLED;
|
||||
}
|
||||
return sprintHeld ? SPRINTING_HELD : null;
|
||||
}
|
||||
|
||||
/** What the HUD line says now, or null for nothing. For tests. */
|
||||
@Nullable
|
||||
String status() {
|
||||
return status;
|
||||
}
|
||||
|
||||
/** The HUD line. */
|
||||
private final class Status extends HudElement {
|
||||
|
||||
/** What it says now; in the HUD editor, the longest common line when nothing's latched. */
|
||||
private @Nullable String shown(HudCanvas canvas) {
|
||||
String text = status;
|
||||
return text == null && canvas.editing() ? SPRINTING_TOGGLED : text;
|
||||
}
|
||||
|
||||
Status() {
|
||||
super("status", "Status", Anchor.TOP_LEFT, 2, 2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isVisible() {
|
||||
return showStatus.get() && status != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPlaceable() {
|
||||
return showStatus.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int width(HudCanvas canvas) {
|
||||
String text = shown(canvas);
|
||||
return text == null ? 0 : canvas.textWidth(text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int height(HudCanvas canvas) {
|
||||
return canvas.lineHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(HudCanvas canvas) {
|
||||
String text = shown(canvas);
|
||||
if (text == null) {
|
||||
return;
|
||||
}
|
||||
if (customColor.get()) {
|
||||
canvas.text(text, 0, 0, color.get());
|
||||
} else {
|
||||
canvas.text(text, 0, 0, Tone.TEXT);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
/** Toggle Sprint (the framework's first module, ADR 0046): the sprint and sneak keys latch. */
|
||||
@NullMarked
|
||||
package incari.features.togglesprint;
|
||||
|
||||
import org.jspecify.annotations.NullMarked;
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package incari.features.togglesprint;
|
||||
|
||||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
|
||||
|
||||
import com.tngtech.archunit.core.importer.ImportOption;
|
||||
import com.tngtech.archunit.junit.AnalyzeClasses;
|
||||
import com.tngtech.archunit.junit.ArchTest;
|
||||
import com.tngtech.archunit.lang.ArchRule;
|
||||
|
||||
/** A feature sees the module framework and the JDK's basics: no game, platform or other feature. */
|
||||
@AnalyzeClasses(packages = "incari.features.togglesprint", importOptions = ImportOption.DoNotIncludeTests.class)
|
||||
class BoundaryTest {
|
||||
|
||||
@ArchTest
|
||||
static final ArchRule onlyTheFramework = classes()
|
||||
.should()
|
||||
.onlyDependOnClassesThat()
|
||||
.resideInAnyPackage(
|
||||
"java.lang..",
|
||||
"java.util..",
|
||||
"org.jspecify..",
|
||||
"incari.framework..",
|
||||
"incari.features.togglesprint..");
|
||||
}
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
package incari.features.togglesprint;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import incari.framework.event.MovementInput;
|
||||
import incari.framework.setting.ChoiceSetting;
|
||||
import incari.framework.setting.Setting;
|
||||
import incari.framework.testing.TestGame;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ToggleSprintTest {
|
||||
|
||||
private final ToggleSprint module = new ToggleSprint();
|
||||
private final TestGame game = new TestGame(module);
|
||||
|
||||
@Test
|
||||
void doesNothingWhileOff() {
|
||||
assertThat(game.movement(false, 1).sprint()).isFalse();
|
||||
assertThat(game.drawHud().drawn).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPressLatchesSprintingUntilTheNext() {
|
||||
module.setEnabled(true);
|
||||
|
||||
assertThat(game.movement(false, 1).sprint()).isTrue();
|
||||
assertThat(game.movement(false, 0).sprint()).isTrue();
|
||||
assertThat(module.status()).isEqualTo(ToggleSprint.SPRINTING_TOGGLED);
|
||||
assertThat(game.movement(false, 1).sprint()).isFalse();
|
||||
assertThat(module.status()).isNull();
|
||||
// Two taps within one tick cancel out.
|
||||
assertThat(game.movement(false, 2).sprint()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void theGamesOwnToggleIsLeftToTheGame() {
|
||||
module.setEnabled(true);
|
||||
game.gameTogglesSprint = true;
|
||||
|
||||
// The game's latch on: the module doesn't latch a second time.
|
||||
MovementInput input = game.movement(true, 1);
|
||||
assertThat(input.sprint()).isTrue();
|
||||
assertThat(input.changed()).isFalse();
|
||||
assertThat(module.status()).isEqualTo(ToggleSprint.SPRINTING_TOGGLED);
|
||||
// The game reset its latch (a respawn): a press then latches it again, and the next stops.
|
||||
assertThat(game.movement(false, 0).sprint()).isFalse();
|
||||
assertThat(game.movement(true, 1).sprint()).isTrue();
|
||||
assertThat(game.movement(false, 1).sprint()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void deathLetsGoOfTheLatches() {
|
||||
module.setEnabled(true);
|
||||
game.join();
|
||||
assertThat(game.movement(false, 1).sprint()).isTrue();
|
||||
|
||||
game.player.dead(true);
|
||||
game.tick();
|
||||
game.player.dead(false);
|
||||
assertThat(game.movement(false, 0).sprint()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void holdingTheKeyStillSprintsWhenNotLatched() {
|
||||
module.setEnabled(true);
|
||||
MovementInput input = game.movement(true, 0);
|
||||
assertThat(input.sprint()).isTrue();
|
||||
assertThat(input.changed()).isFalse();
|
||||
assertThat(module.status()).isEqualTo(ToggleSprint.SPRINTING_HELD);
|
||||
}
|
||||
|
||||
@Test
|
||||
void alwaysSprintsWithoutTheKey() {
|
||||
module.setEnabled(true);
|
||||
choice("sprint", ToggleSprint.SprintMode.ALWAYS);
|
||||
|
||||
assertThat(game.movement(false, 0).sprint()).isTrue();
|
||||
assertThat(module.status()).isEqualTo(ToggleSprint.SPRINTING_ALWAYS);
|
||||
}
|
||||
|
||||
@Test
|
||||
void holdModeLeavesTheKeyAlone() {
|
||||
module.setEnabled(true);
|
||||
choice("sprint", ToggleSprint.SprintMode.HOLD);
|
||||
|
||||
MovementInput input = game.movement(false, 1);
|
||||
assertThat(input.sprint()).isFalse();
|
||||
assertThat(input.changed()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void sneakLatchesWhenSetToToggle() {
|
||||
module.setEnabled(true);
|
||||
choice("sneak", ToggleSprint.SneakMode.TOGGLE);
|
||||
|
||||
MovementInput input = game.movement(false, 0, false, 1);
|
||||
assertThat(input.sneak()).isTrue();
|
||||
assertThat(module.status()).isEqualTo(ToggleSprint.SNEAKING_TOGGLED);
|
||||
assertThat(game.movement(false, 0, false, 1).sneak()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void leavingTheWorldOrTurningOffUnlatches() {
|
||||
module.setEnabled(true);
|
||||
game.movement(false, 1);
|
||||
game.system().events().fireLeave();
|
||||
assertThat(game.movement(false, 0).sprint()).isFalse();
|
||||
|
||||
game.movement(false, 1);
|
||||
module.setEnabled(false);
|
||||
module.setEnabled(true);
|
||||
assertThat(game.movement(false, 0).sprint()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void theHudSaysWhatsLatchedInTheThemeOrTheChosenColor() {
|
||||
module.setEnabled(true);
|
||||
game.movement(false, 1);
|
||||
|
||||
assertThat(game.drawHud().drawn).containsExactly("text \"[Sprinting (Toggled)]\" at 2,2 TEXT");
|
||||
|
||||
bool("custom-color", true);
|
||||
assertThat(game.drawHud().drawn).containsExactly("text \"[Sprinting (Toggled)]\" at 2,2 #ffffffff");
|
||||
|
||||
bool("show-status", false);
|
||||
assertThat(game.drawHud().drawn).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void theHudEditorShowsASampleUntilTheStatusIsTurnedOff() {
|
||||
module.setEnabled(true);
|
||||
assertThat(game.drawHud().drawn).isEmpty();
|
||||
assertThat(game.drawHud(true).drawn).containsExactly("text \"[Sprinting (Toggled)]\" at 2,2 TEXT");
|
||||
|
||||
bool("show-status", false);
|
||||
assertThat(game.drawHud(true).drawn).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void theColorShowsInTheSettingsOnlyWhenItsUsed() {
|
||||
Setting<?> color = setting("color");
|
||||
assertThat(color.isVisible()).isFalse();
|
||||
bool("custom-color", true);
|
||||
assertThat(color.isVisible()).isTrue();
|
||||
bool("show-status", false);
|
||||
assertThat(color.isVisible()).isFalse();
|
||||
}
|
||||
|
||||
private Setting<?> setting(String id) {
|
||||
Setting<?> setting = module.settings().get(id);
|
||||
assertThat(setting).as(id).isNotNull();
|
||||
return setting;
|
||||
}
|
||||
|
||||
private void bool(String id, boolean value) {
|
||||
((incari.framework.setting.BooleanSetting) setting(id)).set(value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <E extends Enum<E>> void choice(String id, E value) {
|
||||
((ChoiceSetting<E>) setting(id)).set(value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user