Added PlayerBlockPlaceEvent#setBlockData

This commit is contained in:
themode 2021-01-07 16:12:56 +01:00
parent 349364e15d
commit 7c5bb7126a
2 changed files with 28 additions and 5 deletions

View File

@ -1,6 +1,7 @@
package net.minestom.server.event.player; package net.minestom.server.event.player;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.data.Data;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent; import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent; import net.minestom.server.event.PlayerEvent;
@ -8,7 +9,9 @@ import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockManager; import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.instance.block.CustomBlock; import net.minestom.server.instance.block.CustomBlock;
import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/** /**
* Called when a player tries placing a block. * Called when a player tries placing a block.
@ -19,6 +22,7 @@ public class PlayerBlockPlaceEvent extends PlayerEvent implements CancellableEve
private short blockStateId; private short blockStateId;
private short customBlockId; private short customBlockId;
private Data blockData;
private final BlockPosition blockPosition; private final BlockPosition blockPosition;
private final Player.Hand hand; private final Player.Hand hand;
@ -52,6 +56,7 @@ public class PlayerBlockPlaceEvent extends PlayerEvent implements CancellableEve
*/ */
public void setCustomBlock(short customBlockId) { public void setCustomBlock(short customBlockId) {
final CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId); final CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
Check.notNull(customBlock, "The custom block with the id '" + customBlockId + "' does not exist");
setCustomBlock(customBlock); setCustomBlock(customBlock);
} }
@ -62,6 +67,7 @@ public class PlayerBlockPlaceEvent extends PlayerEvent implements CancellableEve
*/ */
public void setCustomBlock(@NotNull String customBlockId) { public void setCustomBlock(@NotNull String customBlockId) {
final CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId); final CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
Check.notNull(customBlock, "The custom block with the identifier '" + customBlockId + "' does not exist");
setCustomBlock(customBlock); setCustomBlock(customBlock);
} }
@ -104,6 +110,25 @@ public class PlayerBlockPlaceEvent extends PlayerEvent implements CancellableEve
this.blockStateId = blockStateId; this.blockStateId = blockStateId;
} }
/**
* Gets the data that the (not placed yet) block should have
*
* @return the block data, null if not any
*/
@Nullable
public Data getBlockData() {
return blockData;
}
/**
* Sets the data of the block to place.
*
* @param blockData the block data, null if not any
*/
public void setBlockData(@Nullable Data blockData) {
this.blockData = blockData;
}
/** /**
* Gets the block position. * Gets the block position.
* *

View File

@ -133,11 +133,9 @@ public class BlockPlacementListener {
// Place the block // Place the block
final short customBlockId = playerBlockPlaceEvent.getCustomBlockId(); final short customBlockId = playerBlockPlaceEvent.getCustomBlockId();
if (customBlockId != 0) { final Data blockData = playerBlockPlaceEvent.getBlockData(); // Possibly null
instance.setSeparateBlocks(blockPosition, blockStateId, customBlockId); instance.setSeparateBlocks(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(),
} else { blockStateId, customBlockId, blockData);
instance.setBlockStateId(blockPosition, blockStateId);
}
// Block consuming // Block consuming
if (playerBlockPlaceEvent.doesConsumeBlock()) { if (playerBlockPlaceEvent.doesConsumeBlock()) {