Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ public static void toggleSprint() {
sprintToggled = !sprintToggled;
}

public static Player.KeyInput getKeyInput() {
return Interface.get().map(Interface::getKeyInput).orElseGet(() -> {
API.LOGGER.info(API.COMPATIBILITY_MARKER, "Failed to get inputs, are you playing on an unsupported minecraft version?");
return null;
});
}


public enum WorldState {
MENU,
Expand Down Expand Up @@ -165,5 +172,7 @@ static Optional<Interface> get() {
boolean isF3Enabled();

void sendPacket(MPKPacket packet);

Player.KeyInput getKeyInput();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public Float getLast45() {
}

public Player constructKeyInput() {
keyInput = KeyInput.construct();
keyInput = Minecraft.getKeyInput();
return this;
}

Expand Down Expand Up @@ -511,9 +511,7 @@ public static class KeyInput {
public boolean sneak = false;
public boolean jump = false;

public KeyInput() {

}
public KeyInput() {}

public KeyInput(boolean forward, boolean left, boolean back, boolean right, boolean sprint, boolean sneak, boolean jump) {
this.forward = forward;
Expand All @@ -525,19 +523,6 @@ public KeyInput(boolean forward, boolean left, boolean back, boolean right, bool
this.jump = jump;
}

public static KeyInput construct() {
KeyInput k = new KeyInput();
for (Field f : KeyInput.class.getDeclaredFields()) {
KeyBinding b = KeyBinding.getByName("key." + f.getName());
if (b == null) continue;
try {
f.set(k, b.isKeyDown());
} catch (IllegalAccessException ignored) {
}
}
return k;
}

public String toString() {
return "{W:" + forward + ", A:" + left + ", S:" + back + ", D:" + right + ", N:" + sneak + ", P:" + sprint + ", J:" + jump + "}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.util.Util;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;

Expand Down Expand Up @@ -135,25 +136,30 @@ public void onClientTickStart(Minecraft mc) {

public void onClientTickEnd(Minecraft mc) {
if (mc.isPaused() || mc.level == null) return;
LocalPlayer mcPlayer = mc.player;

if (mcPlayer != null) {
AABB playerBB = mcPlayer.getBoundingBox();
new Player()
.setPos(new Vector3D(mcPlayer.getX(), mcPlayer.getY(), mcPlayer.getZ()))
.setLastPos(new Vector3D(mcPlayer.xo, mcPlayer.yo, mcPlayer.zo))
.setMotion(new Vector3D(mcPlayer.getDeltaMovement().x, mcPlayer.getDeltaMovement().y, mcPlayer.getDeltaMovement().z))
.setRotation(mcPlayer.getRotationVector().y, mcPlayer.getRotationVector().x)
.setOnGround(mcPlayer.onGround())
.setSprinting(mcPlayer.isSprinting())
Entity cameraEntity = mc.getCameraEntity();

if (cameraEntity != null) {
AABB cameraEntityBB = cameraEntity.getBoundingBox();
Player mpkPlayer = new Player()
.setPos(new Vector3D(cameraEntity.getX(), cameraEntity.getY(), cameraEntity.getZ()))
.setLastPos(new Vector3D(cameraEntity.xo, cameraEntity.yo, cameraEntity.zo))
.setMotion(new Vector3D(cameraEntity.getDeltaMovement().x, cameraEntity.getDeltaMovement().y, cameraEntity.getDeltaMovement().z))
.setRotation(cameraEntity.getRotationVector().y, cameraEntity.getRotationVector().x)
.setOnGround(cameraEntity.onGround())
.setSprinting(cameraEntity.isSprinting())
.setBoundingBox(new BoundingBox3D(
new Vector3D(playerBB.minX, playerBB.minY, playerBB.minZ),
new Vector3D(playerBB.maxX, playerBB.maxY, playerBB.maxZ)
new Vector3D(cameraEntityBB.minX, cameraEntityBB.minY, cameraEntityBB.minZ),
new Vector3D(cameraEntityBB.maxX, cameraEntityBB.maxY, cameraEntityBB.maxZ)
))
.setFlying(mcPlayer.getAbilities().flying)
.constructKeyInput()
.setKeyMSList(timeQueue)
.buildAndSave();
.setKeyMSList(timeQueue);

if (cameraEntity instanceof net.minecraft.world.entity.player.Player player) {
mpkPlayer.setFlying(player.getAbilities().flying);
}

mpkPlayer.buildAndSave();

timeQueue.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import net.minecraft.util.ARGB;
import net.minecraft.util.Mth;
import net.minecraft.util.Util;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.BlockHitResult;
Expand Down Expand Up @@ -373,6 +375,35 @@ public void endSection() {
net.minecraft.util.profiling.Profiler.get().pop();
}

@Override
public Player.KeyInput getKeyInput() {
Entity cameraEntity = net.minecraft.client.Minecraft.getInstance().getCameraEntity();
if (!(cameraEntity instanceof LivingEntity le)) return new Player.KeyInput();

boolean w, a, s, d, sprint, sneak, jump;
if (cameraEntity instanceof LocalPlayer lp) {
w = lp.input.keyPresses.forward();
a = lp.input.keyPresses.left();
s = lp.input.keyPresses.backward();
d = lp.input.keyPresses.right();

sprint = lp.input.keyPresses.sprint();
sneak = lp.input.keyPresses.shift();
jump = lp.input.keyPresses.jump();
} else {
w = le.zza > 0;
a = le.xxa > 0;
s = le.zza < 0;
d = le.xxa < 0;

sprint = le.isSprinting();
sneak = le.isShiftKeyDown();
jump = le.isJumping();
}

return new Player.KeyInput(w, a, s, d, sprint, sneak, jump);
}

private record PointsRenderState(
Collection<Vector2D> points,
int r, int g, int b, int a,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import io.github.kurrycat.mpkmod.util.Mouse;
import io.github.kurrycat.mpkmod.util.Vector3D;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.settings.GameSettings;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraftforge.client.event.MouseEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
Expand Down Expand Up @@ -102,28 +103,33 @@ private void checkKeyBinding(int keyCode) {
public void onTick(TickEvent.ClientTickEvent e) {
Minecraft mc = Minecraft.getMinecraft();
if (mc.isGamePaused() || mc.world == null) return;
EntityPlayerSP mcPlayer = mc.player;
Entity cameraEntity = mc.getRenderViewEntity();

if (e.type != TickEvent.Type.CLIENT) return;
if (e.side != Side.CLIENT) return;

if (mcPlayer != null && e.phase == TickEvent.Phase.END) {
AxisAlignedBB playerBB = mcPlayer.getEntityBoundingBox();
new Player()
.setPos(new Vector3D(mcPlayer.posX, mcPlayer.posY, mcPlayer.posZ))
.setLastPos(new Vector3D(mcPlayer.lastTickPosX, mcPlayer.lastTickPosY, mcPlayer.lastTickPosZ))
.setMotion(new Vector3D(mcPlayer.motionX, mcPlayer.motionY, mcPlayer.motionZ))
.setRotation(mcPlayer.rotationYaw, mcPlayer.rotationPitch)
.setOnGround(mcPlayer.onGround)
.setSprinting(mcPlayer.isSprinting())
if (cameraEntity != null && e.phase == TickEvent.Phase.END) {
AxisAlignedBB cameraEntityBB = cameraEntity.getEntityBoundingBox();
Player mpkPlayer = new Player()
.setPos(new Vector3D(cameraEntity.posX, cameraEntity.posY, cameraEntity.posZ))
.setLastPos(new Vector3D(cameraEntity.lastTickPosX, cameraEntity.lastTickPosY, cameraEntity.lastTickPosZ))
.setMotion(new Vector3D(cameraEntity.motionX, cameraEntity.motionY, cameraEntity.motionZ))
.setRotation(cameraEntity.rotationYaw, cameraEntity.rotationPitch)
.setOnGround(cameraEntity.onGround)
.setSprinting(cameraEntity.isSprinting())
.setBoundingBox(new BoundingBox3D(
new Vector3D(playerBB.minX, playerBB.minY, playerBB.minZ),
new Vector3D(playerBB.maxX, playerBB.maxY, playerBB.maxZ)
new Vector3D(cameraEntityBB.minX, cameraEntityBB.minY, cameraEntityBB.minZ),
new Vector3D(cameraEntityBB.maxX, cameraEntityBB.maxY, cameraEntityBB.maxZ)
))
.setFlying(mcPlayer.capabilities.isFlying)
.constructKeyInput()
.setKeyMSList(timeQueue.copy())
.buildAndSave();
.setKeyMSList(timeQueue.copy());

if (cameraEntity instanceof EntityPlayer) {
mpkPlayer.setFlying(((EntityPlayer) cameraEntity).capabilities.isFlying);
}

mpkPlayer.buildAndSave();

timeQueue.clear();
}
if (e.phase == TickEvent.Phase.START) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.minecraft.client.settings.GameSettings;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.init.SoundEvents;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
Expand All @@ -30,6 +31,7 @@
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.lang.reflect.Field;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -464,4 +466,44 @@ public void endStartSection(String name) {
public void endSection() {
Minecraft.getMinecraft().profiler.endSection();
}

@Override
public Player.KeyInput getKeyInput() {
Entity ce = Minecraft.getMinecraft().getRenderViewEntity();
if (!(ce instanceof EntityLivingBase)) return new Player.KeyInput();

boolean w, a, s, d, sprint, sneak, jump;
if (ce instanceof EntityPlayerSP) {
GameSettings gs = Minecraft.getMinecraft().gameSettings;

w = gs.keyBindForward.isKeyDown();
a = gs.keyBindLeft.isKeyDown();
s = gs.keyBindBack.isKeyDown();
d = gs.keyBindRight.isKeyDown();

sprint = gs.keyBindSprint.isKeyDown();
sneak = gs.keyBindSneak.isKeyDown();
jump = gs.keyBindJump.isKeyDown();
} else {
EntityLivingBase elb = (EntityLivingBase) ce;

w = elb.moveForward > 0;
a = elb.moveStrafing > 0;
s = elb.moveForward < 0;
d = elb.moveStrafing < 0;

sprint = elb.isSprinting();
sneak = elb.isSneaking();

jump = false;
try {
Field isJumpingField = EntityLivingBase.class.getDeclaredField("isJumping");
isJumpingField.setAccessible(true);

jump = isJumpingField.getBoolean(elb);
} catch (NoSuchFieldException | IllegalAccessException ignored) {}
}

return new Player.KeyInput(w, a, s, d, sprint, sneak, jump);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import io.github.kurrycat.mpkmod.util.Mouse;
import io.github.kurrycat.mpkmod.util.Vector3D;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.settings.GameSettings;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.client.event.MouseEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
Expand Down Expand Up @@ -102,28 +103,33 @@ private void checkKeyBinding(int keyCode) {
public void onTick(TickEvent.ClientTickEvent e) {
Minecraft mc = Minecraft.getMinecraft();
if (mc.isGamePaused() || mc.theWorld == null) return;
EntityPlayerSP mcPlayer = mc.thePlayer;
Entity cameraEntity = mc.getRenderViewEntity();

if (e.type != TickEvent.Type.CLIENT) return;
if (e.side != Side.CLIENT) return;

if (mcPlayer != null && e.phase == TickEvent.Phase.END) {
AxisAlignedBB playerBB = mcPlayer.getEntityBoundingBox();
new Player()
.setPos(new Vector3D(mcPlayer.posX, mcPlayer.posY, mcPlayer.posZ))
.setLastPos(new Vector3D(mcPlayer.lastTickPosX, mcPlayer.lastTickPosY, mcPlayer.lastTickPosZ))
.setMotion(new Vector3D(mcPlayer.motionX, mcPlayer.motionY, mcPlayer.motionZ))
.setRotation(mcPlayer.rotationYaw, mcPlayer.rotationPitch)
.setOnGround(mcPlayer.onGround)
.setSprinting(mcPlayer.isSprinting())
if (cameraEntity != null && e.phase == TickEvent.Phase.END) {
AxisAlignedBB cameraEntityBB = cameraEntity.getEntityBoundingBox();
Player mpkPlayer = new Player()
.setPos(new Vector3D(cameraEntity.posX, cameraEntity.posY, cameraEntity.posZ))
.setLastPos(new Vector3D(cameraEntity.lastTickPosX, cameraEntity.lastTickPosY, cameraEntity.lastTickPosZ))
.setMotion(new Vector3D(cameraEntity.motionX, cameraEntity.motionY, cameraEntity.motionZ))
.setRotation(cameraEntity.rotationYaw, cameraEntity.rotationPitch)
.setOnGround(cameraEntity.onGround)
.setSprinting(cameraEntity.isSprinting())
.setBoundingBox(new BoundingBox3D(
new Vector3D(playerBB.minX, playerBB.minY, playerBB.minZ),
new Vector3D(playerBB.maxX, playerBB.maxY, playerBB.maxZ)
new Vector3D(cameraEntityBB.minX, cameraEntityBB.minY, cameraEntityBB.minZ),
new Vector3D(cameraEntityBB.maxX, cameraEntityBB.maxY, cameraEntityBB.maxZ)
))
.setFlying(mcPlayer.capabilities.isFlying)
.constructKeyInput()
.setKeyMSList(timeQueue.copy())
.buildAndSave();
.setKeyMSList(timeQueue.copy());

if (cameraEntity instanceof EntityPlayer) {
mpkPlayer.setFlying(((EntityPlayer) cameraEntity).capabilities.isFlying);
}

mpkPlayer.buildAndSave();

timeQueue.clear();
}
if (e.phase == TickEvent.Phase.START) {
Expand Down
Loading
Loading