Skip to content

Commit

Permalink
Updated to 1.19.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Galapen committed Mar 27, 2023
1 parent a725dde commit e378650
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 116 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Sneaking (HOLD) can be active by holding the sneak key. When you let go, you wil

## Usage
### Version
This mod is for 1.18 Minecraft clients using <a href="https://files.minecraftforge.net/net/minecraftforge/forge/">Forge</a>.
This mod is for 1.19.4 Minecraft clients using <a href="https://files.minecraftforge.net/net/minecraftforge/forge/">Forge</a>.
This does not work for versions under 1.19.4.

### Installation
- Download the <a href="https://github.com/Shotbow/ToggleSneak/releases">ToggleSneak jar</a> for the desired Minecraft version
Expand Down
22 changes: 6 additions & 16 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
buildscript {
repositories {
maven { url = 'https://maven.minecraftforge.net' }
mavenCentral()
}
dependencies {
classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '5.1.+', changing: true
}
}

plugins {
id 'eclipse'
id 'maven-publish'
id "io.freefair.lombok" version "6.4.3"
id 'io.freefair.lombok' version '6.4.3'
id 'net.minecraftforge.gradle' version '5.1.+'
}
apply plugin: 'net.minecraftforge.gradle'

ext {
title = "Toggle Sneak"
mod_id = "togglesneak"
mc_version = "1.18"
mc_version = "1.19.4"
}
version = '1.1'
version = '1.2'
group = 'net.shotbow'
archivesBaseName = 'ToggleSneak-' + project.mc_version

Expand All @@ -42,7 +32,7 @@ minecraft {
//
// Use non-default mappings at your own risk. They may not always work.
// Simply re-run your setup task after changing the mappings to update your workspace.
mappings channel: 'official', version: '1.18.2'
mappings channel: 'official', version: '1.19.4'

// Default run configurations.
// These can be tweaked, removed, or duplicated as needed.
Expand Down Expand Up @@ -139,7 +129,7 @@ minecraft {
sourceSets.main.resources { srcDir 'src/generated/resources' }

dependencies {
minecraft 'net.minecraftforge:forge:1.18.2-40.1.19'
minecraft 'net.minecraftforge:forge:1.19.4-45.0.25'
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
6 changes: 6 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pluginManagement {
repositories {
gradlePluginPortal()
maven { url = 'https://maven.minecraftforge.net/' }
}
}
6 changes: 3 additions & 3 deletions src/main/java/net/shotbow/ToggleSneak/ToggleSneak.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import lombok.Getter;
import net.minecraft.client.Minecraft;
import net.minecraftforge.client.ConfigGuiHandler;
import net.minecraftforge.client.ConfigScreenHandler;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
Expand Down Expand Up @@ -32,8 +32,8 @@ public ToggleSneak() {
this.keyBinding = new KeyBinding();
this.toggleStatus = new ToggleStatus();
ModLoadingContext.get().registerExtensionPoint(
ConfigGuiHandler.ConfigGuiFactory.class,
() -> new ConfigGuiHandler.ConfigGuiFactory((minecraft, screen) -> new ConfigScreen())
ConfigScreenHandler.ConfigScreenFactory.class,
() -> new ConfigScreenHandler.ConfigScreenFactory((minecraft, screen) -> new ConfigScreen())
);
this.registerListeners();
}
Expand Down
112 changes: 52 additions & 60 deletions src/main/java/net/shotbow/ToggleSneak/gui/ConfigScreen.java
Original file line number Diff line number Diff line change
@@ -1,95 +1,87 @@
package net.shotbow.ToggleSneak.gui;

import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.CycleOption;
import net.minecraft.client.Minecraft;
import net.minecraft.client.ProgressOption;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.OptionsList;
import net.minecraft.client.gui.components.CycleButton;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraftforge.client.gui.widget.ForgeSlider;
import net.shotbow.ToggleSneak.object.ToggleConfig;
import org.jetbrains.annotations.NotNull;

/**
* Credit to <a href="https://leo3418.github.io/2021/03/31/forge-mod-config-screen-1-16.html">leo3418</a>
* for their great tutorial.
*/
public class ConfigScreen extends Screen {
private static final int TITLE_HEIGHT = 8;
private static final int OPTIONS_LIST_TOP_HEIGHT = 24;
private static final int OPTIONS_LIST_BOTTOM_OFFSET = 32;
private static final int OPTIONS_LIST_ITEM_HEIGHT = 25;
private static final int BUTTON_WIDTH = 200;
private static final int BUTTON_WIDTH = 150;
private static final int LONG_BUTTON_WIDTH = BUTTON_WIDTH * 2;
private static final int BUTTON_HEIGHT = 20;
private static final int DONE_BUTTON_TOP_OFFSET = 26;
private OptionsList optionsRowList;
private ForgeSlider releaseTimeSlider;

public ConfigScreen() {
super(new TranslatableComponent("configGui.title"));
super(Component.translatable("configGui.title"));
}

@Override
protected void init() {
this.optionsRowList = new OptionsList(
Minecraft.getInstance(), this.width, this.height,
OPTIONS_LIST_TOP_HEIGHT,
this.height - OPTIONS_LIST_BOTTOM_OFFSET,
OPTIONS_LIST_ITEM_HEIGHT
);
ToggleConfig toggleConfig = ToggleConfig.getInstance();
this.optionsRowList.addBig(CycleOption.createOnOff(
"configGui.option.toggleSneak",
(options) -> toggleConfig.getToggleSneak().get(),
(options, option, bool) -> toggleConfig.setToggleSneak(bool)
));
this.optionsRowList.addBig(CycleOption.createOnOff(
"configGui.option.toggleSprint",
(options) -> toggleConfig.getToggleSprint().get(),
(options, option, bool) -> toggleConfig.getToggleSprint().set(bool)
));
this.optionsRowList.addBig(CycleOption.createOnOff(
"configGui.option.toggleDisplay",
(options) -> toggleConfig.getToggleDisplay().get(),
(options, option, bool) -> toggleConfig.getToggleDisplay().set(bool)
));
this.optionsRowList.addBig(
new ProgressOption("configGui.option.shiftReleaseTime", 500D, 1000D, 1F,
(options) -> toggleConfig.getShiftReleaseTime().get(),
(options, value) -> toggleConfig.getShiftReleaseTime().set((double) (Math.round(value * 100) / 100)),
(options, progressOption) -> {
double value = progressOption.toPct(toggleConfig.getShiftReleaseTime().get());
return new TextComponent(
new TranslatableComponent("configGui.option.shiftReleaseTime").getString()
+ String.format(": %.0f", progressOption.toValue(value))
);
}
)
int screenWidth = this.width;
int screenHeight = this.height;
int buttonGap = 5;
int buttonX = (screenWidth - BUTTON_WIDTH * 2 - buttonGap) / 2; // center the two buttons horizontally
int buttonY = TITLE_HEIGHT + 30; // position the two buttons below the title
int sliderX = (screenWidth - BUTTON_WIDTH * 2) / 2; // center the slider horizontally
int sliderY = buttonY + BUTTON_HEIGHT + 10; // position the slider below the buttons
int doneButtonX = (screenWidth - LONG_BUTTON_WIDTH) / 2; // center the done button horizontally
int doneButtonY = screenHeight - 30; // position the done button near the bottom of the screen

this.addRenderableWidget(CycleButton.onOffBuilder(toggleConfig.getToggleSneak().get()).create(
buttonX,
buttonY,
BUTTON_WIDTH,
BUTTON_HEIGHT,
Component.translatable("configGui.option.toggleSneak"), (widget, value) -> toggleConfig.setToggleSneak(value))
);
this.addWidget(optionsRowList);
this.addRenderableWidget(new Button(
(this.width - BUTTON_WIDTH) / 2,
this.height - DONE_BUTTON_TOP_OFFSET,

this.addRenderableWidget(CycleButton.onOffBuilder(toggleConfig.getToggleSprint().get()).create(
buttonX + BUTTON_WIDTH + buttonGap,
buttonY,
BUTTON_WIDTH,
BUTTON_HEIGHT,
new TranslatableComponent("configGui.done"),
button -> this.onClose()
Component.translatable("configGui.option.toggleSprint"), (widget, value) -> toggleConfig.getToggleSprint().set(value))
);

this.addRenderableWidget(this.releaseTimeSlider = new ForgeSlider(
sliderX,
sliderY,
LONG_BUTTON_WIDTH,
BUTTON_HEIGHT,
Component.translatable("configGui.option.shiftReleaseTime").append(": "),
Component.empty(),
toggleConfig.getShiftReleaseMinimum(),
toggleConfig.getShiftReleaseMaximum(),
toggleConfig.getShiftReleaseTime().get(),
1,
0,
true
));

this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, btn -> onClose())
.bounds(doneButtonX, doneButtonY, LONG_BUTTON_WIDTH, BUTTON_HEIGHT)
.build());
}

@Override
public void render(@NotNull PoseStack poseStack, int mouseX, int mouseY, float ticks) {
this.renderBackground(poseStack);
this.optionsRowList.render(poseStack, mouseX, mouseY, ticks);
drawCenteredString(poseStack, this.font, this.title.getString(),
this.renderDirtBackground(poseStack);
drawCenteredString(poseStack, this.font, this.title,
this.width / 2, TITLE_HEIGHT, 0xFFFFFF);
super.render(poseStack, mouseX, mouseY, ticks);
}

@Override
public void onClose() {
super.onClose();
ToggleConfig.getInstance().getShiftReleaseTime().set(this.releaseTimeSlider.getValue());
ToggleConfig.save();
super.onClose();
}
}
17 changes: 8 additions & 9 deletions src/main/java/net/shotbow/ToggleSneak/gui/StatusDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGuiOverlayEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.shotbow.ToggleSneak.ToggleSneak;
import net.shotbow.ToggleSneak.object.ToggleConfig;
Expand All @@ -14,25 +14,24 @@
public class StatusDisplay {

@SubscribeEvent
public void render(RenderGameOverlayEvent.Post e) {
public void render(RenderGuiOverlayEvent.Post e) {
ToggleConfig config = ToggleConfig.getInstance();
Minecraft minecraft = ToggleSneak.getToggleSneak().getMinecraft();
if (e.getType() != RenderGameOverlayEvent.ElementType.ALL
|| minecraft.player == null
if (minecraft.player == null
|| !config.getToggleDisplay().get()) {
return;
}
ToggleStatus status = ToggleSneak.getToggleSneak().getToggleStatus();
int midPoint = e.getWindow().getGuiScaledHeight() / 2;
if(config.getToggleSneak().get()) {
// Render sneaking display
MutableComponent sneakingDisplayText = new TranslatableComponent("displayGui.sneaking")
MutableComponent sneakingDisplayText = Component.translatable("displayGui.sneaking")
.withStyle(ChatFormatting.WHITE);
if (status.isSneakingToggled()) {
sneakingDisplayText.withStyle(ChatFormatting.GOLD);
}
GuiComponent.drawString(
e.getMatrixStack(),
e.getPoseStack(),
minecraft.font,
sneakingDisplayText,
1, //X
Expand All @@ -42,14 +41,14 @@ public void render(RenderGameOverlayEvent.Post e) {
}
if(config.getToggleSprint().get()) {
// Render sprinting display
MutableComponent sprintingDisplayText = new TranslatableComponent("displayGui.sprinting")
MutableComponent sprintingDisplayText = Component.translatable("displayGui.sprinting")
.withStyle(ChatFormatting.GOLD);
int yHeight = midPoint;
if(config.getToggleSneak().get()){
yHeight += 10;
}
GuiComponent.drawString(
e.getMatrixStack(),
e.getPoseStack(),
minecraft.font,
sprintingDisplayText,
1, //X
Expand Down
38 changes: 25 additions & 13 deletions src/main/java/net/shotbow/ToggleSneak/keyboard/KeyBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,41 @@
import com.mojang.blaze3d.platform.InputConstants;
import lombok.Getter;
import net.minecraft.client.KeyMapping;
import net.minecraftforge.client.ClientRegistry;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.RegisterKeyMappingsEvent;
import net.minecraftforge.client.settings.KeyConflictContext;
import net.minecraftforge.client.settings.KeyModifier;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.shotbow.ToggleSneak.ToggleSneak;

@OnlyIn(Dist.CLIENT)
public class KeyBinding {

private final String toggleSneakCategory = ToggleSneak.MOD_TITLE;
private @Getter KeyMapping toggleSneakKey;
private @Getter KeyMapping toggleSprintKey;

public KeyBinding(){
KeyMapping.createNameSupplier(toggleSneakCategory);
registerKeys();
FMLJavaModLoadingContext.get().getModEventBus().addListener(
EventPriority.NORMAL,
false,
RegisterKeyMappingsEvent.class, e -> {
e.register(this.toggleSneakKey = getKeyMapping("toggle.sneak", InputConstants.KEY_G));
e.register(this.toggleSprintKey = getKeyMapping("toggle.sprint", InputConstants.KEY_H));
}
);
}

private void registerKeys() {
this.toggleSneakKey = registerMapping("toggle.sneak", InputConstants.KEY_G);
this.toggleSprintKey = registerMapping("toggle.sprint", InputConstants.KEY_H);
}

private KeyMapping registerMapping(String key, int keycode){
final KeyMapping keyMapping = new KeyMapping("keybinding." + key, keycode, toggleSneakCategory);
ClientRegistry.registerKeyBinding(keyMapping);
return keyMapping;
private KeyMapping getKeyMapping(String key, int keycode){
return new KeyMapping(
"keybinding." + key,
KeyConflictContext.IN_GAME,
KeyModifier.NONE,
InputConstants.Type.KEYSYM,
keycode,
ToggleSneak.MOD_TITLE
);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package net.shotbow.ToggleSneak.listeners;

import net.minecraft.client.KeyMapping;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.shotbow.ToggleSneak.ToggleSneak;
import net.shotbow.ToggleSneak.keyboard.KeyBinding;
import net.shotbow.ToggleSneak.object.ToggleConfig;

public class KeyboardListener {
Expand All @@ -12,21 +14,24 @@ public void keyPress(TickEvent.ClientTickEvent e){
if(e.phase != TickEvent.Phase.END)
return;
//Handle toggling of options
KeyBinding keyBinding = ToggleSneak.getToggleSneak().getKeyBinding();
ToggleConfig config = ToggleConfig.getInstance();
ToggleSneak toggleSneak = ToggleSneak.getToggleSneak();
if(toggleSneak.getKeyBinding().getToggleSneakKey().isDown()
&& toggleSneak.getKeyBinding().getToggleSneakKey().consumeClick()){
if(keyBinding.getToggleSneakKey() != null && isPressed(keyBinding.getToggleSneakKey())){
//Toggle Sneak pressed
final boolean setTo = !config.getToggleSneak().get();
ToggleConfig.getInstance().setToggleSneak(setTo);
}
if(toggleSneak.getKeyBinding().getToggleSprintKey().isDown()
&& toggleSneak.getKeyBinding().getToggleSprintKey().consumeClick()){
if(keyBinding.getToggleSprintKey() != null && isPressed(keyBinding.getToggleSprintKey())){
//Toggle Sprint pressed
final boolean setTo = !config.getToggleSprint().get();
ToggleConfig.getInstance().getToggleSprint().set(setTo);
}
}

private boolean isPressed(KeyMapping mapping){
return mapping.isDown()
&& mapping.getKeyConflictContext().isActive()
&& mapping.consumeClick();
}

}
Loading

0 comments on commit e378650

Please sign in to comment.