Add tree fell event and make fall event cancellable

This commit is contained in:
electro2560 2019-01-20 14:59:45 -05:00 committed by Brianna O'Keefe
parent 407fbdccc0
commit 8f11177280
3 changed files with 55 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package com.songoda.ultimatetimber.api.events;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import com.songoda.ultimatetimber.treefall.TreeChecker;
@ -9,8 +10,10 @@ import com.songoda.ultimatetimber.treefall.TreeChecker;
/**
* Called when a tree will fall
*/
public class TreeFallEvent extends TreeEvent {
public class TreeFallEvent extends TreeEvent implements Cancellable {
boolean cancelled = false;
public TreeFallEvent(Player player, TreeChecker treeChecker, Block broke) {
super(player, treeChecker, broke);
}
@ -26,5 +29,14 @@ public class TreeFallEvent extends TreeEvent {
return handlers;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}

View File

@ -0,0 +1,30 @@
package com.songoda.ultimatetimber.api.events;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import com.songoda.ultimatetimber.treefall.TreeChecker;
/**
* Called when a tree fell
*/
public class TreeFellEvent extends TreeEvent {
public TreeFellEvent(Player player, TreeChecker treeChecker, Block broke) {
super(player, treeChecker, broke);
}
private static final HandlerList handlers = new HandlerList();
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -2,6 +2,7 @@ package com.songoda.ultimatetimber.treefall;
import com.songoda.ultimatetimber.UltimateTimber;
import com.songoda.ultimatetimber.api.events.TreeFallEvent;
import com.songoda.ultimatetimber.api.events.TreeFellEvent;
import com.songoda.ultimatetimber.configurations.DefaultConfig;
import org.bukkit.Bukkit;
@ -42,7 +43,12 @@ public class TreeFallListener implements Listener {
*/
if (blocks == null)
return;
//Call event that tree will fall
TreeFallEvent treeFallEvent = new TreeFallEvent(event.getPlayer(), treeChecker, event.getBlock());
Bukkit.getPluginManager().callEvent(treeFallEvent);
if(treeFallEvent.isCancelled()) return;
/*
Everything beyond this point assumes that the tree was valid
*/
@ -52,10 +58,6 @@ public class TreeFallListener implements Listener {
if (fileConfiguration.getBoolean(DefaultConfig.CUSTOM_AUDIO))
TreeSounds.tipOverNoise(event.getBlock().getLocation());
//Call event that tree will fall
TreeFallEvent tfe = new TreeFallEvent(event.getPlayer(), treeChecker, event.getBlock());
Bukkit.getPluginManager().callEvent(tfe);
if (fileConfiguration.getBoolean(DefaultConfig.SHOW_ANIMATION)) {
TreeFallAnimation treeFallAnimation = new TreeFallAnimation();
treeFallAnimation.startAnimation(event.getBlock(), blocks, event.getPlayer());
@ -64,6 +66,10 @@ public class TreeFallListener implements Listener {
event.getPlayer().getInventory().getItemInMainHand().containsEnchantment(Enchantment.SILK_TOUCH));
}
//Call event that a tree has fell
TreeFellEvent treeFellEvent = new TreeFellEvent(event.getPlayer(), treeChecker, event.getBlock());
Bukkit.getPluginManager().callEvent(treeFellEvent);
}
}