Skip to content

Commit 7887078

Browse files
committed
add javadocs
1 parent ac37803 commit 7887078

File tree

17 files changed

+712
-9
lines changed

17 files changed

+712
-9
lines changed

core/src/main/java/io/github/projectunified/craftitem/core/Item.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,38 @@
22

33
import java.util.UUID;
44

5+
/**
6+
* Represents a generic item with name, amount, and ownership information.
7+
*
8+
* <p>This interface defines the core API for modifying item properties.
9+
*
10+
* <p><strong>Example Usage:</strong>
11+
* <pre>{@code
12+
* Item item = new SpigotItem(Material.DIAMOND_SWORD);
13+
* item.setName("Legendary Sword");
14+
* item.setAmount(1);
15+
* UUID owner = item.getOwner();
16+
* }</pre>
17+
*/
518
public interface Item {
19+
/**
20+
* Sets the display name of the item.
21+
*
22+
* @param name the new display name for the item
23+
*/
624
void setName(String name);
725

26+
/**
27+
* Sets the stack amount of the item.
28+
*
29+
* @param amount the stack size (typically 1-64)
30+
*/
831
void setAmount(int amount);
932

33+
/**
34+
* Gets the UUID of the item's owner.
35+
*
36+
* @return the owner's UUID, or null if no owner is set
37+
*/
1038
UUID getOwner();
1139
}

core/src/main/java/io/github/projectunified/craftitem/core/ItemModifier.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22

33
import java.util.function.UnaryOperator;
44

5+
/**
6+
* Interface for applying modifications to items.
7+
*
8+
* <p>Implementations modify item properties using a string translator for dynamic value substitution.
9+
* The translator allows for variable interpolation and custom value transformations.
10+
*
11+
* <p><strong>Example Implementation:</strong>
12+
* <pre>{@code
13+
* public class CustomModifier implements ItemModifier {
14+
* @Override
15+
* public void modify(Item item, UnaryOperator<String> translator) {
16+
* String translatedValue = translator.apply("${player_name}");
17+
* item.setName(translatedValue);
18+
* }
19+
* }
20+
* }</pre>
21+
*/
522
public interface ItemModifier {
23+
/**
24+
* Applies modifications to the given item.
25+
*
26+
* @param item the item to modify
27+
* @param translator a function to translate string values (e.g., for variable substitution)
28+
*/
629
void modify(Item item, UnaryOperator<String> translator);
730
}

modifier/src/main/java/io/github/projectunified/craftitem/modifier/AmountModifier.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,61 @@
55

66
import java.util.function.UnaryOperator;
77

8+
/**
9+
* Item modifier that sets the item's stack amount.
10+
*
11+
* <p>The amount can be provided as an integer, string, or defaults to 1.
12+
* The string value is translated before parsing to support dynamic amounts.
13+
*
14+
* <p><strong>Example Usage:</strong>
15+
* <pre>{@code
16+
* // Direct integer amount
17+
* ItemModifier modifier1 = new AmountModifier(64);
18+
*
19+
* // String amount with variables
20+
* ItemModifier modifier2 = new AmountModifier("${stack_size}");
21+
* modifier2.modify(item, s -> s.replace("${stack_size}", "32"));
22+
*
23+
* // Default amount of 1
24+
* ItemModifier modifier3 = new AmountModifier();
25+
* }</pre>
26+
*/
827
public class AmountModifier implements ItemModifier {
928
private final String amount;
1029

30+
/**
31+
* Creates a new AmountModifier with the specified integer amount.
32+
*
33+
* @param amount the stack size
34+
*/
1135
public AmountModifier(int amount) {
1236
this.amount = Integer.toString(amount);
1337
}
1438

39+
/**
40+
* Creates a new AmountModifier with the specified string amount.
41+
* The string can contain variables for translation.
42+
*
43+
* @param amount the stack size as a string
44+
*/
1545
public AmountModifier(String amount) {
1646
this.amount = amount;
1747
}
1848

49+
/**
50+
* Creates a new AmountModifier with a default amount of 1.
51+
*/
1952
public AmountModifier() {
2053
this.amount = "1";
2154
}
2255

56+
/**
57+
* Applies the translated amount to the item.
58+
* Invalid amounts are silently ignored.
59+
*
60+
* @param item the item to modify
61+
* @param translator the string translator for variable substitution
62+
*/
2363
@Override
2464
public void modify(Item item, UnaryOperator<String> translator) {
2565
String amount = translator.apply(this.amount);

modifier/src/main/java/io/github/projectunified/craftitem/modifier/NameModifier.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,37 @@
55

66
import java.util.function.UnaryOperator;
77

8+
/**
9+
* Item modifier that sets the item's display name.
10+
*
11+
* <p>The provided name is passed through the translator before being applied to the item,
12+
* allowing for variable substitution and dynamic name generation.
13+
*
14+
* <p><strong>Example Usage:</strong>
15+
* <pre>{@code
16+
* ItemModifier modifier = new NameModifier("Sword of ${owner}");
17+
* modifier.modify(item, s -> s.replace("${owner}", "Player123"));
18+
* // Item name becomes: "Sword of Player123"
19+
* }</pre>
20+
*/
821
public class NameModifier implements ItemModifier {
922
private final String name;
1023

24+
/**
25+
* Creates a new NameModifier with the specified name.
26+
*
27+
* @param name the display name (can contain variables for translation)
28+
*/
1129
public NameModifier(String name) {
1230
this.name = name;
1331
}
1432

33+
/**
34+
* Applies the translated name to the item.
35+
*
36+
* @param item the item to modify
37+
* @param translator the string translator for variable substitution
38+
*/
1539
@Override
1640
public void modify(Item item, UnaryOperator<String> translator) {
1741
String name = translator.apply(this.name);

spigot/core/src/main/java/io/github/projectunified/craftitem/spigot/core/SpigotItem.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,129 @@
88
import java.util.UUID;
99
import java.util.function.Consumer;
1010

11+
/**
12+
* Spigot-specific implementation of the Item interface.
13+
* Wraps Bukkit's ItemStack and provides convenient modification methods.
14+
*
15+
* <p>Provides multiple constructors for flexibility and specialized methods
16+
* for editing item metadata and the underlying ItemStack.
17+
*
18+
* <p><strong>Example Usage:</strong>
19+
* <pre>{@code
20+
* SpigotItem item = new SpigotItem(Material.DIAMOND_SWORD, playerUUID);
21+
* item.setName("Legendary Sword");
22+
* item.setAmount(1);
23+
* item.editMeta(meta -> meta.setCustomModelData(1));
24+
* ItemStack resultStack = item.getItemStack();
25+
* }</pre>
26+
*/
1127
public class SpigotItem implements Item {
1228
private final UUID owner;
1329
private ItemStack itemStack;
1430

31+
/**
32+
* Creates a new SpigotItem with the specified ItemStack and owner.
33+
*
34+
* @param itemStack the ItemStack to wrap (will be cloned)
35+
* @param owner the UUID of the item's owner, or null
36+
*/
1537
public SpigotItem(ItemStack itemStack, UUID owner) {
1638
this.owner = owner;
1739
this.itemStack = itemStack.clone();
1840
}
1941

42+
/**
43+
* Creates a new SpigotItem with the specified ItemStack and no owner.
44+
*
45+
* @param itemStack the ItemStack to wrap (will be cloned)
46+
*/
2047
public SpigotItem(ItemStack itemStack) {
2148
this(itemStack, null);
2249
}
2350

51+
/**
52+
* Creates a new SpigotItem with the specified Material type and owner.
53+
*
54+
* @param material the Material type for the new ItemStack
55+
* @param owner the UUID of the item's owner, or null
56+
*/
2457
public SpigotItem(Material material, UUID owner) {
2558
this.owner = owner;
2659
this.itemStack = new ItemStack(material);
2760
}
2861

62+
/**
63+
* Creates a new SpigotItem with the specified Material type and no owner.
64+
*
65+
* @param material the Material type for the new ItemStack
66+
*/
2967
public SpigotItem(Material material) {
3068
this(material, null);
3169
}
3270

71+
/**
72+
* Creates a new SpigotItem of STONE material with the specified owner.
73+
*
74+
* @param owner the UUID of the item's owner, or null
75+
*/
3376
public SpigotItem(UUID owner) {
3477
this.owner = owner;
3578
this.itemStack = new ItemStack(Material.STONE);
3679
}
3780

81+
/**
82+
* Creates a new SpigotItem of STONE material with no owner.
83+
*/
3884
public SpigotItem() {
3985
this((UUID) null);
4086
}
4187

88+
/**
89+
* Gets the underlying ItemStack.
90+
*
91+
* @return the ItemStack
92+
*/
4293
public ItemStack getItemStack() {
4394
return itemStack;
4495
}
4596

97+
/**
98+
* Sets the underlying ItemStack (cloned to prevent external modifications).
99+
*
100+
* @param itemStack the new ItemStack
101+
*/
46102
public void setItemStack(ItemStack itemStack) {
47103
this.itemStack = itemStack.clone();
48104
}
49105

106+
/**
107+
* Allows direct modification of the ItemStack.
108+
*
109+
* @param consumer the consumer to modify the ItemStack
110+
*/
50111
public void edit(Consumer<ItemStack> consumer) {
51112
consumer.accept(this.itemStack);
52113
}
53114

115+
/**
116+
* Allows modification of the item's metadata.
117+
*
118+
* @param consumer the consumer to modify the ItemMeta
119+
*/
54120
public void editMeta(Consumer<ItemMeta> consumer) {
55121
ItemMeta meta = this.itemStack.getItemMeta();
56122
if (meta == null) return;
57123
consumer.accept(meta);
58124
this.itemStack.setItemMeta(meta);
59125
}
60126

127+
/**
128+
* Allows type-safe modification of specific ItemMeta subclasses.
129+
*
130+
* @param <T> the ItemMeta subclass type
131+
* @param metaClass the class of the ItemMeta to modify
132+
* @param consumer the consumer to modify the metadata
133+
*/
61134
public <T extends ItemMeta> void editMeta(Class<T> metaClass, Consumer<T> consumer) {
62135
ItemMeta meta = this.itemStack.getItemMeta();
63136
if (meta == null) return;
@@ -66,16 +139,31 @@ public <T extends ItemMeta> void editMeta(Class<T> metaClass, Consumer<T> consum
66139
this.itemStack.setItemMeta(meta);
67140
}
68141

142+
/**
143+
* Sets the display name of the item.
144+
*
145+
* @param name the new display name
146+
*/
69147
@Override
70148
public void setName(String name) {
71149
editMeta(itemMeta -> itemMeta.setDisplayName(name));
72150
}
73151

152+
/**
153+
* Sets the stack amount of the item.
154+
*
155+
* @param amount the stack size
156+
*/
74157
@Override
75158
public void setAmount(int amount) {
76159
this.itemStack.setAmount(amount);
77160
}
78161

162+
/**
163+
* Gets the UUID of the item's owner.
164+
*
165+
* @return the owner's UUID, or null if no owner
166+
*/
79167
@Override
80168
public UUID getOwner() {
81169
return owner;

spigot/core/src/main/java/io/github/projectunified/craftitem/spigot/core/SpigotItemModifier.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,31 @@
55

66
import java.util.function.UnaryOperator;
77

8+
/**
9+
* Interface for Spigot-specific item modifications.
10+
*
11+
* <p>Extends ItemModifier to work specifically with SpigotItem instances.
12+
* Provides type-safe modification methods while maintaining compatibility with the generic ItemModifier interface.
13+
*
14+
* <p><strong>Example Implementation:</strong>
15+
* <pre>{@code
16+
* public class CustomSpigotModifier implements SpigotItemModifier {
17+
* @Override
18+
* public void modify(SpigotItem item, UnaryOperator<String> translator) {
19+
* item.editMeta(meta -> {
20+
* meta.setCustomModelData(42);
21+
* });
22+
* }
23+
* }
24+
* }</pre>
25+
*/
826
public interface SpigotItemModifier extends ItemModifier {
27+
/**
28+
* Applies Spigot-specific modifications to a SpigotItem.
29+
*
30+
* @param item the SpigotItem to modify
31+
* @param translator the string translator for variable substitution
32+
*/
933
void modify(SpigotItem item, UnaryOperator<String> translator);
1034

1135
@Override

0 commit comments

Comments
 (0)