Allow PlayerBlockPlaceEvent callbacks to change the block placed.

Can be used to place custom blocks in replacement of normal ones.
This commit is contained in:
jglrxavpok 2020-04-28 13:47:41 +02:00
parent 4c6d31fac1
commit f8f649852b
4 changed files with 28 additions and 4 deletions

View File

@ -10,6 +10,7 @@ import net.minestom.server.entity.*;
import net.minestom.server.entity.damage.DamageType;
import net.minestom.server.event.*;
import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.instance.block.Block;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryType;
import net.minestom.server.item.ItemStack;
@ -107,6 +108,10 @@ public class PlayerInit {
if (event.getHand() != Player.Hand.MAIN)
return;
if(event.getBlockId() == Block.STONE.getBlockId()) {
event.setCustomBlockId((short) 2); // custom stone block
}
/*for (Player p : player.getInstance().getPlayers()) {
if (p != player)
p.teleport(player.getPosition());

View File

@ -16,7 +16,7 @@ public class StoneBlock extends CustomBlock {
@Override
public void onPlace(Instance instance, BlockPosition blockPosition, Data data) {
System.out.println("PLACED");
}
@Override

View File

@ -6,18 +6,32 @@ import net.minestom.server.utils.BlockPosition;
public class PlayerBlockPlaceEvent extends CancellableEvent {
private short blockId;
private short customBlockId;
private BlockPosition blockPosition;
private Player.Hand hand;
private boolean consumeBlock;
public PlayerBlockPlaceEvent(short blockId, BlockPosition blockPosition, Player.Hand hand) {
public PlayerBlockPlaceEvent(short blockId, short customBlockId, BlockPosition blockPosition, Player.Hand hand) {
this.blockId = blockId;
this.customBlockId = customBlockId;
this.blockPosition = blockPosition;
this.hand = hand;
this.consumeBlock = true;
}
public void setCustomBlockId(short customBlockId) {
this.customBlockId = customBlockId;
}
public short getCustomBlockId() {
return customBlockId;
}
public void setBlockId(short blockId) {
this.blockId = blockId;
}
public short getBlockId() {
return blockId;
}

View File

@ -78,7 +78,7 @@ public class BlockPlacementListener {
}
if (!intersect) {
PlayerBlockPlaceEvent playerBlockPlaceEvent = new PlayerBlockPlaceEvent((short) 10, blockPosition, packet.hand);
PlayerBlockPlaceEvent playerBlockPlaceEvent = new PlayerBlockPlaceEvent(material.getBlock().getBlockId(), (short)0, blockPosition, packet.hand);
playerBlockPlaceEvent.consumeBlock(player.getGameMode() != GameMode.CREATIVE);
// BlockPlacementRule check
@ -91,7 +91,12 @@ public class BlockPlacementListener {
player.callEvent(PlayerBlockPlaceEvent.class, playerBlockPlaceEvent);
if (!playerBlockPlaceEvent.isCancelled() && canPlace) {
instance.setBlock(blockPosition, material.getBlock());
short customBlockId = playerBlockPlaceEvent.getCustomBlockId();
if(customBlockId != 0) {
instance.setCustomBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), playerBlockPlaceEvent.getCustomBlockId());
} else {
instance.setBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), playerBlockPlaceEvent.getBlockId());
}
if (playerBlockPlaceEvent.doesConsumeBlock()) {
StackingRule stackingRule = usedItem.getStackingRule();