Skip to content

Commit cb0e03d

Browse files
Add files via upload
1 parent b447188 commit cb0e03d

21 files changed

+2415
-0
lines changed

pom.xml

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

0 commit comments

Comments
 (0)