/
LootGenerateScriptEvent.java
132 lines (119 loc) · 4.84 KB
/
LootGenerateScriptEvent.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.denizenscript.denizen.events.world;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.InventoryTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.LootGenerateEvent;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
public class LootGenerateScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// loot generates
//
// @Regex ^on loot generates$
//
// @Group World
//
// @Location true
// @Switch for:<type> to only process the event if a certain inventory type is receiving loot (like 'for:chest').
//
// @Cancellable true
//
// @Triggers when loot is generated somewhere in the world (like a vanilla chest being opened for the first time).
//
// @Context
// <context.entity> returns an entity that caused loot generation, if any.
// <context.inventory> returns the InventoryTag that loot is generating into.
// <context.items> returns a ListTag of the items being generated.
// <context.loot_table_id> returns an element indicating the minecraft key for the loot-table that was generated.
//
// @Determine
// "LOOT:" + ListTag(ItemTag) to change the list of items that will generate as loot.
//
// @Player when the linked entity is a player.
//
// -->
public LootGenerateScriptEvent() {
instance = this;
}
public static LootGenerateScriptEvent instance;
public LootGenerateEvent event;
@Override
public boolean couldMatch(ScriptPath path) {
if (!path.eventLower.startsWith("loot generates")) {
return false;
}
return true;
}
@Override
public boolean matches(ScriptPath path) {
if (!runInCheck(path, event.getLootContext().getLocation())) {
return false;
}
if (path.switches.containsKey("for")) {
if (event.getInventoryHolder() == null || !runGenericSwitchCheck(path, "for", event.getInventoryHolder().getInventory().getType().name())) {
return false;
}
}
return super.matches(path);
}
@Override
public String getName() {
return "LootGenerates";
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(event.getEntity() != null ? new EntityTag(event.getEntity()) : null);
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag) {
String determination = determinationObj.toString();
String determinationLower = CoreUtilities.toLowerCase(determination);
if (determinationLower.startsWith("loot:")) {
ListTag list = ListTag.valueOf(determination.substring("loot:".length()), getTagContext(path));
ArrayList<ItemStack> newLoot = new ArrayList<>(list.size());
for (ItemTag item : list.filter(ItemTag.class, getTagContext(path))) {
newLoot.add(item.getItemStack());
}
event.setLoot(newLoot);
return true;
}
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("inventory") && event.getInventoryHolder() != null) {
return InventoryTag.mirrorBukkitInventory(event.getInventoryHolder().getInventory());
}
else if (name.equals("entity") && event.getEntity() != null) {
return new EntityTag(event.getEntity()).getDenizenObject();
}
else if (name.equals("items")) {
ListTag result = new ListTag();
for (ItemStack item : event.getLoot()) {
result.addObject(new ItemTag(item));
}
return result;
}
else if (name.equals("loot_table_id")) {
return new ElementTag(event.getLootTable().getKey().toString());
}
return super.getContext(name);
}
@EventHandler
public void onLightningStrikes(LootGenerateEvent event) {
this.event = event;
fire(event);
}
}