fix ghost shield issue and delayed interaction when switching to another item (#2432)

This commit is contained in:
LabyStudio 2021-04-12 16:56:53 +02:00 committed by GitHub
parent 0ee80dc41f
commit 76e739e4f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 10 deletions

View File

@ -301,13 +301,17 @@ public class WorldPackets {
// Check if the shield is already there or if we have to give it here
boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand();
if (item != null && Protocol1_9To1_8.isSword(item.getIdentifier())) {
// Method to identify the sword in hand
boolean isSword = showShieldWhenSwordInHand ? tracker.hasSwordInHand()
: item != null && Protocol1_9To1_8.isSword(item.getIdentifier());
if (isSword) {
if (hand == 0) {
if (!tracker.isBlocking()) {
tracker.setBlocking(true);
// Check if the shield is already in the offhand
if (!showShieldWhenSwordInHand || tracker.getItemInSecondHand() == null) {
if (!showShieldWhenSwordInHand && tracker.getItemInSecondHand() == null) {
// Set shield in offhand when interacting with main hand
Item shield = new Item(442, (byte) 1, (short) 0, null);

View File

@ -108,20 +108,28 @@ public class EntityTracker1_9 extends EntityTracker {
* The item in the offhand will be cleared if there is no sword in the main hand.
*/
public void syncShieldWithSword() {
boolean swordInHand = hasSwordInHand();
// Update if there is no sword in the main hand or if the player has no shield in the second hand but a sword in the main hand
if (!swordInHand || this.itemInSecondHand == null) {
// Update shield in off hand depending if a sword is in the main hand
setSecondHand(swordInHand ? new Item(442, (byte) 1, (short) 0, null) : null);
}
}
/**
* Returns true if the item in the held inventory slot is a sword.
* @return player has a sword in the main hand
*/
public boolean hasSwordInHand() {
InventoryTracker inventoryTracker = getUser().get(InventoryTracker.class);
// Get item in new selected slot
int inventorySlot = this.heldItemSlot + 36; // Hotbar slot index to inventory slot
int itemIdentifier = inventoryTracker.getItemId((short) 0, (short) inventorySlot);
boolean isSword = Protocol1_9To1_8.isSword(itemIdentifier);
// Update if the state changed
if (isSword == (this.itemInSecondHand == null)) {
// Update shield in off hand depending if a sword is in the main hand
setSecondHand(isSword ? new Item(442, (byte) 1, (short) 0, null) : null);
}
return Protocol1_9To1_8.isSword(itemIdentifier);
}
@Override