2020-04-24 03:25:58 +02:00
|
|
|
package net.minestom.server.listener;
|
2019-08-20 17:41:07 +02:00
|
|
|
|
2020-04-24 03:25:58 +02:00
|
|
|
import net.minestom.server.MinecraftServer;
|
|
|
|
import net.minestom.server.data.Data;
|
|
|
|
import net.minestom.server.entity.Entity;
|
|
|
|
import net.minestom.server.entity.GameMode;
|
|
|
|
import net.minestom.server.entity.Player;
|
2020-05-07 15:46:21 +02:00
|
|
|
import net.minestom.server.event.player.PlayerBlockInteractEvent;
|
|
|
|
import net.minestom.server.event.player.PlayerBlockPlaceEvent;
|
|
|
|
import net.minestom.server.event.player.PlayerUseItemOnBlockEvent;
|
2020-04-24 03:25:58 +02:00
|
|
|
import net.minestom.server.instance.Chunk;
|
|
|
|
import net.minestom.server.instance.Instance;
|
|
|
|
import net.minestom.server.instance.block.Block;
|
2020-06-28 23:11:40 +02:00
|
|
|
import net.minestom.server.instance.block.BlockFace;
|
2020-04-24 03:25:58 +02:00
|
|
|
import net.minestom.server.instance.block.BlockManager;
|
|
|
|
import net.minestom.server.instance.block.CustomBlock;
|
|
|
|
import net.minestom.server.instance.block.rule.BlockPlacementRule;
|
|
|
|
import net.minestom.server.inventory.PlayerInventory;
|
|
|
|
import net.minestom.server.item.ItemStack;
|
|
|
|
import net.minestom.server.item.Material;
|
|
|
|
import net.minestom.server.network.packet.client.play.ClientPlayerBlockPlacementPacket;
|
2021-02-28 19:37:31 +01:00
|
|
|
import net.minestom.server.network.packet.server.play.BlockChangePacket;
|
2020-04-24 03:25:58 +02:00
|
|
|
import net.minestom.server.utils.BlockPosition;
|
2020-08-13 19:00:19 +02:00
|
|
|
import net.minestom.server.utils.Direction;
|
2020-05-25 13:46:48 +02:00
|
|
|
import net.minestom.server.utils.chunk.ChunkUtils;
|
2020-12-20 03:38:36 +01:00
|
|
|
import net.minestom.server.utils.validate.Check;
|
2019-08-20 17:41:07 +02:00
|
|
|
|
2020-04-21 15:31:41 +02:00
|
|
|
import java.util.Set;
|
|
|
|
|
2019-08-20 17:41:07 +02:00
|
|
|
public class BlockPlacementListener {
|
|
|
|
|
2020-12-20 03:38:36 +01:00
|
|
|
private static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
|
|
|
|
|
2019-08-20 17:41:07 +02:00
|
|
|
public static void listener(ClientPlayerBlockPlacementPacket packet, Player player) {
|
2020-07-23 05:36:15 +02:00
|
|
|
final PlayerInventory playerInventory = player.getInventory();
|
|
|
|
final Player.Hand hand = packet.hand;
|
|
|
|
final BlockFace blockFace = packet.blockFace;
|
|
|
|
final BlockPosition blockPosition = packet.blockPosition;
|
2020-08-13 19:00:19 +02:00
|
|
|
final Direction direction = blockFace.toDirection();
|
2019-08-20 17:41:07 +02:00
|
|
|
|
2020-07-23 05:36:15 +02:00
|
|
|
final Instance instance = player.getInstance();
|
2019-08-20 17:41:07 +02:00
|
|
|
if (instance == null)
|
|
|
|
return;
|
|
|
|
|
2020-12-20 03:38:36 +01:00
|
|
|
// Prevent outdated/modified client data
|
|
|
|
if (!ChunkUtils.isLoaded(instance.getChunkAt(blockPosition))) {
|
|
|
|
// Client tried to place a block in an unloaded chunk, ignore the request
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-13 19:00:19 +02:00
|
|
|
final ItemStack usedItem = player.getItemInHand(hand);
|
|
|
|
|
2020-04-21 15:31:41 +02:00
|
|
|
// Interact at block
|
2020-08-13 19:00:19 +02:00
|
|
|
final boolean cancel = usedItem.onUseOnBlock(player, hand, blockPosition, direction);
|
2020-07-24 16:11:48 +02:00
|
|
|
PlayerBlockInteractEvent playerBlockInteractEvent = new PlayerBlockInteractEvent(player, blockPosition, hand, blockFace);
|
2020-08-13 19:00:19 +02:00
|
|
|
playerBlockInteractEvent.setCancelled(cancel);
|
|
|
|
playerBlockInteractEvent.setBlockingItemUse(cancel);
|
2020-03-30 19:48:25 +02:00
|
|
|
player.callCancellableEvent(PlayerBlockInteractEvent.class, playerBlockInteractEvent, () -> {
|
2020-07-23 05:36:15 +02:00
|
|
|
final CustomBlock customBlock = instance.getCustomBlock(blockPosition);
|
2020-03-30 19:48:25 +02:00
|
|
|
if (customBlock != null) {
|
2020-07-23 05:36:15 +02:00
|
|
|
final Data data = instance.getBlockData(blockPosition);
|
|
|
|
final boolean blocksItem = customBlock.onInteract(player, hand, blockPosition, data);
|
|
|
|
if (blocksItem) {
|
2020-04-28 18:12:06 +02:00
|
|
|
playerBlockInteractEvent.setBlockingItemUse(true);
|
|
|
|
}
|
2020-03-30 19:48:25 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-21 18:48:15 +02:00
|
|
|
if (playerBlockInteractEvent.isBlockingItemUse()) {
|
2020-04-28 18:12:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-18 00:28:06 +01:00
|
|
|
final Material useMaterial = usedItem.getMaterial();
|
|
|
|
|
|
|
|
// Verify if the player can place the block
|
2021-02-28 19:37:31 +01:00
|
|
|
boolean canPlaceBlock = true;
|
2020-12-18 00:28:06 +01:00
|
|
|
{
|
|
|
|
if (useMaterial == Material.AIR) { // Can't place air
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-28 19:37:31 +01:00
|
|
|
//Check if the player is allowed to place blocks based on their game mode
|
|
|
|
if (player.getGameMode() == GameMode.SPECTATOR) {
|
|
|
|
canPlaceBlock = false; //Spectators can't place blocks
|
|
|
|
} else if (player.getGameMode() == GameMode.ADVENTURE) {
|
|
|
|
//Check if the block can placed on the block
|
|
|
|
canPlaceBlock = usedItem.canPlaceOn(instance.getBlock(blockPosition).getName());
|
|
|
|
}
|
2019-09-01 06:18:41 +02:00
|
|
|
}
|
2019-08-29 02:15:52 +02:00
|
|
|
|
2020-04-21 15:31:41 +02:00
|
|
|
// Get the newly placed block position
|
2020-07-21 18:48:15 +02:00
|
|
|
final int offsetX = blockFace == BlockFace.WEST ? -1 : blockFace == BlockFace.EAST ? 1 : 0;
|
|
|
|
final int offsetY = blockFace == BlockFace.BOTTOM ? -1 : blockFace == BlockFace.TOP ? 1 : 0;
|
|
|
|
final int offsetZ = blockFace == BlockFace.NORTH ? -1 : blockFace == BlockFace.SOUTH ? 1 : 0;
|
2019-08-20 17:41:07 +02:00
|
|
|
|
2019-08-22 14:52:32 +02:00
|
|
|
blockPosition.add(offsetX, offsetY, offsetZ);
|
2020-04-21 15:31:41 +02:00
|
|
|
|
2021-03-02 23:37:46 +01:00
|
|
|
if (!canPlaceBlock) {
|
2021-02-28 19:37:31 +01:00
|
|
|
//Send a block change with AIR as block to keep the client in sync,
|
|
|
|
//using refreshChunk results in the client not being in sync
|
|
|
|
//after rapid invalid block placements
|
|
|
|
BlockChangePacket blockChangePacket = new BlockChangePacket();
|
|
|
|
blockChangePacket.blockPosition = blockPosition;
|
|
|
|
blockChangePacket.blockStateId = Block.AIR.getBlockId();
|
|
|
|
player.getPlayerConnection().sendPacket(blockChangePacket);
|
|
|
|
return;
|
|
|
|
}
|
2020-04-21 15:31:41 +02:00
|
|
|
|
2020-07-21 18:48:15 +02:00
|
|
|
final Chunk chunk = instance.getChunkAt(blockPosition);
|
2020-12-20 03:24:29 +01:00
|
|
|
|
2020-12-20 03:38:36 +01:00
|
|
|
Check.stateCondition(!ChunkUtils.isLoaded(chunk),
|
|
|
|
"A player tried to place a block in the border of a loaded chunk " + blockPosition);
|
2020-12-20 03:24:29 +01:00
|
|
|
|
2020-07-24 18:23:15 +02:00
|
|
|
// The concerned chunk will be send to the player if an error occur
|
|
|
|
// This will ensure that the player has the correct version of the chunk
|
2020-04-21 15:31:41 +02:00
|
|
|
boolean refreshChunk = false;
|
|
|
|
|
2020-12-18 00:28:06 +01:00
|
|
|
if (useMaterial.isBlock()) {
|
2020-10-24 00:17:23 +02:00
|
|
|
if (!chunk.isReadOnly()) {
|
2020-12-18 00:28:06 +01:00
|
|
|
final Block block = useMaterial.getBlock();
|
2020-10-24 00:17:23 +02:00
|
|
|
final Set<Entity> entities = instance.getChunkEntities(chunk);
|
|
|
|
// Check if the player is trying to place a block in an entity
|
2021-01-13 06:32:42 +01:00
|
|
|
boolean intersect = player.getBoundingBox().intersect(blockPosition);
|
|
|
|
if (!intersect && block.isSolid()) {
|
2020-10-24 00:17:23 +02:00
|
|
|
for (Entity entity : entities) {
|
2021-01-13 06:34:09 +01:00
|
|
|
// 'player' has already been checked
|
|
|
|
if (entity == player)
|
|
|
|
continue;
|
|
|
|
|
2020-10-24 00:17:23 +02:00
|
|
|
intersect = entity.getBoundingBox().intersect(blockPosition);
|
|
|
|
if (intersect)
|
|
|
|
break;
|
|
|
|
}
|
2020-04-25 20:40:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-24 00:17:23 +02:00
|
|
|
if (!intersect) {
|
|
|
|
|
2020-12-20 03:24:29 +01:00
|
|
|
// BlockPlaceEvent check
|
|
|
|
PlayerBlockPlaceEvent playerBlockPlaceEvent = new PlayerBlockPlaceEvent(player, block, blockPosition, packet.hand);
|
2020-10-24 00:17:23 +02:00
|
|
|
playerBlockPlaceEvent.consumeBlock(player.getGameMode() != GameMode.CREATIVE);
|
|
|
|
|
|
|
|
player.callEvent(PlayerBlockPlaceEvent.class, playerBlockPlaceEvent);
|
2020-12-20 03:24:29 +01:00
|
|
|
if (!playerBlockPlaceEvent.isCancelled()) {
|
|
|
|
|
|
|
|
// BlockPlacementRule check
|
2021-03-02 23:37:46 +01:00
|
|
|
short blockStateId = playerBlockPlaceEvent.getBlockStateId();
|
|
|
|
final Block resultBlock = Block.fromStateId(blockStateId);
|
2020-12-20 03:38:36 +01:00
|
|
|
final BlockPlacementRule blockPlacementRule = BLOCK_MANAGER.getBlockPlacementRule(resultBlock);
|
2021-03-02 23:37:46 +01:00
|
|
|
if (blockPlacementRule != null) {
|
|
|
|
// Get id from block placement rule instead of the event
|
|
|
|
blockStateId = blockPlacementRule.blockPlace(instance, resultBlock, blockFace, blockPosition, player);
|
|
|
|
}
|
2020-12-20 03:24:29 +01:00
|
|
|
final boolean placementRuleCheck = blockStateId != BlockPlacementRule.CANCEL_CODE;
|
|
|
|
|
|
|
|
if (placementRuleCheck) {
|
|
|
|
|
|
|
|
// Place the block
|
|
|
|
final short customBlockId = playerBlockPlaceEvent.getCustomBlockId();
|
2021-01-07 16:12:56 +01:00
|
|
|
final Data blockData = playerBlockPlaceEvent.getBlockData(); // Possibly null
|
|
|
|
instance.setSeparateBlocks(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(),
|
|
|
|
blockStateId, customBlockId, blockData);
|
2020-02-17 17:33:53 +01:00
|
|
|
|
2020-12-20 03:24:29 +01:00
|
|
|
// Block consuming
|
|
|
|
if (playerBlockPlaceEvent.doesConsumeBlock()) {
|
|
|
|
// Consume the block in the player's hand
|
|
|
|
final ItemStack newUsedItem = usedItem.consume(1);
|
|
|
|
|
|
|
|
if (newUsedItem != null) {
|
|
|
|
playerInventory.setItemInHand(hand, newUsedItem);
|
|
|
|
}
|
2020-10-24 00:17:23 +02:00
|
|
|
}
|
2020-12-20 03:24:29 +01:00
|
|
|
} else {
|
|
|
|
refreshChunk = true;
|
2020-04-25 20:40:08 +02:00
|
|
|
}
|
2020-10-24 00:17:23 +02:00
|
|
|
} else {
|
|
|
|
refreshChunk = true;
|
2019-08-31 07:54:53 +02:00
|
|
|
}
|
2020-04-25 20:40:08 +02:00
|
|
|
} else {
|
|
|
|
refreshChunk = true;
|
2019-08-29 02:15:52 +02:00
|
|
|
}
|
2019-08-31 07:54:53 +02:00
|
|
|
} else {
|
2020-04-21 15:31:41 +02:00
|
|
|
refreshChunk = true;
|
2019-08-29 02:15:52 +02:00
|
|
|
}
|
2020-04-20 18:46:39 +02:00
|
|
|
} else {
|
2020-07-24 18:23:15 +02:00
|
|
|
// Player didn't try to place a block but interacted with one
|
2021-03-12 04:23:27 +01:00
|
|
|
final BlockPosition usePosition = blockPosition.clone().subtract(offsetX, offsetY, offsetZ);
|
|
|
|
PlayerUseItemOnBlockEvent event = new PlayerUseItemOnBlockEvent(player, hand, usedItem, usePosition, direction);
|
2020-05-03 22:44:38 +02:00
|
|
|
player.callEvent(PlayerUseItemOnBlockEvent.class, event);
|
2020-04-21 15:31:41 +02:00
|
|
|
refreshChunk = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Refresh chunk section if needed
|
|
|
|
if (refreshChunk) {
|
2020-08-16 00:53:42 +02:00
|
|
|
chunk.sendChunkSectionUpdate(ChunkUtils.getSectionAt(blockPosition.getY()), player);
|
2019-08-20 17:41:07 +02:00
|
|
|
}
|
2020-04-21 15:31:41 +02:00
|
|
|
|
2019-08-20 17:41:07 +02:00
|
|
|
player.getInventory().refreshSlot(player.getHeldSlot());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|