/
ScribeCommand.java
150 lines (124 loc) · 5.7 KB
/
ScribeCommand.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.denizenscript.denizen.scripts.commands.item;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.scripts.containers.core.BookScriptContainer;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ScriptTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.Deprecations;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
public class ScribeCommand extends AbstractCommand {
public ScribeCommand() {
setName("scribe");
setSyntax("(Deprecated)");
setRequiredArguments(0, -1);
isProcedural = false;
}
private enum BookAction {GIVE, DROP, EQUIP, NONE}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (arg.matchesEnum(BookAction.class)
&& !scriptEntry.hasObject("action")) {
scriptEntry.addObject("action", BookAction.valueOf(arg.getValue().toUpperCase()));
}
else if (!scriptEntry.hasObject("script")
&& arg.matchesArgumentType(ScriptTag.class)) {
scriptEntry.addObject("script", arg.asType(ScriptTag.class));
}
else if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(LocationTag.class)) {
scriptEntry.addObject("location", arg.asType(LocationTag.class));
scriptEntry.addObject("action", BookAction.DROP);
}
else if (!scriptEntry.hasObject("item")
&& arg.matchesArgumentType(ItemTag.class)) {
scriptEntry.addObject("item", arg.asType(ItemTag.class));
}
else {
arg.reportUnhandled();
}
}
scriptEntry.defaultObject("action", BookAction.GIVE);
scriptEntry.defaultObject("item", new ItemTag(Material.WRITTEN_BOOK));
// Must contain a book script
if (!scriptEntry.hasObject("script")) {
throw new InvalidArgumentsException("Missing SCRIPT argument!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
Deprecations.scribeCommand.warn(scriptEntry);
// Retrieve objects from ScriptEntry
BookAction action = (BookAction) scriptEntry.getObject("action");
ItemTag book = scriptEntry.getObjectTag("item");
ScriptTag script = scriptEntry.getObjectTag("script");
LocationTag location = scriptEntry.getObjectTag("location");
BookScriptContainer bookScript = (BookScriptContainer) script.getContainer();
book = bookScript.writeBookTo(book, scriptEntry.getContext());
// Post-write action? Can be NONE.
switch (action) {
case DROP:
dropBook(location, book.getItemStack());
break;
case GIVE:
giveBook(Utilities.getEntryPlayer(scriptEntry).getPlayerEntity(), book.getItemStack());
// Update player's inventory
Utilities.getEntryPlayer(scriptEntry).getPlayerEntity().updateInventory();
break;
case EQUIP:
equipBook(Utilities.getEntryPlayer(scriptEntry).getPlayerEntity(), book.getItemStack());
// Update player's inventory
Utilities.getEntryPlayer(scriptEntry).getPlayerEntity().updateInventory();
break;
case NONE:
break;
}
}
private void giveBook(Player player, ItemStack book) {
Inventory inv = player.getInventory();
int emptySpot = inv.firstEmpty();
if (emptySpot != -1) {
player.getInventory().addItem(book.clone());
}
else {
player.getWorld().dropItem(player.getLocation(), book);
Debug.log("Player's inventory is full, dropped book.");
}
}
private void equipBook(Player player, ItemStack book) {
ItemStack currItem = player.getEquipment().getItemInMainHand();
Inventory inv = player.getInventory();
int emptySpot = inv.firstEmpty();
// if player isn't holding anything
if (currItem == null || currItem.getType() == Material.AIR) {
player.setItemInHand(book);
return;
}
// drop it if inventory has no empty slots
Debug.log("emptySpot: " + emptySpot);
if (emptySpot == -1) {
player.getWorld().dropItem(player.getLocation(), book);
Debug.log("Player's inventory is full, dropped book.");
}
// move current held item to empty spot, set item in hand to the book
else {
NMSHandler.getItemHelper().setInventoryItem(inv, currItem, emptySpot);
inv.setItem(emptySpot, currItem);
player.setItemInHand(book);
Debug.log("...added book to player hand, moved original item");
}
}
private void dropBook(Location location, ItemStack book) {
location.getWorld().dropItem(location, book);
}
}