Added custom model support and dropped support for versions bellow 1.14.4

This commit is contained in:
thomas 2021-08-06 14:23:43 +02:00
parent ab4bb73cc1
commit 6122d28bb9
3 changed files with 79 additions and 47 deletions

19
menuapi.iml Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.spigotmc:spigot-api:1.14.4-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-lang:commons-lang:2.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.guava:guava:21.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-chat:1.13-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.23" level="project" />
</component>
</module>

View File

@ -5,19 +5,19 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<properties> <properties>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>11</maven.compiler.target>
</properties> </properties>
<groupId>nl.iobyte</groupId> <groupId>nl.iobyte</groupId>
<artifactId>menuapi</artifactId> <artifactId>menuapi</artifactId>
<version>1.0</version> <version>1.1</version>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId> <artifactId>spigot-api</artifactId>
<version>1.12.2-R0.1-SNAPSHOT</version> <version>1.14.4-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -1,16 +1,10 @@
package nl.iobyte.menuapi.item; package nl.iobyte.menuapi.item;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class ItemBuilder { public class ItemBuilder {
@ -28,8 +22,9 @@ public class ItemBuilder {
item = new ItemStack(material, amount); item = new ItemStack(material, amount);
} }
public ItemBuilder(Material material, int amount, short data) { public ItemBuilder(Material material, int amount, int data) {
item = new ItemStack(material, amount, data); this(material, amount);
setCustomModelData(data);
} }
public ItemStack getItem() { public ItemStack getItem() {
@ -44,10 +39,19 @@ public class ItemBuilder {
return this; return this;
} }
public ItemBuilder setCustomModelData(int i) {
Optional.ofNullable(item.getItemMeta()).ifPresent(meta -> {
meta.setCustomModelData(i);
item.setItemMeta(meta);
});
return this;
}
public ItemBuilder setUnbreakable(boolean b) { public ItemBuilder setUnbreakable(boolean b) {
ItemMeta meta = item.getItemMeta(); Optional.ofNullable(item.getItemMeta()).ifPresent(meta -> {
meta.setUnbreakable(b); meta.setUnbreakable(b);
item.setItemMeta(meta); item.setItemMeta(meta);
});
return this; return this;
} }
@ -55,9 +59,10 @@ public class ItemBuilder {
if(name == null) if(name == null)
return this; return this;
ItemMeta meta = item.getItemMeta(); Optional.ofNullable(item.getItemMeta()).ifPresent(meta -> {
meta.setDisplayName(Color.parse(name)); meta.setDisplayName(Color.parse(name));
this.item.setItemMeta(meta); this.item.setItemMeta(meta);
});
return this; return this;
} }
@ -65,12 +70,13 @@ public class ItemBuilder {
if(lore == null) if(lore == null)
return this; return this;
ItemMeta meta = item.getItemMeta(); Optional.ofNullable(item.getItemMeta()).ifPresent(meta -> {
for (String string : lore) for (String string : lore)
lore.set(lore.indexOf(string), Color.parse(string)); lore.set(lore.indexOf(string), Color.parse(string));
meta.setLore(lore); meta.setLore(lore);
item.setItemMeta(meta); item.setItemMeta(meta);
});
return this; return this;
} }
@ -82,14 +88,16 @@ public class ItemBuilder {
} }
public ItemBuilder addLore(String lore) { public ItemBuilder addLore(String lore) {
List<String> list = item.getItemMeta().getLore(); Optional.ofNullable(item.getItemMeta()).ifPresent(meta -> {
List<String> list = meta.hasLore() ? meta.getLore() : new ArrayList<>();
list.add(lore); list.add(lore);
setLore(list); setLore(list);
});
return this; return this;
} }
public ItemBuilder setEnchantment(HashMap<Enchantment, Integer> enchantments) { public ItemBuilder setEnchantment(HashMap<Enchantment, Integer> enchantments) {
ItemMeta meta = item.getItemMeta(); Optional.ofNullable(item.getItemMeta()).ifPresent(meta -> {
if (!meta.getEnchants().isEmpty()) if (!meta.getEnchants().isEmpty())
meta.getEnchants().clear(); meta.getEnchants().clear();
@ -97,37 +105,42 @@ public class ItemBuilder {
meta.addEnchant(entry.getKey(), entry.getValue(), true); meta.addEnchant(entry.getKey(), entry.getValue(), true);
item.setItemMeta(meta); item.setItemMeta(meta);
});
return this; return this;
} }
public ItemBuilder setEnchantment(Enchantment enchantment, int i) { public ItemBuilder setEnchantment(Enchantment enchantment, int i) {
ItemMeta meta = item.getItemMeta(); Optional.ofNullable(item.getItemMeta()).ifPresent(meta -> {
if (!meta.getEnchants().isEmpty()) if (!meta.getEnchants().isEmpty())
meta.getEnchants().clear(); meta.getEnchants().clear();
meta.addEnchant(enchantment, i, true); meta.addEnchant(enchantment, i, true);
item.setItemMeta(meta); item.setItemMeta(meta);
});
return this; return this;
} }
public ItemBuilder addEnchantment(Enchantment enchantment, int i) { public ItemBuilder addEnchantment(Enchantment enchantment, int i) {
ItemMeta meta = item.getItemMeta(); Optional.ofNullable(item.getItemMeta()).ifPresent(meta -> {
meta.addEnchant(enchantment, i, true); meta.addEnchant(enchantment, i, true);
item.setItemMeta(meta); item.setItemMeta(meta);
});
return this; return this;
} }
public ItemBuilder addFlag(ItemFlag flag) { public ItemBuilder addFlag(ItemFlag flag) {
ItemMeta meta = item.getItemMeta(); Optional.ofNullable(item.getItemMeta()).ifPresent(meta -> {
meta.addItemFlags(flag); meta.addItemFlags(flag);
item.setItemMeta(meta); item.setItemMeta(meta);
});
return this; return this;
} }
public ItemBuilder addFlags(ItemFlag... flags) { public ItemBuilder addFlags(ItemFlag... flags) {
ItemMeta meta = item.getItemMeta(); Optional.ofNullable(item.getItemMeta()).ifPresent(meta -> {
meta.addItemFlags(flags); meta.addItemFlags(flags);
item.setItemMeta(meta); item.setItemMeta(meta);
});
return this; return this;
} }