Skip to content

Commit

Permalink
Rewrote "on block burns" event in new ScriptEvent format.
Browse files Browse the repository at this point in the history
Removed from BukkitWorldScriptHelper.
  • Loading branch information
Talamar1 committed Jun 13, 2015
1 parent a218dfa commit 290d631
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -577,6 +577,7 @@ public void onEnable() {

ScriptEvent.registerScriptEvent(new BiomeEnterExitScriptEvent());
ScriptEvent.registerScriptEvent(new BlockBuiltScriptEvent());
ScriptEvent.registerScriptEvent(new BlockBurnsScriptEvent());
ScriptEvent.registerScriptEvent(new BlockFallsScriptEvent());
ScriptEvent.registerScriptEvent(new BlockPhysicsScriptEvent());
ScriptEvent.registerScriptEvent(new BucketEmptyScriptEvent());
Expand Down
@@ -0,0 +1,96 @@
package net.aufdemrand.denizen.events.scriptevents;

import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dMaterial;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizencore.events.ScriptEvent;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.utilities.CoreUtilities;

import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBurnEvent;

import java.util.HashMap;

public class BlockBurnsScriptEvent extends ScriptEvent implements Listener {

// <--[event]
// @Events
// block burns
// <block> burns
//
// @Cancellable true
//
// @Triggers when a block is destroyed by fire.
//
// @Context
// <context.location> returns the dLocation the block was burned at.
// <context.material> returns the dMaterial of the block that was burned.
//
// -->

public BlockBurnsScriptEvent() {
instance = this;
}
public static BlockBurnsScriptEvent instance;
public dLocation location;
public dMaterial material;
public BlockBurnEvent event;

@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String mat = CoreUtilities.getXthArg(0, lower);
return lower.contains("block burns")
|| (lower.equals(mat + " burns") && dMaterial.matches(mat));
}

@Override
public boolean matches(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String mat = CoreUtilities.getXthArg(0, lower);
return mat.equals("block")
|| (material.identifySimpleNoIdentifier().toLowerCase().equals(mat));
}

@Override
public String getName() {
return "BlockBurns";
}

@Override
public void init() {
Bukkit.getServer().getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
}

@Override
public void destroy() {
BlockBurnEvent.getHandlerList().unregister(this);
}

@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
return super.applyDetermination(container, determination);
}

@Override
public HashMap<String, dObject> getContext() {
HashMap<String, dObject> context = super.getContext();
context.put("location", location);
context.put("material", material);
return context;
}

@EventHandler
public void onBlockBurns(BlockBurnEvent event) {
location = new dLocation(event.getBlock().getLocation());
material = dMaterial.getMaterialFrom(event.getBlock().getType(), event.getBlock().getData());
cancelled = event.isCancelled();
this.event = event;
fire();
event.setCancelled(cancelled);
}
}
Expand Up @@ -171,39 +171,6 @@ public void blockBreak(BlockBreakEvent event) {
}
}


// <--[event]
// @Events
// block burns
// <block> burns
//
// @Triggers when a block is destroyed by fire.
// @Context
// <context.location> returns the dLocation the block was burned at.
// <context.material> returns the dMaterial of the block that was burned.
//
// @Determine
// "CANCELLED" to stop the block from being destroyed.
//
// -->
@EventHandler
public void blockBurn(BlockBurnEvent event) {

Map<String, dObject> context = new HashMap<String, dObject>();

context.put("location", new dLocation(event.getBlock().getLocation()));
dMaterial material = dMaterial.getMaterialFrom(event.getBlock().getType(), event.getBlock().getData());
context.put("material", material);

String determination = doEvents(Arrays.asList
("block burns",
material.identifySimple() + " burns"),
null, null, context, true);

if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}

// <--[event]
// @Events
// player damages block
Expand Down

0 comments on commit 290d631

Please sign in to comment.