/
PlayerEditsBookScriptEvent.java
122 lines (111 loc) · 4.6 KB
/
PlayerEditsBookScriptEvent.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
package com.denizenscript.denizen.events.player;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.scripts.containers.core.BookScriptContainer;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizen.utilities.blocks.MaterialCompat;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ScriptTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerEditBookEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
public class PlayerEditsBookScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// player edits book
// player signs book
//
// @Regex ^on player (edits|signs) book$
//
// @Triggers when a player edits or signs a book.
// @Context
// <context.title> returns the name of the book, if any.
// <context.pages> returns the number of pages in the book.
// <context.book> returns the book item being edited, containing the new page contents.
// <context.signing> returns whether the book is about to be signed.
//
// @Determine
// "NOT_SIGNING" to prevent the book from being signed.
// ScriptTag to set the book information to set it to instead.
//
// @Player Always.
//
// -->
PlayerEditsBookScriptEvent instance;
PlayerEditBookEvent event;
PlayerTag player;
@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith("player edits book") || path.eventLower.startsWith("player signs book");
}
@Override
public boolean matches(ScriptPath path) {
String action = path.eventArgLowerAt(1);
if (!(action.equals("edits") && !event.isSigning()) && !(action.equals("signs") && event.isSigning())) {
return false;
}
return super.matches(path);
}
@Override
public String getName() {
return "PlayerEditsBook";
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
String determination = determinationObj.toString();
if (determination.toUpperCase().equals("NOT_SIGNING")) {
event.setSigning(false);
return true;
}
else if (ScriptTag.matches(determination)) {
ScriptTag script = ScriptTag.valueOf(determination);
if (script.getContainer() instanceof BookScriptContainer) {
ItemTag dBook = ((BookScriptContainer) script.getContainer()).getBookFrom((BukkitTagContext) getScriptEntryData().getTagContext());
BookMeta bookMeta = (BookMeta) dBook.getItemStack().getItemMeta();
if (dBook.getMaterial().getMaterial() == MaterialCompat.WRITABLE_BOOK) {
event.setSigning(false);
}
event.setNewBookMeta(bookMeta);
return true;
}
else {
Debug.echoError("Script '" + determination + "' is valid, but not of type 'book'!");
return true;
}
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(player, null);
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("signing")) {
return new ElementTag(event.isSigning());
}
if (name.equals("title")) {
return event.isSigning() ? new ElementTag(event.getNewBookMeta().getTitle()) : null;
}
else if (name.equals("book")) {
ItemStack book = new ItemStack(Material.WRITABLE_BOOK);
book.setItemMeta(event.getNewBookMeta());
return new ItemTag(book);
}
return super.getContext(name);
}
@EventHandler
public void onPlayerEditsBook(PlayerEditBookEvent event) {
player = PlayerTag.mirrorBukkitPlayer(event.getPlayer());
this.event = event;
fire(event);
}
}