Skip to content

Commit

Permalink
Furnace smelts event - support for all blocks (#2320)
Browse files Browse the repository at this point in the history
* Support for all block types in furnace smelts event

* Fix order

* Meta fix
  • Loading branch information
tal5 committed Feb 9, 2022
1 parent 773c18a commit 9cecdb5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
Expand Up @@ -13,8 +13,6 @@
import com.denizenscript.denizen.events.vehicle.*;
import com.denizenscript.denizen.events.world.*;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.events.ScriptEventCouldMatcher;

Expand Down Expand Up @@ -50,6 +48,7 @@ public static void registerMainEvents() {
// Block events
ScriptEvent.registerScriptEvent(BlockBuiltScriptEvent.class);
ScriptEvent.registerScriptEvent(BlockBurnsScriptEvent.class);
ScriptEvent.registerScriptEvent(BlockCooksSmeltsItemScriptEvent.class);
ScriptEvent.registerScriptEvent(BlockDestroyedByExplosionEvent.class);
ScriptEvent.registerScriptEvent(BlockDispensesScriptEvent.class);
ScriptEvent.registerScriptEvent(BlockExplodesScriptEvent.class);
Expand All @@ -64,7 +63,6 @@ public static void registerMainEvents() {
ScriptEvent.registerScriptEvent(BrewsScriptEvent.class);
ScriptEvent.registerScriptEvent(CauldronLevelChangeScriptEvent.class);
ScriptEvent.registerScriptEvent(FurnaceBurnsItemScriptEvent.class);
ScriptEvent.registerScriptEvent(FurnaceSmeltsItemScriptEvent.class);
ScriptEvent.registerScriptEvent(LeafDecaysScriptEvent.class);
ScriptEvent.registerScriptEvent(LiquidLevelChangeScriptEvent.class);
ScriptEvent.registerScriptEvent(LiquidSpreadScriptEvent.class);
Expand Down
Expand Up @@ -4,65 +4,68 @@
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.ObjectTag;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.FurnaceSmeltEvent;
import org.bukkit.event.block.BlockCookEvent;

public class FurnaceSmeltsItemScriptEvent extends BukkitScriptEvent implements Listener {
public class BlockCooksSmeltsItemScriptEvent extends BukkitScriptEvent implements Listener {

// <--[event]
// @Events
// furnace smelts <item> (into <item>)
// <block> cooks|smelts <item> (into <item>)
//
// @Cancellable true
//
// @Group Block
//
// @Location true
//
// @Triggers when a furnace smelts an item.
// @Triggers when an item is smelted/cooked by a block.
//
// @Context
// <context.location> returns the LocationTag of the furnace.
// <context.source_item> returns the ItemTag that is being smelted.
// <context.result_item> returns the ItemTag that is the result of the smelting.
// <context.location> returns the LocationTag of the block smelting/cooking.
// <context.source_item> returns the ItemTag that is being smelted/cooked.
// <context.result_item> returns the ItemTag that is the result of the smelting/cooking.
//
// @Determine
// ItemTag to set the item that is the result of the smelting.
// ItemTag to set the item that is the result of the smelting/cooking.
//
// -->

public FurnaceSmeltsItemScriptEvent() {
public BlockCooksSmeltsItemScriptEvent() {
instance = this;
registerCouldMatcher("furnace smelts <item> (into <item>)");
registerCouldMatcher("<block> cooks|smelts <item> (into <item>)");
}

public static FurnaceSmeltsItemScriptEvent instance;
public static BlockCooksSmeltsItemScriptEvent instance;
public ItemTag source_item;
public ItemTag result_item;
public LocationTag location;
public FurnaceSmeltEvent event;
public Block block;
public BlockCookEvent event;

@Override
public boolean matches(ScriptPath path) {
if (!tryMaterial(block.getType(), path.eventArgLowerAt(0))) {
return false;
}
if (!tryItem(source_item, path.eventArgLowerAt(2))) {
return false;
}

if (path.eventArgLowerAt(3).equals("into")) {
if (!tryItem(result_item, path.eventArgLowerAt(4))) {
return false;
}
}
if (!runInCheck(path, location)) {
if (!runInCheck(path, block.getLocation())) {
return false;
}
return super.matches(path);
}

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

@Override
Expand All @@ -78,16 +81,16 @@ public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "location": return location;
case "location": return new LocationTag(block.getLocation());
case "source_item": return source_item;
case "result_item": return result_item;
}
return super.getContext(name);
}

@EventHandler
public void onFurnaceSmelts(FurnaceSmeltEvent event) {
location = new LocationTag(event.getBlock().getLocation());
public void onBlockCooks(BlockCookEvent event) {
block = event.getBlock();
source_item = new ItemTag(event.getSource());
result_item = new ItemTag(event.getResult());
this.event = event;
Expand Down

0 comments on commit 9cecdb5

Please sign in to comment.