package me.km.utils; import java.util.ArrayList; import me.km.utils.ItemStackUtils.ItemFlag; import net.minecraft.block.Block; import net.minecraft.init.Enchantments; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; public class ItemStackBuilder { private final ItemStack stack; public ItemStackBuilder(Block b, int amount) { stack = new ItemStack(b, amount); } public ItemStackBuilder(Block b, int amount, int meta) { stack = new ItemStack(b, amount, meta); } public ItemStackBuilder(Item i, int amount) { stack = new ItemStack(i, amount); } public ItemStackBuilder(Item i, int amount, int meta) { stack = new ItemStack(i, amount, meta); } public ItemStackBuilder(ItemStack stack, int amount) { this.stack = stack.copy(); this.stack.setCount(amount); } public ItemStack build() { return stack; } public void drop(World w, BlockPos l) { Block.spawnAsEntity(w, l, stack); } public ItemStackBuilder setName(String name) { stack.setStackDisplayName(name); return this; } public ItemStackBuilder addToName(String name) { stack.setStackDisplayName(stack.getDisplayName() + name); return this; } public ItemStackBuilder addLore(String line) { ItemStackUtils.addLore(stack, line); return this; } public ItemStackBuilder addLimitedLore(String whole, int limit, String addition) { ItemStackUtils.setLore(stack, buildLimitedLore(whole, limit, addition)); return this; } public ItemStackBuilder addLimitedLore(String whole, String addition) { return addLimitedLore(whole, 25, addition); } public static ArrayList buildLimitedLore(String whole, int limit, String addition) { ArrayList list = new ArrayList<>(); int pos = 0; int space; while(pos + limit < whole.length()) { space = whole.lastIndexOf(" ", pos + limit); if(space == -1 || pos > space) { space = whole.indexOf(" ", pos + limit); } list.add(addition + whole.substring(pos, space)); pos = space + 1; } list.add(addition + whole.substring(pos)); return list; } public ItemStackBuilder hideTags() { ItemStackUtils.addItemFlag(stack, ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_POTION_EFFECTS, ItemFlag.HIDE_ENCHANTS); return this; } public ItemStackBuilder addGlow() { stack.addEnchantment(Enchantments.PROTECTION, 1); return this; } }