Skip to content

Commit

Permalink
ensure grim sent the transaction in PacketEntityReplication
Browse files Browse the repository at this point in the history
  • Loading branch information
AoElite committed Jan 23, 2024
1 parent 70f6ea0 commit 60cc4d7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public void onPacketReceive(PacketReceiveEvent event) {

@Override
public void onPacketSend(PacketSendEvent event) {
if (event.getPacketType() == PacketType.Play.Server.PING || event.getPacketType() == PacketType.Play.Server.WINDOW_CONFIRMATION) {
// ensure grim is the one that sent the transaction
if ((event.getPacketType() == PacketType.Play.Server.PING || event.getPacketType() == PacketType.Play.Server.WINDOW_CONFIRMATION) && player.packetStateData.lastServerTransWasValid) {
despawnedEntitiesThisTransaction.clear();
}
if (event.getPacketType() == PacketType.Play.Server.SPAWN_LIVING_ENTITY) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ public void onPacketSend(PacketSendEvent event) {
if (event.getPacketType() == PacketType.Play.Server.WINDOW_CONFIRMATION) {
WrapperPlayServerWindowConfirmation confirmation = new WrapperPlayServerWindowConfirmation(event);
short id = confirmation.getActionId();

//
GrimPlayer player = GrimAPI.INSTANCE.getPlayerDataManager().getPlayer(event.getUser());
if (player == null) return;
player.packetStateData.lastServerTransWasValid = false;
// Vanilla always uses an ID starting from 1
if (id <= 0) {
GrimPlayer player = GrimAPI.INSTANCE.getPlayerDataManager().getPlayer(event.getUser());
if (player == null) return;

if (player.didWeSendThatTrans.remove((Short) id)) {
if (player.didWeSendThatTrans.remove(id)) {
player.packetStateData.lastServerTransWasValid = true;
player.transactionsSent.add(new Pair<>(id, System.nanoTime()));
player.lastTransactionSent.getAndIncrement();
}
Expand All @@ -88,15 +89,17 @@ public void onPacketSend(PacketSendEvent event) {

if (event.getPacketType() == PacketType.Play.Server.PING) {
WrapperPlayServerPing pong = new WrapperPlayServerPing(event);

int id = pong.getId();
//
GrimPlayer player = GrimAPI.INSTANCE.getPlayerDataManager().getPlayer(event.getUser());
if (player == null) return;
player.packetStateData.lastServerTransWasValid = false;
// Check if in the short range, we only use short range
if (id == (short) id) {
GrimPlayer player = GrimAPI.INSTANCE.getPlayerDataManager().getPlayer(event.getUser());
if (player == null) return;
// Cast ID twice so we can use the list
Short shortID = ((short) id);
if (player.didWeSendThatTrans.remove(shortID)) {
player.packetStateData.lastServerTransWasValid = true;
player.transactionsSent.add(new Pair<>(shortID, System.nanoTime()));
player.lastTransactionSent.getAndIncrement();
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/ac/grim/grimac/utils/data/PacketStateData.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public class PacketStateData {

public float lastHealth, lastSaturation;
public int lastFood;
public boolean lastServerTransWasValid = false;

}

0 comments on commit 60cc4d7

Please sign in to comment.