Skip to content

Commit 9148f15

Browse files
committed
Item utilities : Only specific given item flags can now be hidden.
[ITEM UTILITIES] * NEW: ItemUtils.hideItemAttributes() can now take as a parameter the item flags to hide, instead of all of them. * NEW: ItemStackBuilder.hideAttributes() can now take as a parameter the item flags to hide, instead of all of them.
1 parent f8fa3ba commit 9148f15

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public class ItemStackBuilder
6868

6969
private boolean glowing = false;
7070
private boolean hideAttributes = false;
71+
private String[] hiddenAttributes = null;
7172

7273
private final HashMap<Enchantment, Integer> enchantments = new HashMap<>();
7374

@@ -153,7 +154,16 @@ public ItemStack item()
153154
meta.setLore(loreLines);
154155

155156
if (hideAttributes)
156-
ItemUtils.hideItemAttributes(meta);
157+
{
158+
if(hiddenAttributes == null)
159+
{
160+
ItemUtils.hideItemAttributes(meta);
161+
}
162+
else
163+
{
164+
ItemUtils.hideItemAttributes(meta, hiddenAttributes);
165+
}
166+
}
157167

158168
newItemStack.setItemMeta(meta);
159169

@@ -484,6 +494,14 @@ public ItemStackBuilder hideAttributes()
484494
this.hideAttributes = true;
485495
return this;
486496
}
497+
498+
public ItemStackBuilder hideAttributes(String... flagsNames)
499+
{
500+
hideAttributes();
501+
this.hiddenAttributes = flagsNames;
502+
503+
return this;
504+
}
487505

488506
/**
489507
* Sets the data (= damage) value of the ItemStack.

src/main/java/fr/zcraft/zlib/tools/items/ItemUtils.java

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import fr.zcraft.zlib.tools.reflection.NMSException;
3434
import fr.zcraft.zlib.tools.reflection.Reflection;
3535
import fr.zcraft.zlib.tools.runners.RunTask;
36+
import java.lang.reflect.Array;
3637
import org.bukkit.GameMode;
3738
import org.bukkit.Location;
3839
import org.bukkit.Material;
@@ -44,6 +45,8 @@
4445
import java.lang.reflect.InvocationTargetException;
4546
import java.lang.reflect.Method;
4647
import java.lang.reflect.Type;
48+
import java.util.ArrayList;
49+
import java.util.List;
4750
import java.util.Random;
4851
import org.bukkit.inventory.meta.PotionMeta;
4952
import org.bukkit.potion.Potion;
@@ -96,6 +99,34 @@ static private void init()
9699
addItemFlagLoaded = true;
97100
}
98101

102+
static private Object getItemFlagValue(String flagName)
103+
{
104+
if(itemFlagValues == null) return null;
105+
106+
for(Object value : itemFlagValues)
107+
{
108+
if(value.toString().equalsIgnoreCase(flagName))
109+
return value;
110+
}
111+
112+
return null;
113+
}
114+
115+
static private Object[] getItemFlagsValues(String... flagsNames)
116+
{
117+
List flagsList = new ArrayList();
118+
for(String flagName : flagsNames)
119+
{
120+
Object flag = getItemFlagValue(flagName);
121+
if(flag != null)
122+
flagsList.add(flag);
123+
}
124+
125+
126+
Object array = Array.newInstance(itemFlagValues[0].getClass(), flagsList.size());
127+
return flagsList.toArray((Object[]) array);
128+
}
129+
99130
/**
100131
* Hides all the item attributes of the given {@link ItemMeta}.
101132
*
@@ -106,15 +137,33 @@ static private void init()
106137
static public ItemMeta hideItemAttributes(ItemMeta meta)
107138
{
108139
if (!addItemFlagLoaded) init();
109-
140+
return hideItemAttributes(meta, itemFlagValues);
141+
}
142+
143+
/**
144+
* Hides the specified item attributes of the given {@link ItemMeta}.
145+
*
146+
* @param meta The {@link ItemMeta} to hide attributes from.
147+
* @param itemFlagsNames The item flags to hide.
148+
* @return The same item meta. The modification is applied by reference, the
149+
* stack is returned for convenience reasons.
150+
*/
151+
static public ItemMeta hideItemAttributes(ItemMeta meta, String... itemFlagsNames)
152+
{
153+
if (!addItemFlagLoaded) init();
154+
return hideItemAttributes(meta, getItemFlagsValues(itemFlagsNames));
155+
}
156+
157+
static private ItemMeta hideItemAttributes(ItemMeta meta, Object[] itemFlags)
158+
{
110159
if (addItemFlagsMethod == null)
111160
{
112161
return meta;
113162
}
114163

115164
try
116165
{
117-
addItemFlagsMethod.invoke(meta, new Object[]{itemFlagValues});
166+
addItemFlagsMethod.invoke(meta, new Object[]{itemFlags});
118167
}
119168
catch (IllegalAccessException ex)
120169
{

0 commit comments

Comments
 (0)