Skip to content

Commit

Permalink
Fix movement stance calculation (1.7.2 works!)
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkStorm652 committed Apr 13, 2014
1 parent 09c7bdc commit 073081d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.darkstorm.darkbot.minecraftbot.event.protocol.client;

import org.darkstorm.darkbot.minecraftbot.event.AbstractEvent;
import org.darkstorm.darkbot.minecraftbot.event.AbstractCancellableEvent;
import org.darkstorm.darkbot.minecraftbot.world.item.Inventory;

public abstract class InventoryEvent extends AbstractEvent {
public abstract class InventoryEvent extends AbstractCancellableEvent {
private final Inventory inventory;

public InventoryEvent(Inventory inventory) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.darkstorm.darkbot.minecraftbot.protocol.v4x;

import java.io.*;
import java.math.BigInteger;
import java.math.*;
import java.security.*;
import java.util.*;

Expand Down Expand Up @@ -67,11 +67,14 @@ public byte[] getData() {
public static final int VERSION = 4;
public static final String VERSION_NAME = "1.7.2";

private static final double STANCE_CONSTANT = 1.62000000476837;
private static final double STANCE_CONSTANT = 1.620000004768372;
private static final BigDecimal STANCE_CONSTANT_PRECISE = BigDecimal.valueOf(STANCE_CONSTANT);

private final MinecraftBot bot;
private final Map<String, String> lang;

private boolean positionSet = false;

public Protocol4X(MinecraftBot bot) {
super(VERSION);
this.bot = bot;
Expand Down Expand Up @@ -179,15 +182,21 @@ public void onInventoryClose(InventoryCloseEvent event) {

@EventHandler
public void onHeldItemDrop(HeldItemDropEvent event) {
ConnectionHandler handler = bot.getConnectionHandler();
C07PacketBlockDig.Action action = event.isEntireStack() ? C07PacketBlockDig.Action.DROP_ITEM_STACK : C07PacketBlockDig.Action.DROP_ITEM;
handler.sendPacket(new C07PacketBlockDig(action, 0, 0, 0, 0));
if(positionSet) {
ConnectionHandler handler = bot.getConnectionHandler();
C07PacketBlockDig.Action action = event.isEntireStack() ? C07PacketBlockDig.Action.DROP_ITEM_STACK : C07PacketBlockDig.Action.DROP_ITEM;
handler.sendPacket(new C07PacketBlockDig(action, 0, 0, 0, 0));
} else
event.setCancelled(true);
}

@EventHandler
public void onHeldItemChange(HeldItemChangeEvent event) {
ConnectionHandler handler = bot.getConnectionHandler();
handler.sendPacket(new C09PacketHeldItemChange(event.getNewSlot()));
if(positionSet) {
ConnectionHandler handler = bot.getConnectionHandler();
handler.sendPacket(new C09PacketHeldItemChange(event.getNewSlot()));
} else
event.setCancelled(true);
}

@EventHandler
Expand Down Expand Up @@ -261,14 +270,15 @@ public void onBlockPlace(BlockPlaceEvent event) {
public void onPlayerUpdate(PlayerUpdateEvent event) {
MainPlayerEntity player = event.getEntity();
double x = player.getX(), y = player.getY(), z = player.getZ(), yaw = player.getYaw(), pitch = player.getPitch();
double eyeY = BigDecimal.valueOf(y).add(STANCE_CONSTANT_PRECISE).doubleValue();
boolean move = x != player.getLastX() || y != player.getLastY() || z != player.getLastZ();
boolean rotate = yaw != player.getLastYaw() || pitch != player.getLastPitch();
boolean onGround = player.isOnGround();
C03PacketPlayerUpdate packet;
if(move && rotate)
packet = new C06PacketPositionRotationUpdate(x, y, y + STANCE_CONSTANT, z, yaw, pitch, onGround);
packet = new C06PacketPositionRotationUpdate(x, y, z, eyeY, yaw, pitch, onGround);
else if(move)
packet = new C04PacketPositionUpdate(x, y, y + STANCE_CONSTANT, z, onGround);
packet = new C04PacketPositionUpdate(x, y, z, eyeY, onGround);
else if(rotate)
packet = new C05PacketRotationUpdate(yaw, pitch, onGround);
else
Expand Down Expand Up @@ -385,6 +395,11 @@ public void onPacketProcess(PacketProcessEvent event) {
256,
joinPacket.getMaxPlayers()));
connectionHandler.sendPacket(new C15PacketClientSettings("en_US", ViewDistance.FAR, ChatMode.ENABLED, Difficulty.NORMAL, true, true));
try {
connectionHandler.sendPacket(new C17PacketPluginMessage("MC|Brand", "vanilla".getBytes("UTF-8")));
} catch(UnsupportedEncodingException exception) {
throw new RuntimeException(exception);
}
break;
}
case 0x02: {
Expand Down Expand Up @@ -429,20 +444,17 @@ public void onPacketProcess(PacketProcessEvent event) {
case 0x08: {
S08PacketTeleport teleportPacket = (S08PacketTeleport) packet;

C06PacketPositionRotationUpdate clientUpdatePacket = new C06PacketPositionRotationUpdate( teleportPacket.getX(),
teleportPacket.getY(),
teleportPacket.getZ(),
teleportPacket.getY() + STANCE_CONSTANT,
teleportPacket.getYaw(),
teleportPacket.getPitch(),
teleportPacket.isGrounded());
double x = teleportPacket.getX(), eyeY = teleportPacket.getY(), z = teleportPacket.getZ();
double yaw = teleportPacket.getYaw(), pitch = teleportPacket.getPitch();
boolean grounded = teleportPacket.isGrounded();
// Computers can't arithmetic
double actualY = BigDecimal.valueOf(eyeY).subtract(STANCE_CONSTANT_PRECISE).doubleValue();

C06PacketPositionRotationUpdate clientUpdatePacket = new C06PacketPositionRotationUpdate(x, actualY, z, eyeY, yaw, pitch, grounded);
connectionHandler.sendPacket(clientUpdatePacket);
positionSet = true;

eventBus.fire(new TeleportEvent(teleportPacket.getX(),
teleportPacket.getY(),
teleportPacket.getZ(),
(float) teleportPacket.getYaw(),
(float) teleportPacket.getPitch()));
eventBus.fire(new TeleportEvent(x, actualY, z, (float) yaw, (float) pitch));
break;
}
case 0x09: {
Expand Down Expand Up @@ -633,6 +645,10 @@ else if(updatePacket.getStatus() == 9)
processChunk(chunk, chunkPacket.hasSkylight(), true);
break;
}
case 0x40: {
S40PacketDisconnect disconnectPacket = (S40PacketDisconnect) packet;
eventBus.fire(new KickEvent(disconnectPacket.getReason()));
}
}
break;
default:
Expand Down

0 comments on commit 073081d

Please sign in to comment.