Inventory update

This commit is contained in:
Felix Cravic 2020-03-31 12:37:51 +02:00
parent 5c2771c059
commit 7b11a54d29
7 changed files with 72 additions and 46 deletions

View File

@ -108,7 +108,7 @@ public class PlayerInit {
player.getInventory().addItemStack(item);
Inventory inventory = new Inventory(InventoryType.CHEST_1_ROW, "Test inventory");
inventory.setInventoryCondition((slot, inventory1, inventoryConditionResult) -> {
inventory.setInventoryCondition((p, slot, inventoryConditionResult) -> {
inventoryConditionResult.setCancel(false);
});
inventory.setItemStack(0, item.clone());

View File

@ -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
*
@ -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() {
return updateType;

View File

@ -162,7 +162,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
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())
player.getPlayerConnection().sendPacket(getWindowItemsPacket());
@ -183,7 +183,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
boolean isInWindow = isClickInWindow(slot);
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())
player.getPlayerConnection().sendPacket(getWindowItemsPacket());
@ -208,7 +208,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
InventoryCondition inventoryCondition = getInventoryCondition();
if (inventoryCondition != null) {
InventoryConditionResult result = new InventoryConditionResult(clicked, cursorItem);
inventoryCondition.accept(slot, null, result);
inventoryCondition.accept(player, slot, result);
cursorItem = result.getCursorItem();
clicked = result.getClickedItem();
@ -312,7 +312,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset);
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())
player.getPlayerConnection().sendPacket(getWindowItemsPacket());
@ -356,7 +356,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
InventoryCondition inventoryCondition = getInventoryCondition();
if (inventoryCondition != null) {
InventoryConditionResult result = new InventoryConditionResult(clicked, cursorItem);
inventoryCondition.accept(slot, null, result);
inventoryCondition.accept(player, slot, result);
cursorItem = result.getCursorItem();
clicked = result.getClickedItem();

View File

@ -262,7 +262,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
ItemStack cursor = getCursorItem();
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())
sendSlotRefresh((short) slot, clicked);
@ -276,7 +276,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
ItemStack cursor = getCursorItem();
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())
sendSlotRefresh((short) slot, clicked);
@ -309,7 +309,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
InventoryCondition inventoryCondition = getInventoryCondition();
if (inventoryCondition != null) {
InventoryConditionResult result = new InventoryConditionResult(clicked, cursorItem);
inventoryCondition.accept(slot, null, result);
inventoryCondition.accept(player, slot, result);
cursorItem = result.getCursorItem();
clicked = result.getClickedItem();
@ -377,7 +377,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
ItemStack heldItem = getItemStack(key);
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())
sendSlotRefresh((short) slot, clicked);
@ -397,7 +397,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
InventoryCondition inventoryCondition = getInventoryCondition();
if (inventoryCondition != null) {
InventoryConditionResult result = new InventoryConditionResult(clicked, cursorItem);
inventoryCondition.accept(slot, null, result);
inventoryCondition.accept(player, slot, result);
cursorItem = result.getCursorItem();
clicked = result.getClickedItem();
@ -427,18 +427,36 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
if (!leftDraggingMap.containsKey(player))
return;
Set<Integer> slots = leftDraggingMap.get(player);
int size = slots.size();
int slotCount = slots.size();
int cursorAmount = stackingRule.getAmount(cursorItem);
if (size > cursorAmount)
if (slotCount > cursorAmount)
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) {
ItemStack draggedItem = cursorItem.clone();
draggedItem = stackingRule.apply(draggedItem, slotSize);
setItemStack(s, OFFSET, draggedItem);
ItemStack slotItem = getItemStack(s, OFFSET);
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;
}
} else if (slotItem.isAir()) {
slotItem = stackingRule.apply(draggedItem, slotSize);
finalCursorAmount -= slotSize;
}
setItemStack(s, OFFSET, slotItem);
}
cursorItem = stackingRule.apply(cursorItem, cursorAmount - (slotSize * size));
cursorItem = stackingRule.apply(cursorItem, finalCursorAmount);
setCursorItem(cursorItem);
leftDraggingMap.remove(player);
@ -453,14 +471,13 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
return;
for (Integer s : slots) {
ItemStack draggedItem = cursorItem.clone();
ItemStack slotItem = getItemStack(s);
if (draggedItem.isSimilar(slotItem)) {
ItemStack slotItem = getItemStack(s, OFFSET);
if (stackingRule.canBeStacked(draggedItem, slotItem)) {
int amount = slotItem.getAmount() + 1;
if (stackingRule.canApply(slotItem, amount)) {
slotItem = stackingRule.apply(slotItem, amount);
setItemStack(s, OFFSET, slotItem);
}
System.out.println("TEST: " + s + ":" + OFFSET);
} else if (slotItem.isAir()) {
draggedItem = stackingRule.apply(draggedItem, 1);
setItemStack(s, OFFSET, draggedItem);
@ -499,7 +516,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
InventoryCondition inventoryCondition = getInventoryCondition();
if (inventoryCondition != null) {
InventoryConditionResult result = new InventoryConditionResult(clicked, cursorItem);
inventoryCondition.accept(slot, null, result);
inventoryCondition.accept(player, slot, result);
cursorItem = result.getCursorItem();
clicked = result.getClickedItem();

View File

@ -1,14 +1,23 @@
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.InventoryConditionResult;
import fr.themode.minestom.item.ItemStack;
import fr.themode.minestom.item.StackingRule;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class InventoryClickProcessor {
public InventoryClickResult leftClick(InventoryCondition inventoryCondition, int slot, ItemStack clicked, ItemStack cursor) {
InventoryClickResult clickResult = startCondition(inventoryCondition, slot, clicked, cursor);
// Dragging maps
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()) {
return clickResult;
@ -50,8 +59,8 @@ public class InventoryClickProcessor {
return clickResult;
}
public InventoryClickResult rightClick(InventoryCondition inventoryCondition, int slot, ItemStack clicked, ItemStack cursor) {
InventoryClickResult clickResult = startCondition(inventoryCondition, slot, clicked, cursor);
public InventoryClickResult rightClick(InventoryCondition inventoryCondition, Player player, int slot, ItemStack clicked, ItemStack cursor) {
InventoryClickResult clickResult = startCondition(inventoryCondition, player, slot, clicked, cursor);
if (clickResult.isCancel()) {
return clickResult;
@ -107,8 +116,8 @@ public class InventoryClickProcessor {
return clickResult;
}
public InventoryClickResult changeHeld(InventoryCondition inventoryCondition, int slot, ItemStack clicked, ItemStack cursor) {
InventoryClickResult clickResult = startCondition(inventoryCondition, slot, clicked, cursor);
public InventoryClickResult changeHeld(InventoryCondition inventoryCondition, Player player, int slot, ItemStack clicked, ItemStack cursor) {
InventoryClickResult clickResult = startCondition(inventoryCondition, player, slot, clicked, cursor);
if (clickResult.isCancel()) {
return clickResult;
@ -147,11 +156,11 @@ public class InventoryClickProcessor {
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);
if (inventoryCondition != null) {
InventoryConditionResult result = new InventoryConditionResult(clicked, cursor);
inventoryCondition.accept(slot, null, result);
inventoryCondition.accept(player, slot, result);
cursor = result.getCursorItem();
clicked = result.getClickedItem();

View File

@ -1,9 +1,9 @@
package fr.themode.minestom.inventory.rule;
import fr.themode.minestom.inventory.Inventory;
import fr.themode.minestom.entity.Player;
public interface InventoryCondition {
void accept(int slot, Inventory inventory, InventoryConditionResult inventoryConditionResult);
void accept(Player player, int slot, InventoryConditionResult inventoryConditionResult);
}

View File

@ -8,7 +8,7 @@ import java.util.ArrayList;
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 int materialId;