Drop cursor item when closing inventory

This commit is contained in:
Felix Cravic 2020-05-17 11:40:49 +02:00
parent 47ca22c3d1
commit dff29a4717
4 changed files with 33 additions and 11 deletions

View File

@ -942,12 +942,25 @@ public class Player extends LivingEntity {
*/
public void closeInventory() {
Inventory openInventory = getOpenInventory();
// Drop cursor item when closing inventory
ItemStack cursorItem;
if (openInventory == null) {
cursorItem = getInventory().getCursorItem();
getInventory().setCursorItem(ItemStack.getAirItem());
} else {
cursorItem = openInventory.getCursorItem(this);
}
if (!cursorItem.isAir()) {
dropItem(cursorItem);
}
CloseWindowPacket closeWindowPacket = new CloseWindowPacket();
if (openInventory == null) {
closeWindowPacket.windowId = 0;
} else {
closeWindowPacket.windowId = openInventory.getWindowId();
openInventory.removeViewer(this);
openInventory.removeViewer(this); // Clear cache
refreshOpenInventory(null);
}
playerConnection.sendPacket(closeWindowPacket);

View File

@ -127,6 +127,8 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
@Override
public void removeViewer(Player player) {
this.viewers.remove(player);
this.cursorPlayersItem.remove(player);
this.clickProcessor.clearCache(player);
}
public ItemStack getCursorItem(Player player) {

View File

@ -532,4 +532,9 @@ public class InventoryClickProcessor {
player.callEvent(InventoryClickEvent.class, inventoryClickEvent);
}
public void clearCache(Player player) {
this.leftDraggingMap.remove(player);
this.rightDraggingMap.remove(player);
}
}

View File

@ -153,18 +153,20 @@ public class StorageFolder {
* @param key the specified cached data key
*/
public void saveAndRemoveCachedData(String key) {
SerializableData serializableData = cachedData.get(key);
if (serializableData == null)
return;
synchronized (cachedData) {
SerializableData serializableData = cachedData.get(key);
if (serializableData == null)
return;
try {
set(key, serializableData.getSerializedData());
} catch (IOException e) {
e.printStackTrace();
try {
set(key, serializableData.getSerializedData());
} catch (IOException e) {
e.printStackTrace();
}
// Remove from map
this.cachedData.remove(key);
}
// Remove from map
this.cachedData.remove(key);
}
/**