-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
PlayerPreparesEnchantScriptEvent.java
131 lines (120 loc) · 5.34 KB
/
PlayerPreparesEnchantScriptEvent.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
package com.denizenscript.denizen.events.player;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizencore.utilities.debugging.Debug;
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.objects.core.MapTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.enchantments.EnchantmentOffer;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.enchantment.PrepareItemEnchantEvent;
public class PlayerPreparesEnchantScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// player prepares item enchant
// player prepares <item> enchant
//
// @Regex ^on player prepares [^\s]+ enchant$
//
// @Group Player
//
// @Triggers when a player prepares to enchant an item.
//
// @Context
// <context.location> returns the LocationTag of the enchanting block.
// <context.item> returns the ItemTag to be enchanted.
// <context.bonus> returns an ElementTag(Number) of the enchanting bonus available (number of bookshelves).
// <context.offers> returns a ListTag of the available enchanting offers, each as a MapTag with keys 'cost', 'enchantment_type', and 'level'.
//
// @Determine
// "OFFERS:" + ListTag of MapTags to set the offers available. Cannot be a different size list than the size of context.offers.
//
// @Player Always.
//
// -->
public PlayerPreparesEnchantScriptEvent() {
}
public PrepareItemEnchantEvent event;
@Override
public boolean couldMatch(ScriptPath path) {
if (!path.eventLower.startsWith("player prepares") || !path.eventArgLowerAt(3).equals("enchant")) {
return false;
}
if (!couldMatchItem(path.eventArgLowerAt(2))) {
return false;
}
return true;
}
@Override
public boolean matches(ScriptPath path) {
if (!path.tryArgObject(2, new ItemTag(event.getItem()))) {
return false;
}
return super.matches(path);
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag) {
String determineLow = CoreUtilities.toLowerCase(determinationObj.toString());
if (determineLow.startsWith("offers:")) {
ListTag offers = ListTag.valueOf(determineLow.substring("offers:".length()), getTagContext(path));
if (offers.size() != event.getOffers().length) {
Debug.echoError("Offer list size incorrect.");
return false;
}
for (int i = 0; i < offers.size(); i++) {
MapTag map = MapTag.getMapFor(offers.getObject(i), getTagContext(path));
event.getOffers()[i].setCost(map.getElement("cost").asInt());
EnchantmentTag enchantment = map.getObjectAs("enchantment_type", EnchantmentTag.class, getTagContext(path));
if (enchantment == null) {
enchantment = map.getObjectAs("enchantment", EnchantmentTag.class, getTagContext(path));
}
event.getOffers()[i].setEnchantment(enchantment.enchantment);
event.getOffers()[i].setEnchantmentLevel(map.getElement("level").asInt());
}
return true;
}
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(event.getEnchanter());
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "item":
return new ItemTag(event.getItem());
case "location":
return new LocationTag(event.getEnchantBlock().getLocation());
case "bonus":
return new ElementTag(event.getEnchantmentBonus());
case "offers":
ListTag output = new ListTag();
for (EnchantmentOffer offer : event.getOffers()) {
MapTag map = new MapTag();
map.putObject("cost", new ElementTag(offer.getCost()));
map.putObject("enchantment", new ElementTag(offer.getEnchantment().getKey().getKey()));
map.putObject("enchantment_type", new EnchantmentTag(offer.getEnchantment()));
map.putObject("level", new ElementTag(offer.getEnchantmentLevel()));
output.addObject(map);
}
return output;
}
return super.getContext(name);
}
@EventHandler
public void onEnchantItem(PrepareItemEnchantEvent event) {
if (event.getInventory().getViewers().isEmpty()) {
return;
}
this.event = event;
fire(event);
}
}