下面列出了怎么用org.bukkit.event.entity.EntityChangeBlockEvent的API类实例代码及写法,或者点击链接到github查看源代码。
private void replaceBlock(BlockDrops drops, Block block, MatchPlayer player) {
if (drops.replacement != null) {
EntityChangeBlockEvent event =
new EntityChangeBlockEvent(
player.getBukkit(),
block,
drops.replacement.getItemType(),
drops.replacement.getData());
match.callEvent(event);
if (!event.isCancelled()) {
BlockState state = block.getState();
state.setType(drops.replacement.getItemType());
state.setData(drops.replacement);
state.update(true, true);
}
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onMobChangeBlock(EntityChangeBlockEvent event) {
if (!useEnhanceProtection) {
return;
}
final Shop shop = getShopNature(event.getBlock().getLocation(), true);
if (shop == null) {
return;
}
if (plugin.getConfig().getBoolean("protect.entity")) {
event.setCancelled(true);
return;
}
plugin.log("Deleting shop "+shop+" request by mob changing.");
shop.delete();
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onEntityChangeBlock(EntityChangeBlockEvent event) {
if (!LWC.ENABLED) {
return;
}
LWC lwc = LWC.getInstance();
Block block = event.getBlock();
if (!lwc.isProtectable(block)) {
return;
}
Protection protection = lwc.findProtection(block);
if (protection != null) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.NORMAL)
public void onEntityBlockChange(EntityChangeBlockEvent event) {
bcoord.setFromLocation(event.getBlock().getLocation());
StructureBlock sb = CivGlobal.getStructureBlock(bcoord);
if (sb != null) {
event.setCancelled(true);
return;
}
RoadBlock rb = CivGlobal.getRoadBlock(bcoord);
if (rb != null) {
event.setCancelled(true);
return;
}
CampBlock cb = CivGlobal.getCampBlock(bcoord);
if (cb != null) {
event.setCancelled(true);
return;
}
return;
}
/**
* Allows or prevents enderman griefing
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onEndermanGrief(final EntityChangeBlockEvent e) {
if (DEBUG) {
plugin.getLogger().info(e.getEventName());
}
if (!(e.getEntity() instanceof Enderman)) {
return;
}
if (!inWorld(e.getEntity())) {
return;
}
// Prevent Enderman griefing at spawn
if (plugin.getGrid() != null && plugin.getGrid().isAtSpawn(e.getEntity().getLocation())) {
e.setCancelled(true);
}
if (Settings.allowEndermanGriefing)
return;
// Stop the Enderman from griefing
// plugin.getLogger().info("Enderman stopped from griefing);
e.setCancelled(true);
}
/**
* Withers change blocks to air after they are hit (don't know why)
* This prevents this when the wither has been spawned by a visitor
* @param e - event
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void WitherChangeBlocks(final EntityChangeBlockEvent e) {
if (DEBUG) {
plugin.getLogger().info(e.getEventName());
}
// Only cover withers in the island world
if (e.getEntityType() != EntityType.WITHER || !IslandGuard.inWorld(e.getEntity()) ) {
return;
}
if (mobSpawnInfo.containsKey(e.getEntity())) {
// We know about this wither
if (DEBUG) {
plugin.getLogger().info("DEBUG: We know about this wither");
}
if (!mobSpawnInfo.get(e.getEntity()).inIslandSpace(e.getEntity().getLocation())) {
// Cancel the block changes
if (DEBUG) {
plugin.getLogger().info("DEBUG: cancelled wither block change");
}
e.setCancelled(true);
}
}
}
@EventHandler(ignoreCancelled = true)
public void onSandCannon(EntityChangeBlockEvent event) {
BukkitEntity entity = new BukkitEntity(event.getEntity());
if (manager.isPlotWorld(entity) && event.getEntityType().equals(EntityType.FALLING_BLOCK)) {
if (event.getTo().equals(Material.AIR)) {
entity.setMetadata("plotFallBlock", new FixedMetadataValue(plugin, event.getBlock().getLocation()));
} else {
List<MetadataValue> values = entity.getMetadata("plotFallBlock");
if (!values.isEmpty()) {
org.bukkit.Location spawn = (org.bukkit.Location) (values.get(0).value());
org.bukkit.Location createdNew = event.getBlock().getLocation();
if (spawn.getBlockX() != createdNew.getBlockX() || spawn.getBlockZ() != createdNew.getBlockZ()) {
event.setCancelled(true);
}
}
}
}
}
@EventHandler
public void onEntityChangeBlock(EntityChangeBlockEvent event) {
if (event.isCancelled()) {
return;
}
for (String gameName : Main.getGameNames()) {
Game game = Main.getGame(gameName);
if (GameCreator.isInArea(event.getBlock().getLocation(), game.getPos1(), game.getPos2())) {
if (game.getStatus() == GameStatus.RUNNING || game.getStatus() == GameStatus.GAME_END_CELEBRATING) {
if (event.getEntityType() == EntityType.FALLING_BLOCK
&& game.getOriginalOrInheritedAllowBlockFalling()) {
if (event.getBlock().getType() != event.getTo()) {
if (!game.getRegion().isBlockAddedDuringGame(event.getBlock().getLocation())) {
if (event.getBlock().getType() != Material.AIR) {
game.getRegion().putOriginalBlock(event.getBlock().getLocation(),
event.getBlock().getState());
}
game.getRegion().addBuiltDuringGame(event.getBlock().getLocation());
}
}
return; // allow block fall
}
}
if (game.getStatus() != GameStatus.DISABLED) {
event.setCancelled(true);
}
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockSpawnFalling(EntityChangeBlockEvent event) {
final Entity entity = event.getEntity();
if (entity instanceof FallingBlock) {
final GDEntity gdEntity = EntityTracker.getCachedEntity(event.getEntity().getEntityId());
if (gdEntity != null) {
final GDPermissionUser user = PermissionHolderCache.getInstance().getOrCreateUser(gdEntity.getOwnerUUID());
final GDClaimManager claimWorldManager = GriefDefenderPlugin.getInstance().dataStore.getClaimWorldManager(event.getBlock().getWorld().getUID());
final GDChunk gdChunk = claimWorldManager.getChunk(event.getBlock().getChunk());
gdChunk.addTrackedBlockPosition(event.getBlock(), user.getUniqueId(), PlayerTracker.Type.OWNER);
}
}
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onFallingBlockLand(BlockTransformEvent event) {
if (event.getCause() instanceof EntityChangeBlockEvent) {
Entity entity = ((EntityChangeBlockEvent) event.getCause()).getEntity();
if (entity instanceof FallingBlock && this.fallingBlocksThatWillNotLand.remove(entity)) {
event.setCancelled(true);
}
}
}
@EventWrapper
public void onEntityChangeBlock(final EntityChangeBlockEvent event) {
callEvent(
event,
event.getBlock().getState(),
BlockStates.cloneWithMaterial(event.getBlock(), event.getTo(), event.getData()),
Trackers.getOwner(event.getEntity()));
}
@EventHandler
public void onEntityChangeBlock(EntityChangeBlockEvent event) {
if (event.isCancelled()) {
return;
}
for (String gameName : Main.getGameNames()) {
Game game = Main.getGame(gameName);
if (GameCreator.isInArea(event.getBlock().getLocation(), game.getPos1(), game.getPos2())) {
if (game.getStatus() == GameStatus.RUNNING || game.getStatus() == GameStatus.GAME_END_CELEBRATING) {
if (event.getEntityType() == EntityType.FALLING_BLOCK
&& game.getOriginalOrInheritedAllowBlockFalling()) {
if (event.getBlock().getType() != event.getTo()) {
if (!game.getRegion().isBlockAddedDuringGame(event.getBlock().getLocation())) {
if (event.getBlock().getType() != Material.AIR) {
game.getRegion().putOriginalBlock(event.getBlock().getLocation(),
event.getBlock().getState());
}
game.getRegion().addBuiltDuringGame(event.getBlock().getLocation());
}
}
return; // allow block fall
}
}
if (game.getStatus() != GameStatus.DISABLED) {
event.setCancelled(true);
}
}
}
}
private void replaceBlock(BlockDrops drops, Block block, MatchPlayer player) {
if(drops.replacement != null) {
EntityChangeBlockEvent event = new EntityChangeBlockEvent(player.getBukkit(), block, drops.replacement);
getMatch().callEvent(event);
if(!event.isCancelled()) {
BlockState state = block.getState();
state.setType(drops.replacement.getItemType());
state.setData(drops.replacement);
state.update(true, true);
}
}
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onFallingBlockLand(BlockTransformEvent event) {
if(event.getCause() instanceof EntityChangeBlockEvent) {
Entity entity = ((EntityChangeBlockEvent) event.getCause()).getEntity();
if(entity instanceof FallingBlock && this.fallingBlocksThatWillNotLand.remove(entity)) {
event.setCancelled(true);
}
}
}
@EventWrapper
public void onEntityChangeBlock(final EntityChangeBlockEvent event) {
// Igniting TNT with an arrow is already handled from the ExplosionPrimeEvent
if(event.getEntity() instanceof Arrow &&
event.getBlock().getType() == Material.TNT &&
event.getTo() == Material.AIR) return;
callEvent(event, event.getBlock().getState(), BlockStateUtils.cloneWithMaterial(event.getBlock(), event.getToData()), entityResolver.getOwner(event.getEntity()));
}
@EventHandler(priority = EventPriority.LOWEST)
public void onBlockChange(EntityChangeBlockEvent event) {
if (physicsFreeze) {
event.setCancelled(true);
return;
}
Block block = event.getBlock();
int x = block.getX();
int z = block.getZ();
int cx = x >> 4;
int cz = z >> 4;
int[] count = getCount(cx, cz);
if (count[1] >= Settings.IMP.TICK_LIMITER.FALLING) {
event.setCancelled(true);
return;
}
if (event.getEntityType() == EntityType.FALLING_BLOCK) {
if (++count[1] >= Settings.IMP.TICK_LIMITER.FALLING) {
// Only cancel falling blocks when it's lagging
if (Fawe.get().getTimer().getTPS() < 18) {
cancelNearby(cx, cz);
if (rateLimit <= 0) {
rateLimit = 20;
Fawe.debug("[FAWE `tick-limiter`] Detected and cancelled falling block lag source at " + block.getLocation());
}
event.setCancelled(true);
return;
} else {
count[1] = 0;
}
}
}
}
@EventHandler
public void onLavaAbsorption(EntityChangeBlockEvent event) {
if (!plugin.getWorldManager().isSkyWorld(event.getBlock().getWorld())) {
return;
}
if (isLavaSource(event.getBlock().getBlockData())) {
if (event.getTo() != Material.LAVA) {
event.setCancelled(true);
// TODO: R4zorax - 21-07-2018: missing datavalue (might convert stuff - exploit)
ItemStack item = new ItemStack(event.getTo(), 1);
Location above = event.getBlock().getLocation().add(0, 1, 0);
event.getBlock().getWorld().dropItemNaturally(above, item);
}
}
}
@EventHandler
public void onBlockFall(EntityChangeBlockEvent e) {
if (seismicAxe == null || seismicAxe.isDisabled()) {
return;
}
if (e.getEntity().getType() == EntityType.FALLING_BLOCK && e.getEntity().hasMetadata("seismic_axe")) {
e.setCancelled(true);
e.getEntity().removeMetadata("seismic_axe", SlimefunPlugin.instance);
e.getEntity().remove();
}
}
@EventHandler(ignoreCancelled = true)
public void onWitherDestroy(EntityChangeBlockEvent e) {
if (e.getEntity().getType() == EntityType.WITHER) {
String id = BlockStorage.checkID(e.getBlock());
if (id != null) {
WitherProof witherproof = SlimefunPlugin.getRegistry().getWitherProofBlocks().get(id);
if (witherproof != null) {
e.setCancelled(true);
witherproof.onAttack(e.getBlock(), (Wither) e.getEntity());
}
}
}
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBlockFall(EntityChangeBlockEvent e) {
if (e.getEntity().getType() == EntityType.FALLING_BLOCK && BlockStorage.hasBlockInfo(e.getBlock())) {
e.setCancelled(true);
FallingBlock block = (FallingBlock) e.getEntity();
if (block.getDropItem()) {
block.getWorld().dropItemNaturally(block.getLocation(), new ItemStack(block.getBlockData().getMaterial(), 1));
}
}
}
@SuppressWarnings("deprecation")
@EventHandler
public void onEntityChangeBlockEvent(EntityChangeBlockEvent e) {
EntityType type = e.getEntityType();
if (type != EntityType.FALLING_BLOCK) {
return;
}
Entity entity = e.getEntity();
if (!fallingAnvils.contains(entity)) {
return;
}
Block block = e.getBlock();
Block under = block.getRelative(BlockFace.DOWN);
fallingAnvils.remove(entity);
e.setCancelled(true);
if (!game.canSpleef(under)) {
entity.remove();
return;
}
Material material = under.getType();
under.setType(Material.AIR);
World world = under.getWorld();
Sound anvilLandSound = Game.getSoundEnumType("ANVIL_LAND");
if (anvilLandSound != null) {
world.playSound(block.getLocation(), anvilLandSound, 1.0f, 1.0f);
}
if (game.getPropertyValue(GameProperty.PLAY_BLOCK_BREAK)) {
world.playEffect(under.getLocation(), Effect.STEP_SOUND, material.getId());
}
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onEntityChangeBlock(EntityChangeBlockEvent event) {
if (!PlayerVaults.getInstance().getConf().isSigns()) {
return;
}
blockChangeCheck(event.getBlock().getLocation());
}
@EventHandler(priority = EventPriority.LOWEST)
public void onEntityChangeBlockEvent(EntityChangeBlockEvent event) {
if (!GDFlags.BLOCK_BREAK) {
return;
}
final Block block = event.getBlock();
if (block.isEmpty()) {
return;
}
final World world = event.getBlock().getWorld();
if (!GriefDefenderPlugin.getInstance().claimsEnabledForWorld(world.getUID())) {
return;
}
final Location location = block.getLocation();
final GDClaim targetClaim = this.baseStorage.getClaimAt(location);
final Entity source = event.getEntity();
GDPermissionUser user = null;
if (source instanceof Tameable) {
final UUID uuid = NMSUtil.getInstance().getTameableOwnerUUID(source);
if (uuid != null) {
user = PermissionHolderCache.getInstance().getOrCreateUser(uuid);
}
}
if (user == null && !NMSUtil.getInstance().isEntityMonster(event.getEntity())) {
final GDEntity gdEntity = EntityTracker.getCachedEntity(event.getEntity().getEntityId());
if (gdEntity != null) {
user = PermissionHolderCache.getInstance().getOrCreateUser(gdEntity.getOwnerUUID());
}
if (user == null && source instanceof FallingBlock) {
// always allow blocks to fall if no user found
return;
}
}
final Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, location, targetClaim, Flags.BLOCK_BREAK, event.getEntity(), event.getBlock(), user, TrustTypes.BUILDER, true);
if (result == Tristate.FALSE) {
event.setCancelled(true);
return;
}
}
private ChangeEvent(final String pattern, final Checker<EntityChangeBlockEvent> c) {
this.pattern = pattern;
checker = c;
}
@Override
public boolean check(final Event e) {
if (!(e instanceof EntityChangeBlockEvent))
return false;
return event.checker.check((EntityChangeBlockEvent) e);
}
/**
* Trap TNT being primed by flaming arrows
* @param e - event
*/
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onTNTPrimed(final EntityChangeBlockEvent e) {
if (DEBUG) {
plugin.getLogger().info(e.getEventName());
plugin.getLogger().info("DEBUG: block = " + e.getBlock().getType());
plugin.getLogger().info("DEBUG: entity = " + e.getEntityType());
plugin.getLogger().info("DEBUG: material changing to " + e.getTo());
}
if (actionAllowed(e.getEntity().getLocation(), SettingsFlag.FIRE)) {
return;
}
if (e.getBlock() == null) {
return;
}
// Check for TNT
if (!e.getBlock().getType().equals(Material.TNT)) {
//plugin.getLogger().info("DEBUG: not tnt");
return;
}
// Check world
if (!inWorld(e.getBlock())) {
return;
}
// Check if this is on an island
Island island = plugin.getGrid().getIslandAt(e.getBlock().getLocation());
if (island == null || island.isSpawn()) {
return;
}
// Stop TNT from being damaged if it is being caused by a visitor with a flaming arrow
if (e.getEntity() instanceof Projectile) {
//plugin.getLogger().info("DEBUG: projectile");
Projectile projectile = (Projectile) e.getEntity();
// Find out who fired it
if (projectile.getShooter() instanceof Player) {
//plugin.getLogger().info("DEBUG: player shot arrow. Fire ticks = " + projectile.getFireTicks());
if (projectile.getFireTicks() > 0) {
//plugin.getLogger().info("DEBUG: arrow on fire");
Player shooter = (Player)projectile.getShooter();
if (!plugin.getGrid().locationIsAtHome(shooter, true, e.getBlock().getLocation())) {
//plugin.getLogger().info("DEBUG: shooter is not at home");
// Only say it once a second
// Debounce event (it can be called twice for the same action)
if (!tntBlocks.contains(e.getBlock().getLocation())) {
Util.sendMessage(shooter, ChatColor.RED + plugin.myLocale(shooter.getUniqueId()).islandProtected);
tntBlocks.add(e.getBlock().getLocation());
plugin.getServer().getScheduler().runTaskLater(plugin, new Runnable() {
@Override
public void run() {
tntBlocks.remove(e.getBlock().getLocation());
}}, 20L);
}
// Remove the arrow
projectile.remove();
e.setCancelled(true);
}
}
}
}
}
@EventHandler
public void onFall(EntityChangeBlockEvent event) {
if (event.getEntityType() == EntityType.FALLING_BLOCK && GuildHeartProtectionHandler.isGuildHeart(event.getBlock())) {
event.setCancelled(true);
}
}
@EventHandler
public void onEntityChangeBlock(EntityChangeBlockEvent event) {
if (place.getBlock().equals(event.getBlock()))
event.setCancelled(true);
}
@EventHandler
public void onEntityChangeBlock(EntityChangeBlockEvent event) {
if (!event.isCancelled() && region.contains(new BlockRegion(null, event.getBlock().getLocation().toVector())) && filter.evaluate(event, event.getBlock()).equals(FilterState.DENY)) {
event.setCancelled(true);
}
}
@EventHandler
public void onEntityChangeBlock(EntityChangeBlockEvent event) {
if (lava.contains(event.getBlock()))
event.setCancelled(true);
}