Skip to content

Commit f4c3dd5

Browse files
Added Files via Upload with the following Updates:
Added VersionHelper; to help support legacy versions >:[ Add Paginated Menus; straight out of triumph-gui (for now) Add MenuItem#editor(); edit items even after creation of menu items Fix issues caught by Jitpack
1 parent b67adbb commit f4c3dd5

22 files changed

+2502
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package me.flame.menus.builders.items;
2+
3+
import lombok.val;
4+
import me.flame.menus.items.MenuItem;
5+
import me.flame.menus.util.VersionHelper;
6+
import org.bukkit.Material;
7+
import org.bukkit.enchantments.Enchantment;
8+
import org.bukkit.entity.Damageable;
9+
import org.bukkit.event.inventory.InventoryClickEvent;
10+
import org.bukkit.inventory.ItemFlag;
11+
import org.bukkit.inventory.ItemStack;
12+
import org.bukkit.inventory.meta.ItemMeta;
13+
import org.jetbrains.annotations.NotNull;
14+
import org.jetbrains.annotations.Nullable;
15+
16+
import java.util.Arrays;
17+
import java.util.List;
18+
import java.util.function.Consumer;
19+
20+
@SuppressWarnings({ "unchecked", "unused" })
21+
public class BaseItemBuilder<B extends BaseItemBuilder<B>> {
22+
final ItemStack item;
23+
ItemMeta meta;
24+
private final boolean hasNoItemMeta;
25+
26+
BaseItemBuilder(Material material, int amount) {
27+
this(new ItemStack(material, amount));
28+
}
29+
30+
BaseItemBuilder(@NotNull ItemStack item) {
31+
this.item = item;
32+
this.meta = item.getItemMeta();
33+
this.hasNoItemMeta = this.meta == null;
34+
}
35+
36+
public B setGlow(boolean glow) {
37+
// add enchantment and hide it if "glow" is true
38+
if (this.hasNoItemMeta) return (B) this;
39+
if (!glow) {
40+
this.meta.removeEnchant(Enchantment.DURABILITY);
41+
this.meta.removeItemFlags(ItemFlag.HIDE_ENCHANTS);
42+
return (B) this;
43+
}
44+
this.meta.addEnchant(Enchantment.DURABILITY, 1, true);
45+
this.meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
46+
return (B) this;
47+
}
48+
49+
public B setAmount(int amount) {
50+
this.item.setAmount(amount);
51+
return (B) this;
52+
}
53+
54+
public B setName(String name) {
55+
if (this.hasNoItemMeta) return (B) this;
56+
this.meta.setDisplayName(name);
57+
return (B) this;
58+
}
59+
60+
public B setLore(String... lore) {
61+
if (this.hasNoItemMeta) return (B) this;
62+
this.meta.setLore(Arrays.asList(lore));
63+
return (B) this;
64+
}
65+
66+
public B setLore(List<String> lore) {
67+
if (this.hasNoItemMeta) return (B) this;
68+
this.meta.setLore(lore);
69+
return (B) this;
70+
}
71+
72+
public B addEnchant(Enchantment enchant) {
73+
if (this.hasNoItemMeta) return (B) this;
74+
this.meta.addEnchant(enchant, 1, false);
75+
return (B) this;
76+
}
77+
78+
public B addEnchant(Enchantment enchant, int level) {
79+
if (this.hasNoItemMeta) return (B) this;
80+
this.meta.addEnchant(enchant, level, false);
81+
return (B) this;
82+
}
83+
84+
public B addEnchant(Enchantment enchant, int level, boolean ignore) {
85+
if (this.hasNoItemMeta) return (B) this;
86+
this.meta.addEnchant(enchant, level, ignore);
87+
return (B) this;
88+
}
89+
90+
public B addEnchant(int level, boolean ignore, Enchantment... enchant) {
91+
if (this.hasNoItemMeta) return (B) this;
92+
for (Enchantment enchantment : enchant)
93+
this.meta.addEnchant(enchantment, level, ignore);
94+
return (B) this;
95+
}
96+
97+
public B addEnchant(int level, Enchantment... enchant) {
98+
if (this.hasNoItemMeta) return (B) this;
99+
for (Enchantment enchantment : enchant)
100+
this.meta.addEnchant(enchantment, level, false);
101+
return (B) this;
102+
}
103+
104+
public B addEnchant(Enchantment... enchant) {
105+
if (this.hasNoItemMeta) return (B) this;
106+
for (Enchantment enchantment : enchant)
107+
this.meta.addEnchant(enchantment, 1, false);
108+
return (B) this;
109+
}
110+
111+
public B setUnbreakable() {
112+
if (this.hasNoItemMeta) return (B) this;
113+
this.meta.setUnbreakable(true);
114+
return (B) this;
115+
}
116+
117+
public B setUnbreakable(boolean breakable) {
118+
if (this.hasNoItemMeta || VersionHelper.IS_UNBREAKABLE_LEGACY) return (B) this;
119+
this.meta.setUnbreakable(breakable);
120+
return (B) this;
121+
}
122+
123+
public B setDamage(int d) {
124+
if (this.hasNoItemMeta) return (B) this;
125+
if (meta instanceof Damageable) {
126+
val damageable = ((Damageable) meta);
127+
damageable.damage(d);
128+
}
129+
return (B) this;
130+
}
131+
132+
public ItemStack build() {
133+
this.item.setItemMeta(meta);
134+
return item;
135+
}
136+
137+
public MenuItem buildItem() {
138+
this.item.setItemMeta(meta);
139+
return new MenuItem(item, null);
140+
}
141+
142+
public MenuItem buildItem(@Nullable Consumer<InventoryClickEvent> clickAction) {
143+
this.item.setItemMeta(meta);
144+
return new MenuItem(item, clickAction);
145+
}
146+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package me.flame.menus.builders.items;
2+
3+
import org.bukkit.Material;
4+
import org.bukkit.inventory.ItemStack;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
@SuppressWarnings({ "unused" })
8+
public class ItemBuilder extends BaseItemBuilder<ItemBuilder> {
9+
ItemBuilder(Material material, int amount) {
10+
super(material, amount);
11+
}
12+
13+
ItemBuilder(@NotNull ItemStack item) {
14+
super(item);
15+
}
16+
17+
public static ItemBuilder of(ItemStack item) {
18+
return new ItemBuilder(item);
19+
}
20+
21+
public static ItemBuilder of(Material item) {
22+
return new ItemBuilder(item, 1);
23+
}
24+
25+
public static ItemBuilder of(Material item, int amount) {
26+
return new ItemBuilder(item, amount);
27+
}
28+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package me.flame.menus.builders.items;
2+
3+
import com.mojang.authlib.GameProfile;
4+
import com.mojang.authlib.properties.Property;
5+
import me.flame.menus.exceptions.MenuException;
6+
import me.flame.menus.util.SkullUtil;
7+
import me.flame.menus.util.VersionHelper;
8+
import org.bukkit.Bukkit;
9+
import org.bukkit.Material;
10+
import org.bukkit.OfflinePlayer;
11+
import org.bukkit.inventory.ItemStack;
12+
import org.bukkit.inventory.meta.SkullMeta;
13+
import org.jetbrains.annotations.Contract;
14+
import org.jetbrains.annotations.NotNull;
15+
16+
import java.lang.reflect.Field;
17+
import java.util.UUID;
18+
import java.util.logging.Level;
19+
20+
/**
21+
* Skull builder, self-explanatory, it builds player heads.
22+
*/
23+
@SuppressWarnings("unused")
24+
25+
public final class SkullBuilder extends BaseItemBuilder<SkullBuilder> {
26+
27+
private static final Field PROFILE_FIELD;
28+
29+
static {
30+
Field field;
31+
try {
32+
field = SkullUtil.skull()
33+
.getItemMeta()
34+
.getClass()
35+
.getDeclaredField("profile");
36+
field.setAccessible(true);
37+
} catch (NoSuchFieldException e) {
38+
e.printStackTrace();
39+
field = null;
40+
}
41+
42+
PROFILE_FIELD = field;
43+
}
44+
45+
SkullBuilder() {
46+
super(SkullUtil.skull());
47+
}
48+
49+
SkullBuilder(final @NotNull ItemStack itemStack) throws MenuException {
50+
super(itemStack);
51+
if (SkullUtil.isNotPlayerSkull(itemStack)) {
52+
throw new MenuException("SkullBuilder requires the material to be a PLAYER_HEAD!");
53+
}
54+
}
55+
56+
@Contract("_ -> new")
57+
public static @NotNull SkullBuilder of(ItemStack item) throws MenuException {
58+
return new SkullBuilder(item);
59+
}
60+
61+
@Contract("_ -> new")
62+
public static @NotNull SkullBuilder of(Material item) throws MenuException {
63+
return new SkullBuilder(new ItemStack(item, 1));
64+
}
65+
66+
@Contract("_, _ -> new")
67+
public static @NotNull SkullBuilder of(Material item, int amount) throws MenuException {
68+
return new SkullBuilder(new ItemStack(item, amount));
69+
}
70+
71+
/**
72+
* Sets the skull texture using a BASE64 string
73+
*
74+
* @param texture The base64 texture
75+
* @param profileId The unique id of the profile
76+
* @return {@link SkullBuilder}
77+
*/
78+
@NotNull
79+
@Contract("_, _ -> this")
80+
public SkullBuilder texture(@NotNull final String texture, @NotNull final UUID profileId) {
81+
if (SkullUtil.isNotPlayerSkull(item) || PROFILE_FIELD == null) return this;
82+
83+
final SkullMeta skullMeta = (SkullMeta) meta;
84+
final GameProfile profile = new GameProfile(profileId, null);
85+
profile.getProperties().put("textures", new Property("textures", texture));
86+
87+
try {
88+
PROFILE_FIELD.set(skullMeta, profile);
89+
} catch (IllegalArgumentException | IllegalAccessException ex) {
90+
Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex);
91+
}
92+
this.meta = skullMeta;
93+
return this;
94+
}
95+
96+
/**
97+
* Sets the skull texture using a BASE64 string
98+
*
99+
* @param texture The base64 texture
100+
* @return {@link SkullBuilder}
101+
*/
102+
@NotNull
103+
@Contract("_ -> this")
104+
public SkullBuilder texture(@NotNull final String texture) {
105+
return texture(texture, UUID.randomUUID());
106+
}
107+
108+
/**
109+
* Sets skull owner via bukkit methods
110+
*
111+
* @param player {@link OfflinePlayer} to set skull of
112+
* @return {@link SkullBuilder}
113+
*/
114+
@NotNull
115+
@Contract("_ -> this")
116+
@SuppressWarnings("deprecation")
117+
public SkullBuilder owner(@NotNull final OfflinePlayer player) {
118+
if (SkullUtil.isNotPlayerSkull(item)) return this;
119+
final SkullMeta skullMeta = (SkullMeta) meta;
120+
121+
if (VersionHelper.IS_SKULL_OWNER_LEGACY)
122+
skullMeta.setOwner(player.getName());
123+
else
124+
skullMeta.setOwningPlayer(player);
125+
126+
this.meta = skullMeta;
127+
return this;
128+
}
129+
130+
}

0 commit comments

Comments
 (0)