1
0
mirror of https://github.com/chylex/Better-Sprinting.git synced 2025-04-09 18:15:41 +02:00

Rework injected tick logic to remove vanilla code from PlayerLogicHandler

This commit is contained in:
chylex 2019-07-01 01:51:47 +02:00
parent bae75d5287
commit 6481ba452c
4 changed files with 117 additions and 119 deletions
src/main
java/chylex/bettersprinting/client
resources/coremods

View File

@ -14,6 +14,7 @@ import net.minecraft.client.gui.widget.list.KeyBindingList.CategoryEntry;
import net.minecraft.client.gui.widget.list.KeyBindingList.KeyEntry;
import net.minecraft.client.resources.I18n;
import net.minecraft.client.settings.AbstractOption;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.GuiOpenEvent;
@ -24,6 +25,8 @@ import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.client.config.GuiButtonExt;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import org.apache.commons.lang3.ArrayUtils;
@OnlyIn(Dist.CLIENT)
@ -61,6 +64,22 @@ public final class ClientEventHandler{
}
}
@SubscribeEvent
public static void onClientTick(ClientTickEvent e){
if (e.phase != Phase.END || mc.player == null){
return;
}
if (ClientModManager.showDisableWarningWhenPossible){
mc.player.sendMessage(new StringTextComponent(ClientModManager.chatPrefix + I18n.format(ClientModManager.isModDisabledByServer() ? "bs.game.disabled" : "bs.game.reenabled")));
ClientModManager.showDisableWarningWhenPossible = false;
}
if (ClientModManager.keyBindOptionsMenu.isKeyDown()){
mc.displayGuiScreen(new GuiSprint(null));
}
}
@SubscribeEvent
public static void onGuiInitPost(GuiScreenEvent.InitGuiEvent.Post e){
Screen gui = e.getGui();

View File

@ -1,17 +1,14 @@
package chylex.bettersprinting.client.player;
import chylex.bettersprinting.client.ClientModManager;
import chylex.bettersprinting.client.ClientSettings;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public final class LivingUpdate{
private static final Minecraft mc = Minecraft.getInstance();
private static PlayerLogicHandler currentHandler;
private static boolean hasTriggered;
private static boolean isModDisabled;
public static boolean checkIntegrity(){
return hasTriggered;
@ -24,69 +21,64 @@ public final class LivingUpdate{
// UPDATE | ClientPlayerEntity.livingTick | 1.14.3
public static void injectMovementInputUpdate(ClientPlayerEntity $this, boolean slowMovement, boolean isSpectator){
hasTriggered = true;
isModDisabled = ClientModManager.isModDisabled();
// this.movementInput.func_217607_a(flag3, this.isSpectator()); <<< REPLACE
if (isModDisabled){
$this.movementInput.func_217607_a(slowMovement, isSpectator);
currentHandler = null;
return;
}
if (currentHandler == null || currentHandler.getPlayer() != $this){
currentHandler = new PlayerLogicHandler($this);
hasTriggered = true;
}
if (mc.playerController.isInCreativeMode() && $this.abilities.isFlying && ClientModManager.canFlyOnGround() && ClientSettings.flyOnGround.get()){
$this.onGround = false;
}
/*
this.movementInput.func_217607_a(flag3, this.isSpectator()); <<< REPLACE
*/
currentHandler.updateMovementInput(slowMovement, isSpectator);
}
// UPDATE | ClientPlayerEntity.livingTick | 1.14.3
public static void injectSprinting(ClientPlayerEntity $this){
/*
this.pushOutOfBlocks(this.posX + (double)this.getWidth() * 0.35D, axisalignedbb.minY + 0.5D, this.posZ + (double)this.getWidth() * 0.35D);
}
public static boolean injectSprinting(){
if (isModDisabled){
return false;
}
/*
}
<<< INSERTED HERE
boolean flag7 = (float)this.getFoodStats().getFoodLevel() > 6.0F || this.abilities.allowFlying;
if ((this.onGround || this.canSwim()) && !flag1 && !flag2 && this.func_223110_ee() && !this.isSprinting() && flag7 && !this.isHandActive() && !this.isPotionActive(Effects.BLINDNESS)) {
*/
currentHandler.updateLiving();
currentHandler.updateSprinting();
return true;
/*
this.setSprinting(false);
}
}
<<< SKIPPED TO HERE
if (this.abilities.allowFlying) {
*/
}
// UPDATE | ClientPlayerEntity.livingTick | 1.14.3
public static void injectAfterSuperCall(ClientPlayerEntity $this){
public static boolean injectAfterSuperCall(){
if (isModDisabled){
return false;
}
/*
super.livingTick();
<<< INSERTED HERE
if (this.onGround && this.abilities.isFlying && !this.mc.playerController.isSpectatorMode()) {
*/
if ($this.onGround && $this.abilities.isFlying && !mc.playerController.isSpectatorMode()){
boolean shouldFlyOnGround = mc.playerController.isInCreativeMode() && ClientModManager.canFlyOnGround() && ClientSettings.flyOnGround.get();
if (!shouldFlyOnGround){
$this.abilities.isFlying = false;
$this.sendPlayerAbilities();
}
}
currentHandler.updateFlight();
return true;
/*
this.sendPlayerAbilities();
}
<<< SKIPPED TO HERE
*/
}

View File

@ -1,14 +1,11 @@
package chylex.bettersprinting.client.player;
import chylex.bettersprinting.client.ClientModManager;
import chylex.bettersprinting.client.ClientSettings;
import chylex.bettersprinting.client.gui.GuiSprint;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.PlayerAbilities;
import net.minecraft.potion.Effects;
import net.minecraft.util.MovementInput;
import net.minecraft.util.text.StringTextComponent;
final class PlayerLogicHandler{
private static final Minecraft mc = Minecraft.getInstance();
@ -37,76 +34,64 @@ final class PlayerLogicHandler{
// UPDATE | ClientPlayerEntity.livingTick | 1.14.3
public void updateMovementInput(boolean slowMovement, boolean isSpectator){
if (mc.playerController.isInCreativeMode() && abilities.isFlying && ClientModManager.canFlyOnGround() && ClientSettings.flyOnGround.get()){
player.onGround = false;
}
wasSneaking = movementInput.sneak;
wasMovingForward = player.func_223110_ee();
movementController.update(slowMovement, isSpectator);
}
// UPDATE | ClientPlayerEntity.livingTick | 1.14.3
public void updateLiving(){
public void updateSprinting(){
boolean enoughHunger = player.getFoodStats().getFoodLevel() > 6F || abilities.allowFlying;
boolean isSprintBlocked = player.isHandActive() || player.isPotionActive(Effects.BLINDNESS);
if (ClientModManager.isModDisabled()){
if ((player.onGround || player.canSwim()) && !wasSneaking && !wasMovingForward && player.func_223110_ee() && !player.isSprinting() && enoughHunger && !isSprintBlocked){
if (player.sprintToggleTimer <= 0 && !ClientModManager.keyBindSprintHold.isKeyDown()){
player.sprintToggleTimer = 7;
}
else{
player.setSprinting(true);
}
boolean prevHeld = isHeld;
boolean sprint = movementController.sprint && !(movementInput.sneak && !abilities.isFlying);
boolean dblTap = ClientSettings.enableDoubleTap.get();
if ((!dblTap || !player.isSprinting()) && (player.onGround || player.canSwim()) && enoughHunger && !isSprintBlocked){
player.setSprinting(sprint);
}
isHeld = sprint;
if (dblTap && !isHeld && stopTimer == 0 && (player.onGround || player.canSwim()) && !wasSneaking && !wasMovingForward && player.func_223110_ee() && !player.isSprinting() && enoughHunger && !isSprintBlocked){
if (player.sprintToggleTimer <= 0){
player.sprintToggleTimer = 7;
}
if (!player.isSprinting() && (!player.isInWater() || player.canSwim()) && player.func_223110_ee() && enoughHunger && !isSprintBlocked && ClientModManager.keyBindSprintHold.isKeyDown()){
else{
player.setSprinting(true);
player.sprintToggleTimer = 0;
}
}
else{
boolean prevHeld = isHeld;
boolean sprint = movementController.sprint && !(movementInput.sneak && !abilities.isFlying);
boolean dblTap = ClientSettings.enableDoubleTap.get();
if ((!dblTap || !player.isSprinting()) && (player.onGround || player.canSwim()) && enoughHunger && !isSprintBlocked){
player.setSprinting(sprint);
if (dblTap){
if (prevHeld && !isHeld){
stopTimer = 1;
}
isHeld = sprint;
if (dblTap && !isHeld && stopTimer == 0 && (player.onGround || player.canSwim()) && !wasSneaking && !wasMovingForward && player.func_223110_ee() && !player.isSprinting() && enoughHunger && !isSprintBlocked){
if (player.sprintToggleTimer <= 0){
player.sprintToggleTimer = 7;
}
else{
player.setSprinting(true);
player.sprintToggleTimer = 0;
}
if (stopTimer > 0){
stopTimer--;
player.setSprinting(false);
}
if (dblTap){
if (prevHeld && !isHeld){
stopTimer = 1;
}
if (stopTimer > 0){
stopTimer--;
player.setSprinting(false);
}
}
int flySpeedBoostMultiplier = ClientSettings.flySpeedBoost.get();
if (flySpeedBoostMultiplier > 0){
if (sprint && abilities.isFlying && ClientModManager.canBoostFlying()){
abilities.setFlySpeed(0.05F + 0.075F * flySpeedBoostMultiplier);
}
int flySpeedBoostMultiplier = ClientSettings.flySpeedBoost.get();
if (flySpeedBoostMultiplier > 0){
if (sprint && abilities.isFlying && ClientModManager.canBoostFlying()){
abilities.setFlySpeed(0.05F + 0.075F * flySpeedBoostMultiplier);
}
else{
abilities.setFlySpeed(0.05F);
}
}
else if (abilities.getFlySpeed() > 0.05F){
else{
abilities.setFlySpeed(0.05F);
}
}
else if (abilities.getFlySpeed() > 0.05F){
abilities.setFlySpeed(0.05F);
}
if (player.isSprinting()){
boolean isSlow = (ClientModManager.canRunInAllDirs() && ClientSettings.enableAllDirs.get()) ? !movementController.isMovingAnywhere() : !movementInput.func_223135_b();
@ -123,18 +108,17 @@ final class PlayerLogicHandler{
player.setSprinting(false);
}
}
postLogic();
}
private void postLogic(){
if (ClientModManager.showDisableWarningWhenPossible){
player.sendMessage(new StringTextComponent(ClientModManager.chatPrefix + I18n.format(ClientModManager.isModDisabledByServer() ? "bs.game.disabled" : "bs.game.reenabled")));
ClientModManager.showDisableWarningWhenPossible = false;
}
if (ClientModManager.keyBindOptionsMenu.isKeyDown()){
mc.displayGuiScreen(new GuiSprint(null));
// UPDATE | ClientPlayerEntity.livingTick | 1.14.3
public void updateFlight(){
if (player.onGround && abilities.isFlying && !mc.playerController.isSpectatorMode()){
boolean shouldFlyOnGround = mc.playerController.isInCreativeMode() && ClientModManager.canFlyOnGround() && ClientSettings.flyOnGround.get();
if (!shouldFlyOnGround){
abilities.isFlying = false;
player.sendPlayerAbilities();
}
}
}
}

View File

@ -290,26 +290,31 @@ function initializeCoreMod(){
print("Transforming livingTick (movement update)");
var instructions = method.instructions;
var entry = null;
for(var index = 0, instrcount = instructions.size(); index < instrcount; index++){
if (checkInstruction(instructions.get(index), opcodes.INVOKEVIRTUAL, "func_217607_a") &&
checkOpcodeChain(instructions, index - 5, [ opcodes.ALOAD, opcodes.GETFIELD, opcodes.ILOAD, opcodes.ALOAD ])
){
print("Found entry point at " + index + ".");
var toRemove = instructions.get(index - 4);
var toReplace = instructions.get(index);
var helper = api.getMethodNode();
helper.visitMethodInsn(opcodes.INVOKESTATIC, "chylex/bettersprinting/client/player/LivingUpdate", "injectMovementInputUpdate", "(Lnet/minecraft/client/entity/player/ClientPlayerEntity;ZZ)V", false);
instructions.remove(toRemove);
instructions.set(toReplace, helper.instructions.get(0));
return true;
entry = index;
break;
}
}
return false;
if (entry === null){
return false;
}
print("Found entry point at " + index + ".");
var call = api.buildMethodCall("chylex/bettersprinting/client/player/LivingUpdate", "injectMovementInputUpdate", "(Lnet/minecraft/client/entity/player/ClientPlayerEntity;ZZ)V", api.MethodType.STATIC);
var toRemove = instructions.get(index - 4);
var toReplace = instructions.get(index);
instructions.remove(toRemove);
instructions.set(toReplace, call);
return true;
};
var transformSprinting = function(method){
@ -341,12 +346,11 @@ function initializeCoreMod(){
if (!validateLabels(labels)){
return false;
}
var helper = api.getMethodNode();
helper.visitVarInsn(opcodes.ALOAD, 0);
helper.visitMethodInsn(opcodes.INVOKESTATIC, "chylex/bettersprinting/client/player/LivingUpdate", "injectSprinting", "(Lnet/minecraft/client/entity/player/ClientPlayerEntity;)V", false);
helper.visitJumpInsn(opcodes.GOTO, getSkipInst(labels[1]));
helper.visitMethodInsn(opcodes.INVOKESTATIC, "chylex/bettersprinting/client/player/LivingUpdate", "injectSprinting", "()Z", false);
helper.visitJumpInsn(opcodes.IFNE, getSkipInst(labels[1]));
instructions.insert(labels[0], helper.instructions);
return true;
};
@ -379,10 +383,9 @@ function initializeCoreMod(){
}
var helper = api.getMethodNode();
helper.visitVarInsn(opcodes.ALOAD, 0);
helper.visitMethodInsn(opcodes.INVOKESTATIC, "chylex/bettersprinting/client/player/LivingUpdate", "injectAfterSuperCall", "(Lnet/minecraft/client/entity/player/ClientPlayerEntity;)V", false);
helper.visitJumpInsn(opcodes.GOTO, getSkipInst(labels[1]));
helper.visitMethodInsn(opcodes.INVOKESTATIC, "chylex/bettersprinting/client/player/LivingUpdate", "injectAfterSuperCall", "()Z", false);
helper.visitJumpInsn(opcodes.IFNE, getSkipInst(labels[1]));
instructions.insert(labels[0], helper.instructions);
return true;
};