Skip to content
This repository was archived by the owner on Mar 12, 2024. It is now read-only.

Commit df61087

Browse files
committed
Fixed some bugs
1 parent 703eeee commit df61087

5 files changed

Lines changed: 60 additions & 26 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.chafficui</groupId>
88
<artifactId>CrucialAPI</artifactId>
9-
<version>2.0.2</version>
9+
<version>2.1.0</version>
1010

1111
<name>CrucialAPI</name>
1212
<organization>

src/main/java/io/github/chafficui/CrucialAPI/Utils/customItems/CrucialItem.java

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package io.github.chafficui.CrucialAPI.Utils.customItems;
22

3+
import com.google.common.collect.Multimap;
34
import io.github.chafficui.CrucialAPI.exceptions.CrucialException;
45
import org.bukkit.Bukkit;
56
import org.bukkit.ChatColor;
67
import org.bukkit.Material;
78
import org.bukkit.NamespacedKey;
9+
import org.bukkit.attribute.Attribute;
10+
import org.bukkit.attribute.AttributeModifier;
811
import org.bukkit.inventory.ItemStack;
912

1013
import java.util.*;
@@ -18,19 +21,32 @@ public class CrucialItem {
1821
*/
1922
@Deprecated
2023
public static String getKey(ItemStack stack){
21-
if(stack != null && stack.getItemMeta() != null && stack.getItemMeta().getLore() != null){
22-
return ChatColor.stripColor(stack.getItemMeta().getLore().get(stack.getItemMeta().getLore().size()-1));
23-
}
24-
return null;
24+
return getId(stack).toString();
2525
}
2626

27-
public static UUID getId(ItemStack stack){
28-
if(stack != null && stack.getItemMeta() != null && stack.getItemMeta().getLore() != null){
29-
String key = ChatColor.stripColor(stack.getItemMeta().getLore().get(stack.getItemMeta().getLore().size()-1));
30-
if(key.length() < 36){
27+
public static UUID getId(ItemStack stack) {
28+
29+
if (stack != null && stack.getItemMeta() != null) {
30+
String key = "";
31+
Multimap<Attribute, AttributeModifier> attributeModifiers = stack.getItemMeta().getAttributeModifiers();
32+
if (attributeModifiers != null) {
33+
Collection<AttributeModifier> healthModifiers = attributeModifiers.get(Attribute.GENERIC_MAX_HEALTH);
34+
if (healthModifiers != null) {
35+
for (AttributeModifier modifier : healthModifiers) {
36+
if(modifier.getName().equals("CRUCIALITEM_ID")) {
37+
return modifier.getUniqueId();
38+
}
39+
}
40+
}
41+
}
42+
//TODO: remove in next major update
43+
if (stack.getItemMeta().getLore() != null) {
44+
key = ChatColor.stripColor(stack.getItemMeta().getLore().get(stack.getItemMeta().getLore().size() - 1));
45+
}
46+
if (key.length() < 36) {
3147
return null;
3248
}
33-
return UUID.fromString(key.substring(0,36));
49+
return UUID.fromString(key.substring(0, 36));
3450
}
3551
return null;
3652
}
@@ -57,13 +73,7 @@ public static CrucialItem getById(UUID id){
5773
*/
5874
@Deprecated
5975
public static CrucialItem getByKey(String key){
60-
for (CrucialItem crucialItem :
61-
CRUCIAL_ITEMS) {
62-
if(crucialItem.getId().equals(UUID.fromString(key.substring(0,36)))){
63-
return crucialItem;
64-
}
65-
}
66-
return null;
76+
return getById(UUID.fromString(key.substring(0,36)));
6777
}
6878

6979
//Custom Item
@@ -161,14 +171,15 @@ public void reload() throws CrucialException {
161171
}
162172

163173
private void registerRecipe() throws CrucialException {
174+
AttributeModifier modifier = new AttributeModifier(this.id, "CRUCIALITEM_ID", 0, AttributeModifier.Operation.ADD_NUMBER);
164175
if(isHead){
165176
if(headOwner != null) {
166-
namespacedKey = Item.createHead(id+type, name, getUniqueLore(), headOwner, recipe);
177+
namespacedKey = Item.createItem(id+type, name, Stack.addAttributeModifier(Stack.getStack(headOwner, name, lore), Attribute.GENERIC_MAX_HEALTH, modifier), recipe);
167178
} else {
168-
namespacedKey = Item.createHead(id+type, name, getUniqueLore(), material, recipe);
179+
namespacedKey = Item.createItem(id+type, name, Stack.addAttributeModifier(Stack.getStack(material, name, lore), Attribute.GENERIC_MAX_HEALTH, modifier), recipe);
169180
}
170181
} else {
171-
namespacedKey = Item.createItem(id+type, name, getUniqueLore(), Material.getMaterial(material), recipe);
182+
namespacedKey = Item.createItem(id+type, name, Stack.addAttributeModifier(Stack.getStack(Material.getMaterial(material), name, lore), Attribute.GENERIC_MAX_HEALTH, modifier), recipe);
172183
}
173184
}
174185

@@ -182,14 +193,15 @@ public void delete(){
182193

183194
public ItemStack getItemStack(){
184195
if(isRegistered){
196+
AttributeModifier modifier = new AttributeModifier(this.id, "CRUCIALITEM_ID", 0, AttributeModifier.Operation.ADD_NUMBER);
185197
if(isHead){
186198
if(headOwner != null) {
187-
return Stack.getStack(headOwner, name, getUniqueLore());
199+
return Stack.addAttributeModifier(Stack.getStack(headOwner, name, lore), Attribute.GENERIC_MAX_HEALTH, modifier);
188200
} else {
189-
return Stack.getStack(material, name, getUniqueLore());
201+
return Stack.addAttributeModifier(Stack.getStack(material, name, lore), Attribute.GENERIC_MAX_HEALTH, modifier);
190202
}
191203
} else {
192-
return Stack.getStack(Material.getMaterial(material), name, getUniqueLore());
204+
return Stack.addAttributeModifier(Stack.getStack(Material.getMaterial(material), name, lore), Attribute.GENERIC_MAX_HEALTH, modifier);
193205
}
194206
}
195207
return null;
@@ -203,6 +215,10 @@ public String getLore(){
203215
return String.valueOf(lore);
204216
}
205217

218+
/**
219+
* @deprecated
220+
*/
221+
@Deprecated
206222
public List<String> getUniqueLore(){
207223
List<String> uniqueLore = new ArrayList<>(lore);
208224
uniqueLore.add("");

src/main/java/io/github/chafficui/CrucialAPI/Utils/customItems/Item.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public static NamespacedKey createHead(String key, String name, List<String> lor
5050
}
5151
}
5252

53+
public static NamespacedKey createItem(String key, String name, ItemStack stack, String[] recipe) throws CrucialException {
54+
try{
55+
return addRecipe(key, name, recipe, stack);
56+
} catch(IllegalArgumentException e){
57+
throw new CrucialException(2);
58+
}
59+
}
60+
5361
private static NamespacedKey addRecipe(String key, String name, String[] recipe, ItemStack stack){
5462
name = name.replaceAll(" ", "_");
5563
key = key.replaceAll(" ", "_");

src/main/java/io/github/chafficui/CrucialAPI/Utils/customItems/Stack.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package io.github.chafficui.CrucialAPI.Utils.customItems;
22

3+
import com.google.common.collect.Multimap;
34
import org.bukkit.Bukkit;
45
import org.bukkit.Material;
6+
import org.bukkit.attribute.Attribute;
7+
import org.bukkit.attribute.AttributeModifier;
58
import org.bukkit.enchantments.Enchantment;
69
import org.bukkit.inventory.ItemFlag;
710
import org.bukkit.inventory.ItemStack;
811
import org.bukkit.inventory.meta.ItemMeta;
912
import org.bukkit.inventory.meta.SkullMeta;
1013

14+
import java.util.HashMap;
1115
import java.util.List;
1216
import java.util.UUID;
1317

@@ -82,6 +86,13 @@ public static ItemStack getStack(Material material, String name, List<String> lo
8286
return getCleanMeta(stack, meta);
8387
}
8488

89+
public static ItemStack addAttributeModifier(ItemStack stack, Attribute attribute, AttributeModifier modifier) {
90+
ItemMeta meta = stack.getItemMeta();
91+
meta.addAttributeModifier(attribute, modifier);
92+
stack.setItemMeta(meta);
93+
return stack;
94+
}
95+
8596
private static ItemStack getCleanMeta(ItemStack stack, ItemMeta meta) {
8697
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
8798
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);

src/main/java/io/github/chafficui/CrucialAPI/Utils/player/inventory/Page.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static boolean exists(Inventory inventory) {
3232

3333
private final int size;
3434
private final String title;
35-
protected final ArrayList<InventoryItem> inventoryItems = new ArrayList<>();
35+
protected ArrayList<InventoryItem> inventoryItems = new ArrayList<>();
3636
private Inventory inventory;
3737
public final HashMap<String, Object> extraData = new HashMap<>();
3838
private final Material fillMaterial;
@@ -65,7 +65,6 @@ public void open(Player player) {
6565

6666
protected void addItem(InventoryItem item) {
6767
inventoryItems.add(item);
68-
reloadInventory();
6968
}
7069

7170
public void reloadInventory() {
@@ -74,7 +73,7 @@ public void reloadInventory() {
7473
for (int i = 0; i < inventory.getSize(); i++) {
7574
inventory.setItem(i, new ItemStack(fillMaterial));
7675
}
77-
inventoryItems.clear();
76+
inventoryItems = new ArrayList<>();
7877
populate();
7978
for (InventoryItem item : inventoryItems) {
8079
inventory.setItem(item.getSlot(), item.getItem());

0 commit comments

Comments
 (0)