Minestom/src/main/java/fr/themode/minestom/listener/BlockPlacementListener.java

65 lines
3.2 KiB
Java

package fr.themode.minestom.listener;
import fr.themode.minestom.entity.GameMode;
import fr.themode.minestom.entity.Player;
import fr.themode.minestom.event.PlayerBlockPlaceEvent;
import fr.themode.minestom.instance.Chunk;
import fr.themode.minestom.instance.Instance;
import fr.themode.minestom.inventory.PlayerInventory;
import fr.themode.minestom.item.ItemStack;
import fr.themode.minestom.net.packet.client.play.ClientPlayerBlockPlacementPacket;
import fr.themode.minestom.net.packet.client.play.ClientPlayerDiggingPacket;
import fr.themode.minestom.utils.BlockPosition;
import fr.themode.minestom.utils.ChunkUtils;
public class BlockPlacementListener {
public static void listener(ClientPlayerBlockPlacementPacket packet, Player player) {
PlayerInventory playerInventory = player.getInventory();
Player.Hand hand = packet.hand;
ClientPlayerDiggingPacket.BlockFace blockFace = packet.blockFace;
BlockPosition blockPosition = packet.blockPosition;
Instance instance = player.getInstance();
if (instance == null)
return;
ItemStack usedItem = hand == Player.Hand.MAIN ? playerInventory.getItemInMainHand() : playerInventory.getItemInOffHand();
if (!usedItem.getMaterial().isBlock()) {
//instance.setBlock(blockPosition.clone().add(0, 1, 0), (short) 10);
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;
blockPosition.add(offsetX, offsetY, offsetZ);
boolean intersectPlayer = player.getBoundingBox().intersect(blockPosition); // TODO check if collide with nearby players
if (!intersectPlayer) {
PlayerBlockPlaceEvent playerBlockPlaceEvent = new PlayerBlockPlaceEvent((short) 10, blockPosition, packet.hand);
playerBlockPlaceEvent.consumeBlock(player.getGameMode() != GameMode.CREATIVE);
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);
}
}
} else {
Chunk chunk = instance.getChunkAt(blockPosition);
instance.sendChunkSectionUpdate(chunk, ChunkUtils.getSectionAt(blockPosition.getY()), player);
}
}
player.getInventory().refreshSlot(player.getHeldSlot());
}
}