2019-08-20 17:41:07 +02:00
|
|
|
package fr.themode.minestom.listener;
|
|
|
|
|
|
|
|
import fr.themode.minestom.entity.Player;
|
|
|
|
import fr.themode.minestom.event.BlockPlaceEvent;
|
|
|
|
import fr.themode.minestom.instance.Chunk;
|
|
|
|
import fr.themode.minestom.instance.Instance;
|
|
|
|
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-08-20 17:41:07 +02:00
|
|
|
|
|
|
|
public class BlockPlacementListener {
|
|
|
|
|
|
|
|
public static void listener(ClientPlayerBlockPlacementPacket packet, Player player) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
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-08-23 23:55:09 +02:00
|
|
|
BlockPlaceEvent blockPlaceEvent = new BlockPlaceEvent((short) 10, blockPosition, packet.hand);
|
2019-08-20 17:41:07 +02:00
|
|
|
player.callEvent(BlockPlaceEvent.class, blockPlaceEvent);
|
|
|
|
if (!blockPlaceEvent.isCancelled()) {
|
2019-08-22 14:52:32 +02:00
|
|
|
instance.setBlock(blockPosition, "custom_block");
|
2019-08-20 17:41:07 +02:00
|
|
|
// TODO consume block in hand for survival players
|
|
|
|
} else {
|
2019-08-21 16:50:52 +02:00
|
|
|
Chunk chunk = instance.getChunkAt(blockPosition);
|
2019-08-20 17:41:07 +02:00
|
|
|
instance.sendChunkUpdate(player, chunk);
|
|
|
|
}
|
|
|
|
player.getInventory().refreshSlot(player.getHeldSlot());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|