/
BlockCooksSmeltsItemScriptEvent.java
93 lines (84 loc) · 3.06 KB
/
BlockCooksSmeltsItemScriptEvent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.denizenscript.denizen.events.block;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.ObjectTag;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockCookEvent;
public class BlockCooksSmeltsItemScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// <block> cooks|smelts <item> (into <item>)
//
// @Cancellable true
//
// @Group Block
//
// @Location true
//
// @Triggers when an item is smelted/cooked by a block.
//
// @Context
// <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/cooking.
//
// -->
public BlockCooksSmeltsItemScriptEvent() {
instance = this;
registerCouldMatcher("<block> cooks|smelts <item> (into <item>)");
}
public static BlockCooksSmeltsItemScriptEvent instance;
public ItemTag source_item;
public ItemTag result_item;
public LocationTag location;
public BlockCookEvent event;
@Override
public boolean matches(ScriptPath path) {
if (!location.tryAdvancedMatcher(path.eventArgLowerAt(0))) {
return false;
}
if (!source_item.tryAdvancedMatcher(path.eventArgLowerAt(2))) {
return false;
}
if (path.eventArgLowerAt(3).equals("into")) {
if (!result_item.tryAdvancedMatcher(path.eventArgLowerAt(4))) {
return false;
}
}
if (!runInCheck(path, location)) {
return false;
}
return super.matches(path);
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj.canBeType(ItemTag.class)) {
result_item = determinationObj.asType(ItemTag.class, getTagContext(path));
event.setResult(result_item.getItemStack());
return true;
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "location": return location;
case "source_item": return source_item;
case "result_item": return result_item;
}
return super.getContext(name);
}
@EventHandler
public void onBlockCooks(BlockCookEvent event) {
location = new LocationTag(event.getBlock().getLocation());
source_item = new ItemTag(event.getSource());
result_item = new ItemTag(event.getResult());
this.event = event;
fire(event);
}
}