Skip to content

Commit 1fdac9b

Browse files
committed
Add cooldown, itemmodel, and tooltipstyle to item meta
1 parent a7d319a commit 1fdac9b

File tree

6 files changed

+298
-10
lines changed

6 files changed

+298
-10
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.laytonsmith.abstraction;
2+
3+
public interface MCCooldownComponent extends AbstractionObject {
4+
float getSeconds();
5+
void setSeconds(float seconds);
6+
String getCooldownGroup();
7+
void setCooldownGroup(String cooldownGroup);
8+
}

src/main/java/com/laytonsmith/abstraction/MCItemMeta.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,22 @@ public interface MCItemMeta extends AbstractionObject {
202202

203203
void setFood(MCFoodComponent component);
204204

205+
boolean hasItemModel();
206+
207+
String getItemModel();
208+
209+
void setItemModel(String key);
210+
211+
boolean hasTooltipStyle();
212+
213+
String getTooltipStyle();
214+
215+
void setTooltipStyle(String key);
216+
217+
boolean hasUseCooldown();
218+
219+
MCCooldownComponent getUseCooldown();
220+
221+
void setUseCooldown(MCCooldownComponent component);
222+
205223
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.laytonsmith.abstraction.bukkit;
2+
3+
import com.laytonsmith.abstraction.MCCooldownComponent;
4+
import com.laytonsmith.commandhelper.CommandHelperPlugin;
5+
import org.bukkit.NamespacedKey;
6+
import org.bukkit.inventory.meta.components.UseCooldownComponent;
7+
8+
public class BukkitMCCooldownComponent implements MCCooldownComponent {
9+
10+
private final UseCooldownComponent cooldownComponent;
11+
12+
public BukkitMCCooldownComponent(UseCooldownComponent foodComponent) {
13+
this.cooldownComponent = foodComponent;
14+
}
15+
16+
@Override
17+
public float getSeconds() {
18+
return cooldownComponent.getCooldownSeconds();
19+
}
20+
21+
@Override
22+
public void setSeconds(float seconds) {
23+
cooldownComponent.setCooldownSeconds(seconds);
24+
}
25+
26+
@Override
27+
public String getCooldownGroup() {
28+
NamespacedKey group = cooldownComponent.getCooldownGroup();
29+
if(group == null) {
30+
return null;
31+
}
32+
return group.toString();
33+
}
34+
35+
@Override
36+
public void setCooldownGroup(String cooldownGroup) {
37+
if(cooldownGroup == null) {
38+
cooldownComponent.setCooldownGroup(null);
39+
} else {
40+
cooldownComponent.setCooldownGroup(NamespacedKey.fromString(cooldownGroup, CommandHelperPlugin.self));
41+
}
42+
}
43+
44+
@Override
45+
public Object getHandle() {
46+
return cooldownComponent;
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return cooldownComponent.toString();
52+
}
53+
54+
@Override
55+
public boolean equals(Object o) {
56+
return o instanceof BukkitMCCooldownComponent && cooldownComponent.equals(((BukkitMCCooldownComponent) o).cooldownComponent);
57+
}
58+
59+
@Override
60+
public int hashCode() {
61+
return cooldownComponent.hashCode();
62+
}
63+
}

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCItemMeta.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.laytonsmith.PureUtilities.Common.ReflectionUtils.ReflectionException;
77
import com.laytonsmith.abstraction.AbstractionObject;
88
import com.laytonsmith.abstraction.MCAttributeModifier;
9+
import com.laytonsmith.abstraction.MCCooldownComponent;
910
import com.laytonsmith.abstraction.MCFoodComponent;
1011
import com.laytonsmith.abstraction.MCItemMeta;
1112
import com.laytonsmith.abstraction.MCItemStack;
@@ -50,6 +51,7 @@
5051
import org.bukkit.inventory.meta.Repairable;
5152
import org.bukkit.inventory.meta.components.FoodComponent;
5253
import org.bukkit.inventory.meta.components.JukeboxPlayableComponent;
54+
import org.bukkit.inventory.meta.components.UseCooldownComponent;
5355

5456
public class BukkitMCItemMeta implements MCItemMeta {
5557

@@ -442,4 +444,50 @@ public MCFoodComponent getFood() {
442444
public void setFood(MCFoodComponent component) {
443445
im.setFood((FoodComponent) component.getHandle());
444446
}
447+
448+
@Override
449+
public boolean hasItemModel() {
450+
return im.hasItemModel();
451+
}
452+
453+
@Override
454+
public String getItemModel() {
455+
return im.getItemModel().toString();
456+
}
457+
458+
@Override
459+
public void setItemModel(String key) {
460+
im.setItemModel(NamespacedKey.fromString(key));
461+
}
462+
463+
@Override
464+
public boolean hasTooltipStyle() {
465+
return im.hasTooltipStyle();
466+
}
467+
468+
@Override
469+
public String getTooltipStyle() {
470+
return im.getTooltipStyle().toString();
471+
}
472+
473+
@Override
474+
public void setTooltipStyle(String key) {
475+
im.setTooltipStyle(NamespacedKey.fromString(key));
476+
}
477+
478+
@Override
479+
public boolean hasUseCooldown() {
480+
return im.hasUseCooldown();
481+
}
482+
483+
@Override
484+
public MCCooldownComponent getUseCooldown() {
485+
return new BukkitMCCooldownComponent(im.getUseCooldown());
486+
}
487+
488+
@Override
489+
public void setUseCooldown(MCCooldownComponent component) {
490+
im.setUseCooldown((UseCooldownComponent) component.getHandle());
491+
}
492+
445493
}

src/main/java/com/laytonsmith/core/ObjectGenerator.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.laytonsmith.abstraction.MCColor;
1313
import com.laytonsmith.abstraction.MCColorableArmorMeta;
1414
import com.laytonsmith.abstraction.MCCompassMeta;
15+
import com.laytonsmith.abstraction.MCCooldownComponent;
1516
import com.laytonsmith.abstraction.MCCreatureSpawner;
1617
import com.laytonsmith.abstraction.MCCrossbowMeta;
1718
import com.laytonsmith.abstraction.MCEnchantmentStorageMeta;
@@ -549,6 +550,22 @@ public Construct itemMeta(MCItemStack is, Target t) {
549550
if(meta.hasUseRemainder()) {
550551
ma.set("remainder", item(meta.getUseRemainder(), t), t);
551552
}
553+
if(meta.hasUseCooldown()) {
554+
MCCooldownComponent cooldownComponent = meta.getUseCooldown();
555+
CArray cooldown = CArray.GetAssociativeArray(t);
556+
cooldown.set("seconds", new CDouble(cooldownComponent.getSeconds(), t), t);
557+
String group = cooldownComponent.getCooldownGroup();
558+
if(group != null) {
559+
cooldown.set("group", new CString(group, t), t);
560+
}
561+
ma.set("cooldown", cooldown, t);
562+
}
563+
if(meta.hasItemModel()) {
564+
ma.set("itemmodel", new CString(meta.getItemModel(), t), t);
565+
}
566+
if(meta.hasTooltipStyle()) {
567+
ma.set("tooltipstyle", new CString(meta.getTooltipStyle(), t), t);
568+
}
552569
}
553570

554571
} else { // versions before 1.20.6
@@ -1133,6 +1150,46 @@ public MCItemMeta itemMeta(Mixed c, MCMaterial mat, Target t) throws ConfigRunti
11331150
meta.setUseRemainder(item(remainder, t));
11341151
}
11351152
}
1153+
if(ma.containsKey("cooldown")) {
1154+
Mixed mixedCooldown = ma.get("cooldown", t);
1155+
if(mixedCooldown instanceof CNull) {
1156+
// not yet supported
1157+
} else if(mixedCooldown.isInstanceOf(CArray.TYPE)) {
1158+
CArray cooldownArray = (CArray) mixedCooldown;
1159+
if(!cooldownArray.isAssociative()) {
1160+
throw new CREFormatException("Cooldown array must be associative.", t);
1161+
}
1162+
MCCooldownComponent cooldown = meta.getUseCooldown();
1163+
if(cooldownArray.containsKey("seconds")) {
1164+
cooldown.setSeconds(ArgumentValidation.getDouble32(cooldownArray.get("seconds", t), t));
1165+
}
1166+
if(cooldownArray.containsKey("group")) {
1167+
Mixed group = cooldownArray.get("group", t);
1168+
if(!(group instanceof CNull)) {
1169+
cooldown.setCooldownGroup(group.val());
1170+
}
1171+
}
1172+
meta.setUseCooldown(cooldown);
1173+
} else {
1174+
throw new CREFormatException("Expected an array for item cooldown.", t);
1175+
}
1176+
}
1177+
if(ma.containsKey("itemmodel")) {
1178+
Mixed itemmodel = ma.get("itemmodel", t);
1179+
if(itemmodel instanceof CNull) {
1180+
// not yet supported
1181+
} else {
1182+
meta.setItemModel(itemmodel.val());
1183+
}
1184+
}
1185+
if(ma.containsKey("tooltipstyle")) {
1186+
Mixed tooltipstyle = ma.get("tooltipstyle", t);
1187+
if(tooltipstyle instanceof CNull) {
1188+
// not yet supported
1189+
} else {
1190+
meta.setTooltipStyle(tooltipstyle.val());
1191+
}
1192+
}
11361193
}
11371194
if(ma.containsKey("damage")) {
11381195
Mixed damage = ma.get("damage", t);

src/main/resources/functionDocs/get_itemmeta

Lines changed: 104 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ array {[player,] slot} Returns an associative array of known item data in the sl
22

33
----
44

5+
==== Basic Fields ====
56
These fields exist for all items with meta.
6-
{| width="100%" cellspacing="1" cellpadding="1" border="1" class="wikitable"
7+
{| class="wikitable"
78
|-
89
! scope="col" width="12%" | Field
910
! scope="col" width="15%" | Type
@@ -53,8 +54,9 @@ These fields exist for all items with meta.
5354
** '''value''' : (mixed) A valid value for the given tag type.
5455
|}
5556

57+
==== Optional Fields ====
5658
These fields optionally exist for any item type.
57-
{| width="100%" cellspacing="1" cellpadding="1" border="1" class="wikitable"
59+
{| class="wikitable"
5860
|-
5961
! scope="col" width="12%" | Field
6062
! scope="col" width="15%" | Type
@@ -106,10 +108,25 @@ These fields optionally exist for any item type.
106108
! remainder
107109
| associative array
108110
| Overrides the remaining item when the quantity of this item decreases after use. (e.g. a bucket after drinking a milk bucket) (MC 1.21.3+)
111+
|-
112+
! cooldown
113+
| associative array
114+
| Overrides the item's cooldown on use. The cooldown temporarily prevents any right-click actions for items of the same type or with the same cooldown group, if set. Not all item types activate cooldowns on use. (MC 1.21.3+)
115+
* '''seconds''' : (double) The number of seconds the cooldown lasts when the item is used. (default: 1.0)
116+
* '''group''' : (string) If this namespaced id is present, it shares cooldowns with items with the same cooldown group instead of by item type. (optional)
117+
|-
118+
! itemmodel
119+
| string
120+
| Overrides the item's model using a namespaced resource location. (MC 1.21.3+)
121+
|-
122+
! tooltipstyle
123+
| string
124+
| Overrides the item's tooltip style using a namespaced resource location. (MC 1.21.3+)
109125
|}
110126

127+
==== Type Specific Fields ====
111128
These fields only exist for specific item types.
112-
{| width="80%" cellspacing="1" cellpadding="1" border="1" class="wikitable"
129+
{| class="wikitable"
113130
|-
114131
! scope="col" width="14%" | Item Type
115132
! scope="col" width="86%" | Meta
@@ -324,10 +341,87 @@ These fields may be null on 1.21.5 due to a server API implementation bug.
324341
The entity's custom name is derived from the item "display" string. All other tropical fish data is not yet supported.
325342
|}
326343

327-
Other data not yet supported in item meta:
328-
329-
* MC 1.13.2 : "can_break" and "can_place_on" (Paper only)
330-
* MC 1.20.5 : "item_name", "intangible_projectile" (Paper 1.21.3 only), "instrument" (can only get custom definitions in Paper, not set), "tool" (cannot get tags in 'rules', can_destroy_blocks_in_creative field only in Paper), "hide_tooltip" (later integrated into "tooltip_display"), "lock" (can only set with server API)
331-
* MC 1.21.2 : "damage_resistant", "tooltip_style", "use_cooldown", "item_model", "death_protection" (Paper only), "repairable" (Paper only), "consumable" (added in Spigot 1.21.4 API, Paper API separate), "equippable" (added in Spigot 1.21.5 API)
332-
* MC 1.21.4 : "custom_model_data" (for anything other than a single float rounded down)
333-
* MC 1.21.5 : "weapon" (separate Paper/Spigot API), "blocks_attacks" (separate Paper/Spigot API), "break_sound" (separate Paper/Spigot API), "provides_banner_patterns" (Paper only), "provides_trim_material" (Paper only), "potion_duration_scale" (Paper only), "tooltip_display" (partially supported with "flags")
344+
==== Unsupported Fields ====
345+
Data components not yet supported in item meta:
346+
{| class="wikitable"
347+
|-
348+
! scope="col" width="20%" | Field
349+
! scope="col" width="10%" | MCVersion
350+
! scope="col" width="70%" | Notes
351+
|-
352+
! can_break
353+
| 1.13.2
354+
| Paper only
355+
|-
356+
! can_place_on
357+
| 1.13.2
358+
| Paper only
359+
|-
360+
! item_name
361+
| 1.20.5
362+
|
363+
|-
364+
! intangible_projectile
365+
| 1.20.5
366+
| Paper 1.21.3+ only
367+
|-
368+
! tool
369+
| 1.20.5
370+
| Cannot read tags in 'rules' field; 'can_destroy_blocks_in_creative' field only in Paper
371+
|-
372+
! lock
373+
| 1.20.5
374+
| Server API is write only
375+
|-
376+
! damage_resistant
377+
| 1.21.2
378+
|
379+
|-
380+
! death_protection
381+
| 1.21.2
382+
| Paper only
383+
|-
384+
! repairable
385+
| 1.21.2
386+
| Paper only
387+
|-
388+
! consumable
389+
| 1.21.2
390+
| Added in Spigot 1.21.4 API; Paper API separate
391+
|-
392+
! equippable
393+
| 1.21.2
394+
| Added in Spigot 1.21.5 API
395+
|-
396+
! custom_model_data
397+
| 1.21.4
398+
| Needed for anything other than a single float rounded down in the 'model' field
399+
|-
400+
! weapon
401+
| 1.21.5
402+
| Separate Paper/Spigot API
403+
|-
404+
! blocks_attacks
405+
| 1.21.5
406+
| Separate Paper/Spigot API
407+
|-
408+
! break_sound
409+
| 1.21.5
410+
| Separate Paper/Spigot API
411+
|-
412+
! provides_banner_patterns
413+
| 1.21.5
414+
| Paper only
415+
|-
416+
! provides_trim_material
417+
| 1.21.5
418+
| Paper only
419+
|-
420+
! potion_duration_scale
421+
| 1.21.5
422+
| Paper only
423+
|-
424+
! tooltip_display
425+
| 1.21.5
426+
| Partially supported with 'flags' field
427+
|}

0 commit comments

Comments
 (0)