Skip to content

Commit

Permalink
Update XSeries
Browse files Browse the repository at this point in the history
add ItemBuilder (from AstralFlow)
  • Loading branch information
iceBear67 committed Oct 13, 2024
1 parent bc17b4d commit 447d66e
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 48 deletions.
8 changes: 5 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format

[versions]
com-comphenix-protocol-protocollib = "5.0.0"
com-github-cryptomorin-xseries = "9.4.0"
com-comphenix-protocol-protocollib = "5.1.0"
com-github-cryptomorin-xseries = "11.3.0"
com-zaxxer-hikaricp = "4.0.3"
me-clip-placeholderapi = "2.11.6"
org-jetbrains-annotations = "23.0.0"
org-spigotmc-spigot-api = "1.21-R0.1-SNAPSHOT"
org-xerial-sqlite-jdbc = "3.41.2.2"
org-mariadb-jdbc-mariadb-java-client = "3.1.2"
canvas = "1.7.0-SNAPSHOT"


[libraries]
Expand All @@ -20,4 +21,5 @@ me-clip-placeholderapi = { module = "me.clip:placeholderapi", version.ref = "me-
org-jetbrains-annotations = { module = "org.jetbrains:annotations", version.ref = "org-jetbrains-annotations" }
org-spigotmc-spigot-api = { module = "org.spigotmc:spigot-api", version.ref = "org-spigotmc-spigot-api" }
org-xerial-sqlite-jdbc = { module = "org.xerial:sqlite-jdbc", version.ref = "org-xerial-sqlite-jdbc" }
org-mariadb-jdbc-mariadb-java-client = { module = "org.mariadb.jdbc:mariadb-java-client", version.ref = "org-mariadb-jdbc-mariadb-java-client" }
org-mariadb-jdbc-mariadb-java-client = { module = "org.mariadb.jdbc:mariadb-java-client", version.ref = "org-mariadb-jdbc-mariadb-java-client" }
org-ipvp-canvas = { module = "org.ipvp:canvas", version.ref = "canvas"}
55 changes: 11 additions & 44 deletions src/main/java/dev/tylerm/khs/configuration/Items.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package dev.tylerm.khs.configuration;

import com.cryptomorin.xseries.XItemStack;
import dev.tylerm.khs.Main;
import dev.tylerm.khs.util.ItemUtil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
Expand Down Expand Up @@ -42,37 +39,37 @@ public static void loadItems() {
SEEKER_ITEMS.add(null);
continue;
}
ItemStack item = createItem(section);
ItemStack item = ItemUtil.createItem(section);
SEEKER_ITEMS.add(item);
}

ConfigurationSection SeekerHelmet = SeekerItems.getConfigurationSection("helmet");
if (SeekerHelmet != null) {
ItemStack item = createItem(SeekerHelmet);
ItemStack item = ItemUtil.createItem(SeekerHelmet);
if (item != null) {
SEEKER_HELM = item;
}
}

ConfigurationSection SeekerChestplate = SeekerItems.getConfigurationSection("chestplate");
if (SeekerChestplate != null) {
ItemStack item = createItem(SeekerChestplate);
ItemStack item = ItemUtil.createItem(SeekerChestplate);
if (item != null) {
SEEKER_CHEST = item;
}
}

ConfigurationSection SeekerLeggings = SeekerItems.getConfigurationSection("leggings");
if (SeekerLeggings != null) {
ItemStack item = createItem(SeekerLeggings);
ItemStack item = ItemUtil.createItem(SeekerLeggings);
if (item != null) {
SEEKER_LEGS = item;
}
}

ConfigurationSection SeekerBoots = SeekerItems.getConfigurationSection("boots");
if (SeekerBoots != null) {
ItemStack item = createItem(SeekerBoots);
ItemStack item = ItemUtil.createItem(SeekerBoots);
if (item != null) {
SEEKER_BOOTS = item;
}
Expand All @@ -92,37 +89,37 @@ public static void loadItems() {
HIDER_ITEMS.add(null);
continue;
}
ItemStack item = createItem(section);
ItemStack item = ItemUtil.createItem(section);
HIDER_ITEMS.add(item);
}

ConfigurationSection HiderHelmet = HiderItems.getConfigurationSection("helmet");
if (HiderHelmet != null) {
ItemStack item = createItem(HiderHelmet);
ItemStack item = ItemUtil.createItem(HiderHelmet);
if (item != null) {
HIDER_HELM = item;
}
}

ConfigurationSection HiderChestplate = HiderItems.getConfigurationSection("chestplate");
if (HiderChestplate != null) {
ItemStack item = createItem(HiderChestplate);
ItemStack item = ItemUtil.createItem(HiderChestplate);
if (item != null) {
HIDER_CHEST = item;
}
}

ConfigurationSection HiderLeggings = HiderItems.getConfigurationSection("leggings");
if (HiderLeggings != null) {
ItemStack item = createItem(HiderLeggings);
ItemStack item = ItemUtil.createItem(HiderLeggings);
if (item != null) {
HIDER_LEGS = item;
}
}

ConfigurationSection HiderBoots = HiderItems.getConfigurationSection("boots");
if (HiderBoots != null) {
ItemStack item = createItem(HiderBoots);
ItemStack item = ItemUtil.createItem(HiderBoots);
if (item != null) {
HIDER_BOOTS = item;
}
Expand Down Expand Up @@ -152,36 +149,6 @@ public static void loadItems() {
}
}

private static ItemStack createItem(ConfigurationSection item) {
ConfigurationSection config = new YamlConfiguration().createSection("temp");
String material = item.getString("material").toUpperCase();
boolean splash = false;
if (material.contains("POTION")) {
config.set("level", 1);
}
if (material.equalsIgnoreCase("SPLASH_POTION") || material.equalsIgnoreCase("LINGERING_POTION")) {
material = "POTION";
splash = true;
}
config.set("name", item.getString("name"));
config.set("material", material);
config.set("enchants", item.getConfigurationSection("enchantments"));
config.set("unbreakable", item.getBoolean("unbreakable"));
if (item.contains("model-data")) {
config.set("model-data", item.getInt("model-data"));
}
if (item.isSet("lore"))
config.set("lore", item.getStringList("lore"));
if (material.equalsIgnoreCase("POTION") || material.equalsIgnoreCase("SPLASH_POTION") || material.equalsIgnoreCase("LINGERING_POTION"))
config.set("base-effect", String.format("%s,%s,%s", item.getString("type"), false, splash));
ItemStack stack = XItemStack.deserialize(config);
int amt = item.getInt("amount");
if (amt < 1) amt = 1;
stack.setAmount(amt);
if (stack.getData().getItemType() == Material.AIR) return null;
return stack;
}

private static PotionEffect getPotionEffect(ConfigurationSection item) {
String type = item.getString("type");
if (type == null) return null;
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/dev/tylerm/khs/util/ItemUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dev.tylerm.khs.util;

import com.cryptomorin.xseries.XItemStack;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;

public class ItemUtil{
public static ItemStack createItem(ConfigurationSection item) {
ConfigurationSection config = new YamlConfiguration().createSection("temp");
String material = item.getString("material").toUpperCase();
boolean splash = false;
if (material.contains("POTION")) {
if(!item.contains("potionLevel")){
config.set("level", 1);
}else{
config.set("level", item.getInt("potionLevel"));
}
}
if (material.equalsIgnoreCase("SPLASH_POTION") || material.equalsIgnoreCase("LINGERING_POTION")) {
material = "POTION";
splash = true;
}
config.set("name", item.getString("name"));
config.set("material", material);
config.set("enchants", item.getConfigurationSection("enchantments"));
config.set("unbreakable", item.getBoolean("unbreakable"));
if (item.contains("model-data")) {
config.set("model-data", item.getInt("model-data"));
}
if (item.isSet("lore"))
config.set("lore", item.getStringList("lore"));
if (material.equalsIgnoreCase("POTION") || material.equalsIgnoreCase("SPLASH_POTION") || material.equalsIgnoreCase("LINGERING_POTION"))
config.set("base-effect", String.format("%s,%s,%s", item.getString("type"), false, splash));
ItemStack stack = XItemStack.deserialize(config);
int amt = item.getInt("amount");
if (amt < 1) amt = 1;
stack.setAmount(amt);
if (stack.getData().getItemType() == Material.AIR) return null;
return stack;
}
}
112 changes: 112 additions & 0 deletions src/main/java/dev/tylerm/khs/util/item/ItemStackBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
*
* AstralFlow - The plugin enriches bukkit servers
* Copyright (C) 2022 The Inlined Lambdas and Contributors
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/

package dev.tylerm.khs.util.item;

import dev.tylerm.khs.item.CustomItems;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.util.Objects.requireNonNull;

public final class ItemStackBuilder {
private Material material = Material.AIR;
private List<String> lore = new ArrayList<>();
private int customModelId;
private String displayName;
private List<ItemFlag> itemFlags = new ArrayList<>();
private int customItemId=-1;

private boolean unbreakable;

public ItemStackBuilder unbreakable() {
this.unbreakable = true;
return this;
}

public ItemStackBuilder material(Material material) {
requireNonNull(material, "Material cannot be null");
if(material.isAir()){
throw new IllegalArgumentException("Material cannot be AIR");
}
this.material = material;
return this;
}

public ItemStackBuilder lore(String... strings) {
requireNonNull(strings, "Lore cannot be null");
for (String string : strings) {
lore.add(string.replaceAll("&", ChatColor.COLOR_CHAR + ""));
}
return this;
}

public ItemStackBuilder customItemKey(int key){
customItemId = key;;
return this;
}

public ItemStackBuilder customModelId(int customModelId) {
if(customModelId < 0){
throw new IllegalArgumentException("Custom model id cannot be negative");
}
this.customModelId = customModelId;
return this;
}

public ItemStackBuilder displayName(String displayName) {
requireNonNull(displayName, "Display name cannot be null");
this.displayName = displayName.replaceAll("&", ChatColor.COLOR_CHAR + "");
return this;
}

public ItemStackBuilder itemFlags(ItemFlag... flags) {
requireNonNull(flags, "Flags cannot be null");
itemFlags.addAll(Arrays.asList(flags));
return this;
}


public ItemStack build() {
if (material == Material.AIR) {
throw new IllegalArgumentException("Material cannot be AIR");
}
var item = new ItemStack(material);
var meta = item.getItemMeta();
if (customModelId != 0 || displayName != null || !lore.isEmpty() || unbreakable || !itemFlags.isEmpty() || customItemId != 0) {
meta.setCustomModelData(customModelId);
meta.setLore(lore);
meta.setDisplayName(displayName);
meta.setUnbreakable(unbreakable);
meta.addItemFlags(itemFlags.toArray(new ItemFlag[0]));
}
meta.getPersistentDataContainer().set(CustomItems.CUSTOM_ITEM, PersistentDataType.INTEGER, customItemId);
item.setItemMeta(meta);
return item;
}
}
73 changes: 73 additions & 0 deletions src/main/java/dev/tylerm/khs/util/item/ItemStacks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
*
* AstralFlow - The plugin enriches bukkit servers
* Copyright (C) 2022 The Inlined Lambdas and Contributors
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/

package dev.tylerm.khs.util.item;

import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import static java.util.Objects.requireNonNull;

/**
* Util for working with ItemStacks.
*/
@ApiStatus.AvailableSince("0.1.0")
public final class ItemStacks {
/**
* Create an itemstack by a builder pattern.
*
* @param material The material of the itemstack.
* @return The itemstack.
*/
public static ItemStackBuilder builder(Material material) {
return new ItemStackBuilder().material(material);
}

/**
* A quicker way to create sample itemstacks.
*
* @param material material of the item
* @param name display name of the item
* @param lores lore of the item
* @return itemstack
*/
@NotNull
public static ItemStack of(Material material, String name, String... lores) {
requireNonNull(material);
if (material.isAir()) throw new IllegalArgumentException("Material cannot be air");
requireNonNull(name);
return builder(material).displayName(name).lore(lores == null ? new String[0] : lores).build();
}

public static ItemStack consumeOrNull(ItemStack itemStack, int amount) {
if (itemStack.getAmount() < amount) {
return null;
}
if (itemStack.getAmount() == amount) {
return new ItemStack(Material.AIR);
}
var item = itemStack.clone();
item.setAmount(item.getAmount() - amount);
return item;
}
}
Loading

0 comments on commit 447d66e

Please sign in to comment.