-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ItemBuilder (from AstralFlow)
- Loading branch information
Showing
6 changed files
with
245 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
112
src/main/java/dev/tylerm/khs/util/item/ItemStackBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.