mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-26 02:57:37 +01:00
Added dropping functions + fixes
This commit is contained in:
parent
f596a8fa84
commit
7cbb964e21
@ -142,7 +142,7 @@ public class PlayerInit {
|
||||
});
|
||||
|
||||
player.setEventCallback(PlayerSpawnEvent.class, event -> {
|
||||
player.setGameMode(GameMode.CREATIVE);
|
||||
player.setGameMode(GameMode.SURVIVAL);
|
||||
player.teleport(new Position(0, 75, 0));
|
||||
|
||||
/*Random random = new Random();
|
||||
@ -168,7 +168,8 @@ public class PlayerInit {
|
||||
player.getInventory().addItemStack(item);
|
||||
|
||||
Inventory inventory = new Inventory(InventoryType.CHEST_1_ROW, "Test inventory");
|
||||
inventory.setInventoryCondition((p, slot, inventoryConditionResult) -> {
|
||||
inventory.setInventoryCondition((p, slot, clickType, inventoryConditionResult) -> {
|
||||
player.sendMessage("click type: " + clickType);
|
||||
inventoryConditionResult.setCancel(false);
|
||||
});
|
||||
inventory.setItemStack(0, item.clone());
|
||||
|
@ -322,6 +322,7 @@ public class Player extends LivingEntity {
|
||||
}
|
||||
boolean isLast = counter.get() == length - 1;
|
||||
if (isLast) {
|
||||
System.out.println("DEBUG CHUNK: " + chunk);
|
||||
// This is the last chunk to be loaded , spawn player
|
||||
super.setInstance(instance);
|
||||
PlayerSpawnEvent spawnEvent = new PlayerSpawnEvent(instance);
|
||||
|
@ -266,12 +266,29 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropOne(Player player, int slot) {
|
||||
public void drop(Player player, int mode, int slot, int button) {
|
||||
PlayerInventory playerInventory = player.getInventory();
|
||||
boolean isInWindow = isClickInWindow(slot);
|
||||
ItemStack clicked = slot == -999 ?
|
||||
null : (isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset));
|
||||
ItemStack cursor = getCursorItem(player);
|
||||
|
||||
}
|
||||
InventoryClickResult clickResult = clickProcessor.drop(getInventoryCondition(), player,
|
||||
mode, slot, button, clicked, cursor);
|
||||
|
||||
@Override
|
||||
public void dropItemStack(Player player, int slot) {
|
||||
if (clickResult.doRefresh())
|
||||
player.getPlayerConnection().sendPacket(getWindowItemsPacket());
|
||||
|
||||
ItemStack resultClicked = clickResult.getClicked();
|
||||
if (isInWindow) {
|
||||
if (resultClicked != null)
|
||||
setItemStack(slot, resultClicked);
|
||||
setCursorPlayerItem(player, clickResult.getCursor());
|
||||
} else {
|
||||
if (resultClicked != null)
|
||||
playerInventory.setItemStack(slot, offset, resultClicked);
|
||||
setCursorPlayerItem(player, clickResult.getCursor());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -14,9 +14,7 @@ public interface InventoryClickHandler {
|
||||
|
||||
void middleClick(Player player, int slot);
|
||||
|
||||
void dropOne(Player player, int slot);
|
||||
|
||||
void dropItemStack(Player player, int slot);
|
||||
void drop(Player player, int mode, int slot, int button);
|
||||
|
||||
void dragging(Player player, int slot, int button);
|
||||
|
||||
|
@ -312,13 +312,20 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropOne(Player player, int slot) {
|
||||
public void drop(Player player, int mode, int slot, int button) {
|
||||
ItemStack cursor = getCursorItem();
|
||||
ItemStack clicked = slot == -999 ? null : getItemStack(slot, OFFSET);
|
||||
|
||||
}
|
||||
InventoryClickResult clickResult = clickProcessor.drop(getInventoryCondition(), player,
|
||||
mode, slot, button, clicked, cursor);
|
||||
|
||||
@Override
|
||||
public void dropItemStack(Player player, int slot) {
|
||||
if (clickResult.doRefresh())
|
||||
sendSlotRefresh((short) slot, clicked);
|
||||
|
||||
ItemStack resultClicked = clickResult.getClicked();
|
||||
if (resultClicked != null)
|
||||
setItemStack(slot, OFFSET, resultClicked);
|
||||
setCursorItem(clickResult.getCursor());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,13 @@
|
||||
package fr.themode.minestom.inventory.click;
|
||||
|
||||
public enum ClickType {
|
||||
|
||||
LEFT_CLICK,
|
||||
RIGHT_CLICK,
|
||||
CHANGE_HELD,
|
||||
SHIFT_CLICK,
|
||||
DRAGGING,
|
||||
DOUBLE_CLICK,
|
||||
DROP
|
||||
|
||||
}
|
@ -20,7 +20,7 @@ public class InventoryClickProcessor {
|
||||
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);
|
||||
InventoryClickResult clickResult = startCondition(inventoryCondition, player, slot, ClickType.LEFT_CLICK, clicked, cursor);
|
||||
|
||||
if (clickResult.isCancel()) {
|
||||
return clickResult;
|
||||
@ -63,7 +63,7 @@ public class InventoryClickProcessor {
|
||||
}
|
||||
|
||||
public InventoryClickResult rightClick(InventoryCondition inventoryCondition, Player player, int slot, ItemStack clicked, ItemStack cursor) {
|
||||
InventoryClickResult clickResult = startCondition(inventoryCondition, player, slot, clicked, cursor);
|
||||
InventoryClickResult clickResult = startCondition(inventoryCondition, player, slot, ClickType.RIGHT_CLICK, clicked, cursor);
|
||||
|
||||
if (clickResult.isCancel()) {
|
||||
return clickResult;
|
||||
@ -120,7 +120,7 @@ public class InventoryClickProcessor {
|
||||
}
|
||||
|
||||
public InventoryClickResult changeHeld(InventoryCondition inventoryCondition, Player player, int slot, ItemStack clicked, ItemStack cursor) {
|
||||
InventoryClickResult clickResult = startCondition(inventoryCondition, player, slot, clicked, cursor);
|
||||
InventoryClickResult clickResult = startCondition(inventoryCondition, player, slot, ClickType.CHANGE_HELD, clicked, cursor);
|
||||
|
||||
if (clickResult.isCancel()) {
|
||||
return clickResult;
|
||||
@ -186,7 +186,7 @@ public class InventoryClickProcessor {
|
||||
StackingRule itemRule = item.getStackingRule();
|
||||
if (itemRule.canBeStacked(item, clicked)) {
|
||||
|
||||
clickResult = startCondition(clickResult, inventoryCondition, player, index, item, cursor);
|
||||
clickResult = startCondition(clickResult, inventoryCondition, player, index, ClickType.SHIFT_CLICK, item, cursor);
|
||||
if (clickResult.isCancel())
|
||||
continue;
|
||||
|
||||
@ -212,7 +212,7 @@ public class InventoryClickProcessor {
|
||||
}
|
||||
} else if (item.isAir()) {
|
||||
|
||||
clickResult = startCondition(clickResult, inventoryCondition, player, index, item, cursor);
|
||||
clickResult = startCondition(clickResult, inventoryCondition, player, index, ClickType.SHIFT_CLICK, item, cursor);
|
||||
if (clickResult.isCancel())
|
||||
continue;
|
||||
|
||||
@ -265,7 +265,7 @@ public class InventoryClickProcessor {
|
||||
ItemStack draggedItem = cursor.clone();
|
||||
ItemStack slotItem = itemGetter.apply(s);
|
||||
|
||||
clickResult = startCondition(clickResult, inventoryCondition, player, s, slotItem, cursor);
|
||||
clickResult = startCondition(clickResult, inventoryCondition, player, s, ClickType.DRAGGING, slotItem, cursor);
|
||||
if (clickResult.isCancel())
|
||||
continue;
|
||||
|
||||
@ -303,7 +303,7 @@ public class InventoryClickProcessor {
|
||||
ItemStack draggedItem = cursor.clone();
|
||||
ItemStack slotItem = itemGetter.apply(s);
|
||||
|
||||
clickResult = startCondition(clickResult, inventoryCondition, player, s, slotItem, cursor);
|
||||
clickResult = startCondition(clickResult, inventoryCondition, player, s, ClickType.DRAGGING, slotItem, cursor);
|
||||
if (clickResult.isCancel())
|
||||
continue;
|
||||
|
||||
@ -377,7 +377,7 @@ public class InventoryClickProcessor {
|
||||
if (!cursorRule.canApply(cursor, amount + 1))
|
||||
break;
|
||||
if (cursorRule.canBeStacked(cursor, item)) {
|
||||
clickResult = startCondition(clickResult, inventoryCondition, player, index, item, cursor);
|
||||
clickResult = startCondition(clickResult, inventoryCondition, player, index, ClickType.DOUBLE_CLICK, item, cursor);
|
||||
if (clickResult.isCancel())
|
||||
continue;
|
||||
|
||||
@ -401,10 +401,70 @@ public class InventoryClickProcessor {
|
||||
return clickResult;
|
||||
}
|
||||
|
||||
private InventoryClickResult startCondition(InventoryClickResult clickResult, InventoryCondition inventoryCondition, Player player, int slot, ItemStack clicked, ItemStack cursor) {
|
||||
public InventoryClickResult drop(InventoryCondition inventoryCondition, Player player,
|
||||
int mode, int slot, int button,
|
||||
ItemStack clicked, ItemStack cursor) {
|
||||
InventoryClickResult clickResult = startCondition(inventoryCondition, player, slot, ClickType.DROP, clicked, cursor);
|
||||
|
||||
if (clickResult.isCancel()) {
|
||||
return clickResult;
|
||||
}
|
||||
|
||||
StackingRule clickedRule = clicked == null ? null : clicked.getStackingRule();
|
||||
StackingRule cursorRule = cursor.getStackingRule();
|
||||
|
||||
ItemStack resultClicked = clicked == null ? null : clicked.clone();
|
||||
ItemStack resultCursor = cursor.clone();
|
||||
|
||||
|
||||
if (slot == -999) {
|
||||
// Click outside
|
||||
if (button == 0) {
|
||||
// Left (drop all)
|
||||
int amount = cursorRule.getAmount(resultCursor);
|
||||
ItemStack dropItem = cursorRule.apply(resultCursor.clone(), amount);
|
||||
if (player.dropItem(dropItem)) {
|
||||
resultCursor = cursorRule.apply(resultCursor, 0);
|
||||
}
|
||||
} else if (button == 1) {
|
||||
// Right (drop 1)
|
||||
ItemStack dropItem = cursorRule.apply(resultCursor.clone(), 1);
|
||||
if (player.dropItem(dropItem)) {
|
||||
int amount = cursorRule.getAmount(resultCursor);
|
||||
int newAmount = amount - 1;
|
||||
resultCursor = cursorRule.apply(resultCursor, newAmount);
|
||||
}
|
||||
}
|
||||
|
||||
} else if (mode == 4) {
|
||||
if (button == 0) {
|
||||
// Drop key Q (drop 1)
|
||||
ItemStack dropItem = cursorRule.apply(resultClicked.clone(), 1);
|
||||
if (player.dropItem(dropItem)) {
|
||||
int amount = clickedRule.getAmount(resultClicked);
|
||||
int newAmount = amount - 1;
|
||||
resultClicked = cursorRule.apply(resultClicked, newAmount);
|
||||
}
|
||||
} else if (button == 1) {
|
||||
// Ctrl + Drop key Q (drop all)
|
||||
int amount = cursorRule.getAmount(resultClicked);
|
||||
ItemStack dropItem = clickedRule.apply(resultClicked.clone(), amount);
|
||||
if (player.dropItem(dropItem)) {
|
||||
resultClicked = cursorRule.apply(resultClicked, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
clickResult.setClicked(resultClicked);
|
||||
clickResult.setCursor(resultCursor);
|
||||
|
||||
return clickResult;
|
||||
}
|
||||
|
||||
private InventoryClickResult startCondition(InventoryClickResult clickResult, InventoryCondition inventoryCondition, Player player, int slot, ClickType clickType, ItemStack clicked, ItemStack cursor) {
|
||||
if (inventoryCondition != null) {
|
||||
InventoryConditionResult result = new InventoryConditionResult(clicked, cursor);
|
||||
inventoryCondition.accept(player, slot, result);
|
||||
inventoryCondition.accept(player, slot, clickType, result);
|
||||
|
||||
cursor = result.getCursorItem();
|
||||
clicked = result.getClickedItem();
|
||||
@ -419,9 +479,9 @@ public class InventoryClickProcessor {
|
||||
return clickResult;
|
||||
}
|
||||
|
||||
private InventoryClickResult startCondition(InventoryCondition inventoryCondition, Player player, int slot, ItemStack clicked, ItemStack cursor) {
|
||||
private InventoryClickResult startCondition(InventoryCondition inventoryCondition, Player player, int slot, ClickType clickType, ItemStack clicked, ItemStack cursor) {
|
||||
InventoryClickResult clickResult = new InventoryClickResult(clicked, cursor);
|
||||
return startCondition(clickResult, inventoryCondition, player, slot, clicked, cursor);
|
||||
return startCondition(clickResult, inventoryCondition, player, slot, clickType, clicked, cursor);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
package fr.themode.minestom.inventory.condition;
|
||||
|
||||
import fr.themode.minestom.entity.Player;
|
||||
import fr.themode.minestom.inventory.click.ClickType;
|
||||
|
||||
public interface InventoryCondition {
|
||||
|
||||
void accept(Player player, int slot, InventoryConditionResult inventoryConditionResult);
|
||||
void accept(Player player, int slot, ClickType clickType, InventoryConditionResult inventoryConditionResult);
|
||||
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package fr.themode.minestom.listener;
|
||||
|
||||
import fr.themode.minestom.MinecraftServer;
|
||||
import fr.themode.minestom.data.Data;
|
||||
import fr.themode.minestom.entity.Entity;
|
||||
import fr.themode.minestom.entity.GameMode;
|
||||
import fr.themode.minestom.entity.Player;
|
||||
import fr.themode.minestom.event.PlayerBlockInteractEvent;
|
||||
@ -21,6 +22,8 @@ import fr.themode.minestom.net.packet.client.play.ClientPlayerDiggingPacket;
|
||||
import fr.themode.minestom.utils.BlockPosition;
|
||||
import fr.themode.minestom.utils.ChunkUtils;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class BlockPlacementListener {
|
||||
|
||||
private Instance instance;
|
||||
@ -35,6 +38,7 @@ public class BlockPlacementListener {
|
||||
if (instance == null)
|
||||
return;
|
||||
|
||||
// Interact at block
|
||||
PlayerBlockInteractEvent playerBlockInteractEvent = new PlayerBlockInteractEvent(blockPosition, hand);
|
||||
player.callCancellableEvent(PlayerBlockInteractEvent.class, playerBlockInteractEvent, () -> {
|
||||
CustomBlock customBlock = instance.getCustomBlock(blockPosition);
|
||||
@ -44,21 +48,32 @@ public class BlockPlacementListener {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// Check if item at hand is a block
|
||||
ItemStack usedItem = hand == Player.Hand.MAIN ? playerInventory.getItemInMainHand() : playerInventory.getItemInOffHand();
|
||||
Material material = Material.fromId(usedItem.getMaterialId());
|
||||
if (material != null && !material.isBlock()) {
|
||||
//instance.setBlock(blockPosition.clone().add(0, 1, 0), (short) 10);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the newly placed block position
|
||||
int offsetX = blockFace == ClientPlayerDiggingPacket.BlockFace.WEST ? -1 : blockFace == ClientPlayerDiggingPacket.BlockFace.EAST ? 1 : 0;
|
||||
int offsetY = blockFace == ClientPlayerDiggingPacket.BlockFace.BOTTOM ? -1 : blockFace == ClientPlayerDiggingPacket.BlockFace.TOP ? 1 : 0;
|
||||
int offsetZ = blockFace == ClientPlayerDiggingPacket.BlockFace.NORTH ? -1 : blockFace == ClientPlayerDiggingPacket.BlockFace.SOUTH ? 1 : 0;
|
||||
|
||||
blockPosition.add(offsetX, offsetY, offsetZ);
|
||||
boolean intersectPlayer = player.getBoundingBox().intersect(blockPosition); // TODO check if collide with nearby other players
|
||||
if (material.isBlock() && !intersectPlayer) {
|
||||
|
||||
Chunk chunk = instance.getChunkAt(blockPosition);
|
||||
Set<Entity> entities = instance.getChunkEntities(chunk);
|
||||
boolean intersect = false;
|
||||
for (Entity entity : entities) {
|
||||
intersect = entity.getBoundingBox().intersect(blockPosition);
|
||||
if (intersect)
|
||||
break;
|
||||
}
|
||||
|
||||
boolean refreshChunk = false;
|
||||
|
||||
if (material.isBlock() && !intersect) {
|
||||
PlayerBlockPlaceEvent playerBlockPlaceEvent = new PlayerBlockPlaceEvent((short) 10, blockPosition, packet.hand);
|
||||
playerBlockPlaceEvent.consumeBlock(player.getGameMode() != GameMode.CREATIVE);
|
||||
|
||||
@ -73,7 +88,6 @@ public class BlockPlacementListener {
|
||||
|
||||
player.callEvent(PlayerBlockPlaceEvent.class, playerBlockPlaceEvent);
|
||||
if (!playerBlockPlaceEvent.isCancelled() && canPlace) {
|
||||
player.sendMessage("PLACE BLOCK");
|
||||
instance.setBlock(blockPosition, material.getBlock());
|
||||
//instance.setCustomBlock(blockPosition, "updatable");
|
||||
if (playerBlockPlaceEvent.doesConsumeBlock()) {
|
||||
@ -88,15 +102,17 @@ public class BlockPlacementListener {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Refresh chunk
|
||||
Chunk chunk = instance.getChunkAt(blockPosition);
|
||||
instance.sendChunkSectionUpdate(chunk, ChunkUtils.getSectionAt(blockPosition.getY()), player);
|
||||
refreshChunk = true;
|
||||
}
|
||||
} else {
|
||||
// Refresh chunk
|
||||
Chunk chunk = instance.getChunkAt(blockPosition);
|
||||
refreshChunk = true;
|
||||
}
|
||||
|
||||
// Refresh chunk section if needed
|
||||
if (refreshChunk) {
|
||||
instance.sendChunkSectionUpdate(chunk, ChunkUtils.getSectionAt(blockPosition.getY()), player);
|
||||
}
|
||||
|
||||
player.getInventory().refreshSlot(player.getHeldSlot());
|
||||
}
|
||||
|
||||
|
@ -63,8 +63,6 @@ public class UseItemListener {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO check if item in main or off hand is food or item with animation (bow/crossbow/riptide)
|
||||
// TODO in material enum?
|
||||
ArmAnimationEvent armAnimationEvent = null;
|
||||
boolean offhand = hand == Player.Hand.OFF;
|
||||
boolean riptideSpinAttack = false;
|
||||
|
@ -24,7 +24,7 @@ public class WindowListener {
|
||||
short actionNumber = packet.actionNumber;
|
||||
int mode = packet.mode;
|
||||
|
||||
System.out.println("Window id: " + windowId + " | slot: " + slot + " | button: " + button + " | mode: " + mode);
|
||||
// System.out.println("Window id: " + windowId + " | slot: " + slot + " | button: " + button + " | mode: " + mode);
|
||||
|
||||
ConfirmTransactionPacket confirmTransactionPacket = new ConfirmTransactionPacket();
|
||||
confirmTransactionPacket.windowId = windowId;
|
||||
@ -35,12 +35,22 @@ public class WindowListener {
|
||||
case 0:
|
||||
switch (button) {
|
||||
case 0:
|
||||
// Left click
|
||||
clickHandler.leftClick(player, slot);
|
||||
if (slot != -999) {
|
||||
// Left click
|
||||
clickHandler.leftClick(player, slot);
|
||||
} else {
|
||||
// DROP
|
||||
clickHandler.drop(player, mode, slot, button);
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
// Right click
|
||||
clickHandler.rightClick(player, slot);
|
||||
if (slot != -999) {
|
||||
// Right click
|
||||
clickHandler.rightClick(player, slot);
|
||||
} else {
|
||||
// DROP
|
||||
clickHandler.drop(player, mode, slot, button);
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
@ -55,6 +65,7 @@ public class WindowListener {
|
||||
break;
|
||||
case 4:
|
||||
// Dropping functions
|
||||
clickHandler.drop(player, mode, slot, button);
|
||||
break;
|
||||
case 5:
|
||||
// Dragging
|
||||
|
Loading…
Reference in New Issue
Block a user