From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Kieran Wallbanks Date: Thu, 11 Feb 2021 13:17:03 +0000 Subject: [PATCH] More note block events diff --git a/src/main/java/net/minecraft/world/level/block/BlockNote.java b/src/main/java/net/minecraft/world/level/block/BlockNote.java index 5b07037c0501d3ecf0c82f40b78bd135b81dbb75..6ad42f85fbdde0dd2c46556afe22fef43fc3cc99 100644 --- a/src/main/java/net/minecraft/world/level/block/BlockNote.java +++ b/src/main/java/net/minecraft/world/level/block/BlockNote.java @@ -21,6 +21,12 @@ import net.minecraft.world.level.block.state.properties.BlockStateEnum; import net.minecraft.world.level.block.state.properties.BlockStateInteger; import net.minecraft.world.level.block.state.properties.IBlockState; import net.minecraft.world.phys.MovingObjectPositionBlock; +// Haricot start +import ml.beancraft.haricot.event.block.NoteBlockPlaceEvent; +import ml.beancraft.haricot.event.block.NoteBlockUpdateEvent; +import org.bukkit.craftbukkit.block.CraftBlock; +import org.bukkit.craftbukkit.block.impl.CraftNote; +// Haricot end public class BlockNote extends Block { @@ -35,12 +41,32 @@ public class BlockNote extends Block { @Override public IBlockData getPlacedState(BlockActionContext blockactioncontext) { - return (IBlockData) this.getBlockData().set(BlockNote.INSTRUMENT, BlockPropertyInstrument.a(blockactioncontext.getWorld().getType(blockactioncontext.getClickPosition().down()))); + // Haricot start - note block api + NoteBlockPlaceEvent event = new NoteBlockPlaceEvent(new CraftBlock(blockactioncontext.getWorld(), blockactioncontext.getClickPosition()), + new CraftNote(this.blockStateList.getBlockData()), blockactioncontext.getItemStack().asBukkitMirror()); + event.callEvent(); + + return this.getBlockData().set(INSTRUMENT, BlockPropertyInstrument.valueOf(event.getData().getInstrument().getName().toUpperCase())) + .set(NOTE, (int) event.getData().getNote().getId()) + .set(POWERED, event.getData().isPowered()); + // Haricot end } @Override public IBlockData updateState(IBlockData iblockdata, EnumDirection enumdirection, IBlockData iblockdata1, GeneratorAccess generatoraccess, BlockPosition blockposition, BlockPosition blockposition1) { - return enumdirection == EnumDirection.DOWN ? (IBlockData) iblockdata.set(BlockNote.INSTRUMENT, BlockPropertyInstrument.a(iblockdata1)) : super.updateState(iblockdata, enumdirection, iblockdata1, generatoraccess, blockposition, blockposition1); + // Haricot start - note block api + NoteBlockUpdateEvent event = new NoteBlockUpdateEvent(new CraftBlock(generatoraccess, blockposition), new CraftNote(iblockdata), + new CraftNote(enumdirection == EnumDirection.DOWN ? iblockdata.set(BlockNote.INSTRUMENT, BlockPropertyInstrument.a(iblockdata1)) : iblockdata)); + event.callEvent(); + + if (!event.isCancelled()) { + return iblockdata.set(INSTRUMENT, BlockPropertyInstrument.valueOf(event.getNewData().getInstrument().getName().toUpperCase())) + .set(NOTE, (int) event.getNewData().getNote().getId()) + .set(POWERED, event.getNewData().isPowered()); + } else { + return iblockdata; + } + // Haricot end } @Override @@ -53,7 +79,18 @@ public class BlockNote extends Block { iblockdata = world.getType(blockposition); // CraftBukkit - SPIGOT-5617: update in case changed in event } - world.setTypeAndData(blockposition, (IBlockData) iblockdata.set(BlockNote.POWERED, flag1), 3); + // Haricot start - note block api + NoteBlockUpdateEvent event = new NoteBlockUpdateEvent(new CraftBlock(world, blockposition), new CraftNote(iblockdata), + new CraftNote(iblockdata.set(BlockNote.POWERED, flag1))); + event.callEvent(); + + if (!event.isCancelled()) { + world.setTypeAndData(blockposition, iblockdata + .set(INSTRUMENT, BlockPropertyInstrument.valueOf(event.getNewData().getInstrument().getName().toUpperCase())) + .set(NOTE, (int) event.getNewData().getNote().getId()) + .set(POWERED, event.getNewData().isPowered()), 3); + } + // Haricot end } } @@ -74,10 +111,21 @@ public class BlockNote extends Block { if (world.isClientSide) { return EnumInteractionResult.SUCCESS; } else { - iblockdata = (IBlockData) iblockdata.a((IBlockState) BlockNote.NOTE); - world.setTypeAndData(blockposition, iblockdata, 3); - this.play(world, blockposition, iblockdata); // CraftBukkit - entityhuman.a(StatisticList.TUNE_NOTEBLOCK); + // Haricot start - note block api + NoteBlockUpdateEvent event = new NoteBlockUpdateEvent(new CraftBlock(world, blockposition), new CraftNote(iblockdata), + new CraftNote(iblockdata.a(NOTE))); + event.callEvent(); + + if (!event.isCancelled()) { + IBlockData newData = iblockdata + .set(INSTRUMENT, BlockPropertyInstrument.valueOf(event.getNewData().getInstrument().getName().toUpperCase())) + .set(NOTE, (int) event.getNewData().getNote().getId()) + .set(POWERED, event.getNewData().isPowered()); + world.setTypeAndData(blockposition, newData, 3); + this.play(world, blockposition, newData); + entityhuman.a(StatisticList.TUNE_NOTEBLOCK); + } + // Haricot end return EnumInteractionResult.CONSUME; } }