/
Conversion.java
116 lines (94 loc) · 3.83 KB
/
Conversion.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
package com.denizenscript.denizen.utilities;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import org.bukkit.Color;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
public class Conversion {
/**
* Turn a list of ColorTags into a list of Colors.
*
* @param colors The list of ColorTags
* @return The list of Colors
*/
public static List<Color> convertColors(List<ColorTag> colors) {
List<Color> newList = new ArrayList<>();
for (ColorTag color : colors) {
newList.add(color.getColor());
}
return newList;
}
/**
* Turn a list of ItemTags into a list of ItemStacks.
*
* @param items The list of ItemTags
* @return The list of ItemStacks
*/
public static List<ItemStack> convertItems(List<ItemTag> items) {
List<ItemStack> newList = new ArrayList<>();
for (ItemTag item : items) {
newList.add(item.getItemStack());
}
return newList;
}
/**
* Turn a list of dEntities into a list of Entities.
*
* @param entities The list of dEntities
* @return The list of Entities
*/
public static List<Entity> convertEntities(List<EntityTag> entities) {
List<Entity> newList = new ArrayList<>();
for (EntityTag entity : entities) {
newList.add(entity.getBukkitEntity());
}
return newList;
}
/**
* Gets a InventoryTag from an Object, which can be a
* EntityTag, LocationTag, InventoryTag, or a ListTag of ItemTags
*
* @param arg An argument to parse
* @return The InventoryTag retrieved by parsing the argument
*/
public static AbstractMap.SimpleEntry<Integer, InventoryTag> getInventory(Argument arg, ScriptEntry scriptEntry) {
String string = arg.getValue();
if (InventoryTag.matches(string)) {
BukkitScriptEntryData data = (BukkitScriptEntryData) scriptEntry.entryData;
if (data != null) {
InventoryTag inv = InventoryTag.valueOf(string, data.getTagContext());
if (inv != null) {
return new AbstractMap.SimpleEntry<>(inv.getContents().length, inv);
}
}
else {
InventoryTag inv = InventoryTag.valueOf(string, null);
if (inv != null) {
return new AbstractMap.SimpleEntry<>(inv.getContents().length, inv);
}
}
}
else if (arg.matchesArgumentList(ItemTag.class)) {
List<ItemTag> list = ListTag.valueOf(string).filter(ItemTag.class, scriptEntry);
ItemStack[] items = convertItems(list).toArray(new ItemStack[list.size()]);
InventoryTag inventory = new InventoryTag(Math.max(InventoryTag.maxSlots, (items.length / 9) * 9 + 9));
inventory.setContents(items);
return new AbstractMap.SimpleEntry<>(items.length, inventory);
}
else if (LocationTag.matches(string)) {
InventoryTag inv = LocationTag.valueOf(string).getInventory();
return new AbstractMap.SimpleEntry<>(inv.getContents().length, inv);
}
else if (EntityTag.matches(string)) {
InventoryTag inv = EntityTag.valueOf(string).getInventory();
return new AbstractMap.SimpleEntry<>(inv.getContents().length, inv);
}
return null;
}
}