Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added NoGhostItems. Makes sure item transactions got accepted before doing client side inventory change #305

Merged
merged 2 commits into from
May 1, 2022
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
12 changes: 12 additions & 0 deletions src/main/java/com/lambda/mixin/player/MixinPlayerControllerMP.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

import com.lambda.client.event.LambdaEventBus;
import com.lambda.client.event.events.PlayerAttackEvent;
import com.lambda.client.module.modules.player.NoGhostItems;
import com.lambda.client.module.modules.player.TpsSync;
import com.lambda.client.util.TpsCalculator;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.multiplayer.PlayerControllerMP;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(PlayerControllerMP.class)
public class MixinPlayerControllerMP {
Expand All @@ -33,4 +37,12 @@ public void attackEntity(EntityPlayer playerIn, Entity targetEntity, CallbackInf
ci.cancel();
}
}

@Inject(method = "windowClick", at = @At("HEAD"), cancellable = true)
public void onWindowClick(int windowId, int slotId, int mouseButton, ClickType type, EntityPlayer player, CallbackInfoReturnable<ItemStack> cir) {
if (NoGhostItems.INSTANCE.isEnabled()) {
NoGhostItems.INSTANCE.handleWindowClick(windowId, slotId, mouseButton, type, player);
cir.cancel();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.lambda.client.module.modules.player

import com.lambda.client.event.events.PacketEvent
import com.lambda.client.event.listener.listener
import com.lambda.client.module.Category
import com.lambda.client.module.Module
import com.lambda.client.util.threads.runSafe
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.inventory.ClickType
import net.minecraft.item.ItemStack
import net.minecraft.network.play.client.CPacketClickWindow
import net.minecraft.network.play.server.SPacketConfirmTransaction

object NoGhostItems : Module(
name = "NoGhostItems",
description = "Syncs inventory transactions for strict environments",
category = Category.PLAYER
) {
private val timeout by setting("Timeout in ticks", 5, 1..50, 1)

private var pendingTransaction: InventoryTransaction? = null
private var lastPending = System.currentTimeMillis()

init {
listener<PacketEvent.Receive> { event ->
if (event.packet is SPacketConfirmTransaction) {
pendingTransaction?.let {
it.player.openContainer.slotClick(it.slotId, it.mouseButton, it.type, it.player)
pendingTransaction = null
}
}
}
}

fun handleWindowClick(windowId: Int, slotId: Int, mouseButton: Int, type: ClickType, player: EntityPlayer) {
val transaction = InventoryTransaction(windowId, slotId, mouseButton, type, player)

if (pendingTransaction == null || System.currentTimeMillis() - lastPending > timeout * 50L) {
val transactionID = transaction.player.openContainer.getNextTransactionID(transaction.player.inventory)
pendingTransaction = transaction
lastPending = System.currentTimeMillis()

runSafe {
connection.sendPacket(CPacketClickWindow(transaction.windowId, transaction.slotId, transaction.mouseButton, transaction.type, ItemStack.EMPTY, transactionID))
}
}
}

data class InventoryTransaction(val windowId: Int, val slotId: Int, val mouseButton: Int, val type: ClickType, val player: EntityPlayer) {
override fun toString(): String {
return "windowId: $windowId slotId: $slotId mouseButton: $mouseButton type: ${type.name} player: ${player.name}"
}
}
}