2019-08-20 17:41:07 +02:00
|
|
|
package fr.themode.minestom.listener;
|
|
|
|
|
|
|
|
import fr.themode.minestom.entity.Player;
|
2019-08-25 20:03:43 +02:00
|
|
|
import fr.themode.minestom.event.PlayerBlockPlaceEvent;
|
2019-08-20 17:41:07 +02:00
|
|
|
import fr.themode.minestom.instance.Chunk;
|
|
|
|
import fr.themode.minestom.instance.Instance;
|
2019-08-29 02:15:52 +02:00
|
|
|
import fr.themode.minestom.inventory.PlayerInventory;
|
|
|
|
import fr.themode.minestom.item.ItemStack;
|
2019-08-20 17:41:07 +02:00
|
|
|
import fr.themode.minestom.net.packet.client.play.ClientPlayerBlockPlacementPacket;
|
|
|
|
import fr.themode.minestom.net.packet.client.play.ClientPlayerDiggingPacket;
|
2019-08-21 16:50:52 +02:00
|
|
|
import fr.themode.minestom.utils.BlockPosition;
|
2019-09-01 06:18:41 +02:00
|
|
|
import fr.themode.minestom.utils.ChunkUtils;
|
2019-08-20 17:41:07 +02:00
|
|
|
|
|
|
|
public class BlockPlacementListener {
|
|
|
|
|
|
|
|
public static void listener(ClientPlayerBlockPlacementPacket packet, Player player) {
|
2019-08-29 02:15:52 +02:00
|
|
|
PlayerInventory playerInventory = player.getInventory();
|
|
|
|
Player.Hand hand = packet.hand;
|
2019-08-20 17:41:07 +02:00
|
|
|
ClientPlayerDiggingPacket.BlockFace blockFace = packet.blockFace;
|
2019-08-21 16:50:52 +02:00
|
|
|
BlockPosition blockPosition = packet.blockPosition;
|
2019-08-20 17:41:07 +02:00
|
|
|
|
|
|
|
Instance instance = player.getInstance();
|
|
|
|
if (instance == null)
|
|
|
|
return;
|
|
|
|
|
2019-08-29 02:15:52 +02:00
|
|
|
ItemStack usedItem = hand == Player.Hand.MAIN ? playerInventory.getItemInMainHand() : playerInventory.getItemInOffHand();
|
2019-09-01 06:18:41 +02:00
|
|
|
if (!usedItem.getMaterial().isBlock()) {
|
|
|
|
//instance.setBlock(blockPosition.clone().add(0, 1, 0), (short) 10);
|
2019-08-29 02:15:52 +02:00
|
|
|
return;
|
2019-09-01 06:18:41 +02:00
|
|
|
}
|
2019-08-29 02:15:52 +02:00
|
|
|
|
2019-08-20 17:41:07 +02:00
|
|
|
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;
|
|
|
|
|
2019-08-22 14:52:32 +02:00
|
|
|
blockPosition.add(offsetX, offsetY, offsetZ);
|
2019-09-01 06:18:41 +02:00
|
|
|
boolean intersectPlayer = player.getBoundingBox().intersect(blockPosition); // TODO check if collide with nearby players
|
2019-08-31 07:54:53 +02:00
|
|
|
if (!intersectPlayer) {
|
|
|
|
PlayerBlockPlaceEvent playerBlockPlaceEvent = new PlayerBlockPlaceEvent((short) 10, blockPosition, packet.hand);
|
|
|
|
player.callEvent(PlayerBlockPlaceEvent.class, playerBlockPlaceEvent);
|
|
|
|
if (!playerBlockPlaceEvent.isCancelled()) {
|
|
|
|
instance.setBlock(blockPosition, "custom_block"); // TODO set useItem's block instead
|
|
|
|
if (playerBlockPlaceEvent.doesConsumeBlock()) {
|
|
|
|
usedItem.setAmount((byte) (usedItem.getAmount() - 1));
|
|
|
|
if (usedItem.getAmount() <= 0)
|
|
|
|
usedItem = ItemStack.AIR_ITEM;
|
|
|
|
if (hand == Player.Hand.OFF) {
|
|
|
|
playerInventory.setItemInOffHand(usedItem);
|
|
|
|
} else { // Main
|
|
|
|
playerInventory.setItemInMainHand(usedItem);
|
|
|
|
}
|
2019-08-29 02:15:52 +02:00
|
|
|
}
|
2019-08-31 07:54:53 +02:00
|
|
|
} else {
|
|
|
|
Chunk chunk = instance.getChunkAt(blockPosition);
|
2019-09-01 06:18:41 +02:00
|
|
|
instance.sendChunkSectionUpdate(chunk, ChunkUtils.getSectionAt(blockPosition.getY()), player);
|
2019-08-29 02:15:52 +02:00
|
|
|
}
|
2019-08-20 17:41:07 +02:00
|
|
|
}
|
|
|
|
player.getInventory().refreshSlot(player.getHeldSlot());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|