#1160: Add more bell API

By: Parker Hawke <hawkeboyz2@hotmail.com>
This commit is contained in:
CraftBukkit/Spigot
2023-04-02 14:35:13 +10:00
parent a9bdb77af1
commit 496189d962
4 changed files with 127 additions and 2 deletions

View File

@@ -1,12 +1,70 @@
package org.bukkit.craftbukkit.block;
import com.google.common.base.Preconditions;
import net.minecraft.core.EnumDirection;
import net.minecraft.world.level.block.BlockBell;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.TileEntity;
import net.minecraft.world.level.block.entity.TileEntityBell;
import org.bukkit.World;
import org.bukkit.block.Bell;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.entity.CraftEntity;
import org.bukkit.entity.Entity;
public class CraftBell extends CraftBlockEntityState<TileEntityBell> implements Bell {
public CraftBell(World world, TileEntityBell tileEntity) {
super(world, tileEntity);
}
@Override
public boolean ring(Entity entity, BlockFace direction) {
Preconditions.checkArgument(direction == null || direction.isCartesian(), "direction must be cartesian, given %s", direction);
TileEntity tileEntity = getTileEntityFromWorld();
if (tileEntity == null) {
return false;
}
net.minecraft.world.entity.Entity nmsEntity = (entity != null) ? ((CraftEntity) entity).getHandle() : null;
EnumDirection enumDirection = CraftBlock.blockFaceToNotch(direction);
return ((BlockBell) Blocks.BELL).attemptToRing(nmsEntity, world.getHandle(), getPosition(), enumDirection);
}
@Override
public boolean ring(Entity entity) {
return ring(entity, null);
}
@Override
public boolean ring(BlockFace direction) {
return ring(null, direction);
}
@Override
public boolean ring() {
return ring(null, null);
}
@Override
public boolean isShaking() {
return getSnapshot().shaking;
}
@Override
public int getShakingTicks() {
return getSnapshot().ticks;
}
@Override
public boolean isResonating() {
return getSnapshot().resonating;
}
@Override
public int getResonatingTicks() {
return isResonating() ? getSnapshot().ticks : 0;
}
}

View File

@@ -11,12 +11,12 @@ import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import net.minecraft.core.BlockPosition;
import net.minecraft.core.EnumDirection;
import net.minecraft.network.protocol.game.PacketPlayInCloseWindow;
import net.minecraft.resources.MinecraftKey;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.EntityPlayer;
import net.minecraft.server.level.WorldServer;
import net.minecraft.tags.DamageTypeTags;
@@ -24,7 +24,6 @@ import net.minecraft.util.Unit;
import net.minecraft.world.EnumHand;
import net.minecraft.world.IInventory;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.damagesource.DamageType;
import net.minecraft.world.damagesource.DamageTypes;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.entity.Entity;
@@ -131,6 +130,8 @@ import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Result;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BellResonateEvent;
import org.bukkit.event.block.BellRingEvent;
import org.bukkit.event.block.BlockDamageAbortEvent;
import org.bukkit.event.block.BlockDamageEvent;
import org.bukkit.event.block.BlockDropItemEvent;
@@ -340,6 +341,21 @@ public class CraftEventFactory {
return tradeSelectEvent;
}
public static boolean handleBellRingEvent(World world, BlockPosition position, EnumDirection direction, Entity entity) {
Block block = CraftBlock.at(world, position);
BlockFace bukkitDirection = CraftBlock.notchToBlockFace(direction);
BellRingEvent event = new BellRingEvent(block, bukkitDirection, (entity != null) ? entity.getBukkitEntity() : null);
Bukkit.getPluginManager().callEvent(event);
return !event.isCancelled();
}
public static Stream<EntityLiving> handleBellResonateEvent(World world, BlockPosition position, List<LivingEntity> bukkitEntities) {
Block block = CraftBlock.at(world, position);
BellResonateEvent event = new BellResonateEvent(block, bukkitEntities);
Bukkit.getPluginManager().callEvent(event);
return event.getResonatedEntities().stream().map((bukkitEntity) -> ((CraftLivingEntity) bukkitEntity).getHandle());
}
/**
* Block place methods
*/