mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-13 10:51:44 +01:00
Inventory update
This commit is contained in:
parent
5c2771c059
commit
7b11a54d29
@ -108,7 +108,7 @@ public class PlayerInit {
|
|||||||
player.getInventory().addItemStack(item);
|
player.getInventory().addItemStack(item);
|
||||||
|
|
||||||
Inventory inventory = new Inventory(InventoryType.CHEST_1_ROW, "Test inventory");
|
Inventory inventory = new Inventory(InventoryType.CHEST_1_ROW, "Test inventory");
|
||||||
inventory.setInventoryCondition((slot, inventory1, inventoryConditionResult) -> {
|
inventory.setInventoryCondition((p, slot, inventoryConditionResult) -> {
|
||||||
inventoryConditionResult.setCancel(false);
|
inventoryConditionResult.setCancel(false);
|
||||||
});
|
});
|
||||||
inventory.setItemStack(0, item.clone());
|
inventory.setItemStack(0, item.clone());
|
||||||
|
@ -70,20 +70,6 @@ public class EntityManager {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void waitingPlayersTick() {
|
|
||||||
Player waitingPlayer;
|
|
||||||
while ((waitingPlayer = waitingPlayers.poll()) != null) {
|
|
||||||
final Player playerCache = waitingPlayer;
|
|
||||||
playersPool.execute(() -> {
|
|
||||||
PlayerLoginEvent loginEvent = new PlayerLoginEvent();
|
|
||||||
playerCache.callEvent(PlayerLoginEvent.class, loginEvent);
|
|
||||||
Instance spawningInstance = loginEvent.getSpawningInstance() == null ? instanceManager.createInstanceContainer() : loginEvent.getSpawningInstance();
|
|
||||||
|
|
||||||
playerCache.setInstance(spawningInstance);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update each entity type separately independently of their location
|
* Update each entity type separately independently of their location
|
||||||
*
|
*
|
||||||
@ -170,6 +156,20 @@ public class EntityManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add connected clients after the handshake (used to free the networking threads)
|
||||||
|
private void waitingPlayersTick() {
|
||||||
|
Player waitingPlayer;
|
||||||
|
while ((waitingPlayer = waitingPlayers.poll()) != null) {
|
||||||
|
final Player playerCache = waitingPlayer;
|
||||||
|
playersPool.execute(() -> {
|
||||||
|
PlayerLoginEvent loginEvent = new PlayerLoginEvent();
|
||||||
|
playerCache.callEvent(PlayerLoginEvent.class, loginEvent);
|
||||||
|
Instance spawningInstance = loginEvent.getSpawningInstance() == null ? instanceManager.createInstanceContainer() : loginEvent.getSpawningInstance();
|
||||||
|
|
||||||
|
playerCache.setInstance(spawningInstance);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public UpdateType getUpdateType() {
|
public UpdateType getUpdateType() {
|
||||||
return updateType;
|
return updateType;
|
||||||
|
@ -162,7 +162,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset);
|
ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset);
|
||||||
|
|
||||||
|
|
||||||
InventoryClickResult clickResult = clickProcessor.leftClick(getInventoryCondition(), slot, clicked, cursor);
|
InventoryClickResult clickResult = clickProcessor.leftClick(getInventoryCondition(), player, slot, clicked, cursor);
|
||||||
|
|
||||||
if (clickResult.doRefresh())
|
if (clickResult.doRefresh())
|
||||||
player.getPlayerConnection().sendPacket(getWindowItemsPacket());
|
player.getPlayerConnection().sendPacket(getWindowItemsPacket());
|
||||||
@ -183,7 +183,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
boolean isInWindow = isClickInWindow(slot);
|
boolean isInWindow = isClickInWindow(slot);
|
||||||
ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset);
|
ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset);
|
||||||
|
|
||||||
InventoryClickResult clickResult = clickProcessor.rightClick(getInventoryCondition(), slot, clicked, cursor);
|
InventoryClickResult clickResult = clickProcessor.rightClick(getInventoryCondition(), player, slot, clicked, cursor);
|
||||||
|
|
||||||
if (clickResult.doRefresh())
|
if (clickResult.doRefresh())
|
||||||
player.getPlayerConnection().sendPacket(getWindowItemsPacket());
|
player.getPlayerConnection().sendPacket(getWindowItemsPacket());
|
||||||
@ -208,7 +208,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
InventoryCondition inventoryCondition = getInventoryCondition();
|
InventoryCondition inventoryCondition = getInventoryCondition();
|
||||||
if (inventoryCondition != null) {
|
if (inventoryCondition != null) {
|
||||||
InventoryConditionResult result = new InventoryConditionResult(clicked, cursorItem);
|
InventoryConditionResult result = new InventoryConditionResult(clicked, cursorItem);
|
||||||
inventoryCondition.accept(slot, null, result);
|
inventoryCondition.accept(player, slot, result);
|
||||||
|
|
||||||
cursorItem = result.getCursorItem();
|
cursorItem = result.getCursorItem();
|
||||||
clicked = result.getClickedItem();
|
clicked = result.getClickedItem();
|
||||||
@ -312,7 +312,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset);
|
ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset);
|
||||||
ItemStack heldItem = playerInventory.getItemStack(key);
|
ItemStack heldItem = playerInventory.getItemStack(key);
|
||||||
|
|
||||||
InventoryClickResult clickResult = clickProcessor.changeHeld(getInventoryCondition(), slot, clicked, heldItem);
|
InventoryClickResult clickResult = clickProcessor.changeHeld(getInventoryCondition(), player, slot, clicked, heldItem);
|
||||||
|
|
||||||
if (clickResult.doRefresh())
|
if (clickResult.doRefresh())
|
||||||
player.getPlayerConnection().sendPacket(getWindowItemsPacket());
|
player.getPlayerConnection().sendPacket(getWindowItemsPacket());
|
||||||
@ -356,7 +356,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
InventoryCondition inventoryCondition = getInventoryCondition();
|
InventoryCondition inventoryCondition = getInventoryCondition();
|
||||||
if (inventoryCondition != null) {
|
if (inventoryCondition != null) {
|
||||||
InventoryConditionResult result = new InventoryConditionResult(clicked, cursorItem);
|
InventoryConditionResult result = new InventoryConditionResult(clicked, cursorItem);
|
||||||
inventoryCondition.accept(slot, null, result);
|
inventoryCondition.accept(player, slot, result);
|
||||||
|
|
||||||
cursorItem = result.getCursorItem();
|
cursorItem = result.getCursorItem();
|
||||||
clicked = result.getClickedItem();
|
clicked = result.getClickedItem();
|
||||||
|
@ -262,7 +262,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
ItemStack cursor = getCursorItem();
|
ItemStack cursor = getCursorItem();
|
||||||
ItemStack clicked = getItemStack(convertSlot(slot, OFFSET));
|
ItemStack clicked = getItemStack(convertSlot(slot, OFFSET));
|
||||||
|
|
||||||
InventoryClickResult clickResult = clickProcessor.leftClick(getInventoryCondition(), slot, clicked, cursor);
|
InventoryClickResult clickResult = clickProcessor.leftClick(getInventoryCondition(), player, slot, clicked, cursor);
|
||||||
|
|
||||||
if (clickResult.doRefresh())
|
if (clickResult.doRefresh())
|
||||||
sendSlotRefresh((short) slot, clicked);
|
sendSlotRefresh((short) slot, clicked);
|
||||||
@ -276,7 +276,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
ItemStack cursor = getCursorItem();
|
ItemStack cursor = getCursorItem();
|
||||||
ItemStack clicked = getItemStack(slot, OFFSET);
|
ItemStack clicked = getItemStack(slot, OFFSET);
|
||||||
|
|
||||||
InventoryClickResult clickResult = clickProcessor.rightClick(getInventoryCondition(), slot, clicked, cursor);
|
InventoryClickResult clickResult = clickProcessor.rightClick(getInventoryCondition(), player, slot, clicked, cursor);
|
||||||
|
|
||||||
if (clickResult.doRefresh())
|
if (clickResult.doRefresh())
|
||||||
sendSlotRefresh((short) slot, clicked);
|
sendSlotRefresh((short) slot, clicked);
|
||||||
@ -309,7 +309,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
InventoryCondition inventoryCondition = getInventoryCondition();
|
InventoryCondition inventoryCondition = getInventoryCondition();
|
||||||
if (inventoryCondition != null) {
|
if (inventoryCondition != null) {
|
||||||
InventoryConditionResult result = new InventoryConditionResult(clicked, cursorItem);
|
InventoryConditionResult result = new InventoryConditionResult(clicked, cursorItem);
|
||||||
inventoryCondition.accept(slot, null, result);
|
inventoryCondition.accept(player, slot, result);
|
||||||
|
|
||||||
cursorItem = result.getCursorItem();
|
cursorItem = result.getCursorItem();
|
||||||
clicked = result.getClickedItem();
|
clicked = result.getClickedItem();
|
||||||
@ -377,7 +377,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
ItemStack heldItem = getItemStack(key);
|
ItemStack heldItem = getItemStack(key);
|
||||||
ItemStack clicked = getItemStack(slot, OFFSET);
|
ItemStack clicked = getItemStack(slot, OFFSET);
|
||||||
|
|
||||||
InventoryClickResult clickResult = clickProcessor.changeHeld(getInventoryCondition(), slot, clicked, heldItem);
|
InventoryClickResult clickResult = clickProcessor.changeHeld(getInventoryCondition(), player, slot, clicked, heldItem);
|
||||||
|
|
||||||
if (clickResult.doRefresh())
|
if (clickResult.doRefresh())
|
||||||
sendSlotRefresh((short) slot, clicked);
|
sendSlotRefresh((short) slot, clicked);
|
||||||
@ -397,7 +397,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
InventoryCondition inventoryCondition = getInventoryCondition();
|
InventoryCondition inventoryCondition = getInventoryCondition();
|
||||||
if (inventoryCondition != null) {
|
if (inventoryCondition != null) {
|
||||||
InventoryConditionResult result = new InventoryConditionResult(clicked, cursorItem);
|
InventoryConditionResult result = new InventoryConditionResult(clicked, cursorItem);
|
||||||
inventoryCondition.accept(slot, null, result);
|
inventoryCondition.accept(player, slot, result);
|
||||||
|
|
||||||
cursorItem = result.getCursorItem();
|
cursorItem = result.getCursorItem();
|
||||||
clicked = result.getClickedItem();
|
clicked = result.getClickedItem();
|
||||||
@ -427,18 +427,36 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
if (!leftDraggingMap.containsKey(player))
|
if (!leftDraggingMap.containsKey(player))
|
||||||
return;
|
return;
|
||||||
Set<Integer> slots = leftDraggingMap.get(player);
|
Set<Integer> slots = leftDraggingMap.get(player);
|
||||||
int size = slots.size();
|
int slotCount = slots.size();
|
||||||
int cursorAmount = stackingRule.getAmount(cursorItem);
|
int cursorAmount = stackingRule.getAmount(cursorItem);
|
||||||
if (size > cursorAmount)
|
if (slotCount > cursorAmount)
|
||||||
return;
|
return;
|
||||||
int slotSize = (int) ((float) cursorAmount / (float) size);
|
// Should be size of each defined slot (if not full)
|
||||||
|
int slotSize = (int) ((float) cursorAmount / (float) slotCount);
|
||||||
|
int finalCursorAmount = cursorAmount;
|
||||||
|
|
||||||
for (Integer s : slots) {
|
for (Integer s : slots) {
|
||||||
ItemStack draggedItem = cursorItem.clone();
|
ItemStack draggedItem = cursorItem.clone();
|
||||||
draggedItem = stackingRule.apply(draggedItem, slotSize);
|
ItemStack slotItem = getItemStack(s, OFFSET);
|
||||||
setItemStack(s, OFFSET, draggedItem);
|
int maxSize = stackingRule.getMaxSize();
|
||||||
|
if (stackingRule.canBeStacked(draggedItem, slotItem)) {
|
||||||
|
int amount = slotItem.getAmount() + slotSize;
|
||||||
|
if (stackingRule.canApply(slotItem, amount)) {
|
||||||
|
slotItem = stackingRule.apply(slotItem, amount);
|
||||||
|
finalCursorAmount -= slotSize;
|
||||||
|
} else {
|
||||||
|
int removedAmount = amount - maxSize;
|
||||||
|
slotItem = stackingRule.apply(slotItem, maxSize);
|
||||||
|
finalCursorAmount -= removedAmount;
|
||||||
}
|
}
|
||||||
cursorItem = stackingRule.apply(cursorItem, cursorAmount - (slotSize * size));
|
} else if (slotItem.isAir()) {
|
||||||
|
slotItem = stackingRule.apply(draggedItem, slotSize);
|
||||||
|
finalCursorAmount -= slotSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
setItemStack(s, OFFSET, slotItem);
|
||||||
|
}
|
||||||
|
cursorItem = stackingRule.apply(cursorItem, finalCursorAmount);
|
||||||
setCursorItem(cursorItem);
|
setCursorItem(cursorItem);
|
||||||
|
|
||||||
leftDraggingMap.remove(player);
|
leftDraggingMap.remove(player);
|
||||||
@ -453,14 +471,13 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
return;
|
return;
|
||||||
for (Integer s : slots) {
|
for (Integer s : slots) {
|
||||||
ItemStack draggedItem = cursorItem.clone();
|
ItemStack draggedItem = cursorItem.clone();
|
||||||
ItemStack slotItem = getItemStack(s);
|
ItemStack slotItem = getItemStack(s, OFFSET);
|
||||||
if (draggedItem.isSimilar(slotItem)) {
|
if (stackingRule.canBeStacked(draggedItem, slotItem)) {
|
||||||
int amount = slotItem.getAmount() + 1;
|
int amount = slotItem.getAmount() + 1;
|
||||||
if (stackingRule.canApply(slotItem, amount)) {
|
if (stackingRule.canApply(slotItem, amount)) {
|
||||||
slotItem = stackingRule.apply(slotItem, amount);
|
slotItem = stackingRule.apply(slotItem, amount);
|
||||||
setItemStack(s, OFFSET, slotItem);
|
setItemStack(s, OFFSET, slotItem);
|
||||||
}
|
}
|
||||||
System.out.println("TEST: " + s + ":" + OFFSET);
|
|
||||||
} else if (slotItem.isAir()) {
|
} else if (slotItem.isAir()) {
|
||||||
draggedItem = stackingRule.apply(draggedItem, 1);
|
draggedItem = stackingRule.apply(draggedItem, 1);
|
||||||
setItemStack(s, OFFSET, draggedItem);
|
setItemStack(s, OFFSET, draggedItem);
|
||||||
@ -499,7 +516,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
InventoryCondition inventoryCondition = getInventoryCondition();
|
InventoryCondition inventoryCondition = getInventoryCondition();
|
||||||
if (inventoryCondition != null) {
|
if (inventoryCondition != null) {
|
||||||
InventoryConditionResult result = new InventoryConditionResult(clicked, cursorItem);
|
InventoryConditionResult result = new InventoryConditionResult(clicked, cursorItem);
|
||||||
inventoryCondition.accept(slot, null, result);
|
inventoryCondition.accept(player, slot, result);
|
||||||
|
|
||||||
cursorItem = result.getCursorItem();
|
cursorItem = result.getCursorItem();
|
||||||
clicked = result.getClickedItem();
|
clicked = result.getClickedItem();
|
||||||
|
@ -1,14 +1,23 @@
|
|||||||
package fr.themode.minestom.inventory.click;
|
package fr.themode.minestom.inventory.click;
|
||||||
|
|
||||||
|
import fr.themode.minestom.entity.Player;
|
||||||
import fr.themode.minestom.inventory.rule.InventoryCondition;
|
import fr.themode.minestom.inventory.rule.InventoryCondition;
|
||||||
import fr.themode.minestom.inventory.rule.InventoryConditionResult;
|
import fr.themode.minestom.inventory.rule.InventoryConditionResult;
|
||||||
import fr.themode.minestom.item.ItemStack;
|
import fr.themode.minestom.item.ItemStack;
|
||||||
import fr.themode.minestom.item.StackingRule;
|
import fr.themode.minestom.item.StackingRule;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class InventoryClickProcessor {
|
public class InventoryClickProcessor {
|
||||||
|
|
||||||
public InventoryClickResult leftClick(InventoryCondition inventoryCondition, int slot, ItemStack clicked, ItemStack cursor) {
|
// Dragging maps
|
||||||
InventoryClickResult clickResult = startCondition(inventoryCondition, slot, clicked, cursor);
|
private Map<Player, Set<Integer>> leftDraggingMap = new HashMap<>();
|
||||||
|
private Map<Player, Set<Integer>> rightDraggingMap = new HashMap<>();
|
||||||
|
|
||||||
|
public InventoryClickResult leftClick(InventoryCondition inventoryCondition, Player player, int slot, ItemStack clicked, ItemStack cursor) {
|
||||||
|
InventoryClickResult clickResult = startCondition(inventoryCondition, player, slot, clicked, cursor);
|
||||||
|
|
||||||
if (clickResult.isCancel()) {
|
if (clickResult.isCancel()) {
|
||||||
return clickResult;
|
return clickResult;
|
||||||
@ -50,8 +59,8 @@ public class InventoryClickProcessor {
|
|||||||
return clickResult;
|
return clickResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InventoryClickResult rightClick(InventoryCondition inventoryCondition, int slot, ItemStack clicked, ItemStack cursor) {
|
public InventoryClickResult rightClick(InventoryCondition inventoryCondition, Player player, int slot, ItemStack clicked, ItemStack cursor) {
|
||||||
InventoryClickResult clickResult = startCondition(inventoryCondition, slot, clicked, cursor);
|
InventoryClickResult clickResult = startCondition(inventoryCondition, player, slot, clicked, cursor);
|
||||||
|
|
||||||
if (clickResult.isCancel()) {
|
if (clickResult.isCancel()) {
|
||||||
return clickResult;
|
return clickResult;
|
||||||
@ -107,8 +116,8 @@ public class InventoryClickProcessor {
|
|||||||
return clickResult;
|
return clickResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InventoryClickResult changeHeld(InventoryCondition inventoryCondition, int slot, ItemStack clicked, ItemStack cursor) {
|
public InventoryClickResult changeHeld(InventoryCondition inventoryCondition, Player player, int slot, ItemStack clicked, ItemStack cursor) {
|
||||||
InventoryClickResult clickResult = startCondition(inventoryCondition, slot, clicked, cursor);
|
InventoryClickResult clickResult = startCondition(inventoryCondition, player, slot, clicked, cursor);
|
||||||
|
|
||||||
if (clickResult.isCancel()) {
|
if (clickResult.isCancel()) {
|
||||||
return clickResult;
|
return clickResult;
|
||||||
@ -147,11 +156,11 @@ public class InventoryClickProcessor {
|
|||||||
return clickResult;
|
return clickResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
private InventoryClickResult startCondition(InventoryCondition inventoryCondition, int slot, ItemStack clicked, ItemStack cursor) {
|
private InventoryClickResult startCondition(InventoryCondition inventoryCondition, Player player, int slot, ItemStack clicked, ItemStack cursor) {
|
||||||
InventoryClickResult clickResult = new InventoryClickResult(clicked, cursor);
|
InventoryClickResult clickResult = new InventoryClickResult(clicked, cursor);
|
||||||
if (inventoryCondition != null) {
|
if (inventoryCondition != null) {
|
||||||
InventoryConditionResult result = new InventoryConditionResult(clicked, cursor);
|
InventoryConditionResult result = new InventoryConditionResult(clicked, cursor);
|
||||||
inventoryCondition.accept(slot, null, result);
|
inventoryCondition.accept(player, slot, result);
|
||||||
|
|
||||||
cursor = result.getCursorItem();
|
cursor = result.getCursorItem();
|
||||||
clicked = result.getClickedItem();
|
clicked = result.getClickedItem();
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package fr.themode.minestom.inventory.rule;
|
package fr.themode.minestom.inventory.rule;
|
||||||
|
|
||||||
import fr.themode.minestom.inventory.Inventory;
|
import fr.themode.minestom.entity.Player;
|
||||||
|
|
||||||
public interface InventoryCondition {
|
public interface InventoryCondition {
|
||||||
|
|
||||||
void accept(int slot, Inventory inventory, InventoryConditionResult inventoryConditionResult);
|
void accept(Player player, int slot, InventoryConditionResult inventoryConditionResult);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
public class ItemStack implements DataContainer {
|
public class ItemStack implements DataContainer {
|
||||||
|
|
||||||
public static final ItemStack AIR_ITEM = new ItemStack(0, (byte) 1);
|
public static final ItemStack AIR_ITEM = new ItemStack(0, (byte) 0);
|
||||||
private static StackingRule defaultStackingRule;
|
private static StackingRule defaultStackingRule;
|
||||||
|
|
||||||
private int materialId;
|
private int materialId;
|
||||||
|
Loading…
Reference in New Issue
Block a user