Skip to content

Commit

Permalink
Added PotionBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
funkemunky committed Mar 23, 2023
1 parent 6c1a7a0 commit 47ae3af
Show file tree
Hide file tree
Showing 4 changed files with 377 additions and 195 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package cc.funkemunky.api.utils;

import org.bukkit.Material;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.Potion;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionType;

import java.util.*;

public class PotionBuilder {

private String name;
private List<String> description;
private final Map<PotionEffect, Boolean> potionEffects;
private ItemFlag[] itemFlags;
private final PotionType type;
private int amount = 1;

public PotionBuilder(PotionType potionType) {
this.potionEffects = new HashMap<>();
this.type = potionType;
}

public PotionBuilder setName(String name) {
this.name = name;
return this;
}

public PotionBuilder setDescription(String... lines) {
this.description = new ArrayList<>(Arrays.asList(lines));
return this;
}

public PotionBuilder addEffect(PotionEffect potionEffect, boolean overwrite) {
potionEffects.put(potionEffect, overwrite);
return this;
}

public PotionBuilder addEffect(PotionEffect potionEffect) {
potionEffects.put(potionEffect, true);
return this;
}

public PotionBuilder amount(int amount) {
this.amount = amount;
return this;
}

public PotionBuilder addEffects(HashMap<PotionEffect, Boolean> potionEffects) {
this.potionEffects.putAll(potionEffects);
return this;
}

public PotionBuilder addItemFlags(ItemFlag... itemFlags) {
this.itemFlags = itemFlags;
return this;
}

public ItemStack build() {


ItemStack potionItem = new ItemStack(Material.POTION);
new Potion(type).apply(potionItem);

potionItem.setAmount(amount);

PotionMeta pM = (PotionMeta) potionItem.getItemMeta();
if (name != null) pM.setDisplayName(name);
if (description != null) pM.setLore(description);
for (Map.Entry<PotionEffect, Boolean> effect : potionEffects.entrySet()) {
pM.addCustomEffect(effect.getKey(), effect.getValue());
}
if (itemFlags != null) pM.addItemFlags(itemFlags);
potionItem.setItemMeta(pM);


return potionItem;
}

public static PotionBuilder of(PotionType type) {
return new PotionBuilder(type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cc.funkemunky.api.utils;

import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

public class PotionEffectBuilder {
private int amplifier;
private int duration;
private PotionEffectType type;
private boolean ambient;
private boolean particles;

private PotionEffectBuilder() {
duration = 100;
amplifier = 0;
type = PotionEffectType.SPEED;
ambient = false;
particles = true;
}

public PotionEffectBuilder amplifier(int amplifier) {
this.amplifier = amplifier;
return this;
}

public PotionEffectBuilder duration(int duration) {
this.duration = duration;
return this;
}

public PotionEffectBuilder type(PotionEffectType type) {
this.type = type;
return this;
}

public PotionEffectBuilder ambient(boolean ambient) {
this.ambient = ambient;
return this;
}

public PotionEffectBuilder particles(boolean particles) {
this.particles = particles;
return this;
}

public PotionEffect build() {
return new PotionEffect(type, duration, amplifier, ambient, particles);
}


public static PotionEffectBuilder builder() {
return new PotionEffectBuilder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,17 @@
package cc.funkemunky.api.utils;

import com.google.common.base.Enums;
import com.google.common.base.Strings;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.apache.commons.lang.WordUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;
import java.util.stream.Collectors;

/**
* Enchantment support with multiple aliases.
Expand Down Expand Up @@ -237,7 +232,7 @@ private static String format(@Nonnull String name) {
*/
@Nonnull
public static Optional<XEnchantment> matchXEnchantment(@Nonnull String enchantment) {
Validate.notEmpty(enchantment, "Enchantment name cannot be null or empty");
if (enchantment == null || enchantment.isEmpty()) throw new IllegalArgumentException("Enchantment name cannot be null or empty");
return Optional.ofNullable(Data.NAMES.get(format(enchantment)));
}

Expand All @@ -258,47 +253,6 @@ public static XEnchantment matchXEnchantment(@Nonnull Enchantment enchantment) {
return Objects.requireNonNull(Data.NAMES.get(enchantment.getName()), () -> "Unsupported enchantment: " + enchantment.getName());
}

/**
* Adds an unsafe enchantment to the given item from a string.
* <p>
* <blockquote><pre>
* ItemStack item = ...;
* addEnchantFromString(item, "unbreaking, 10");
* addEnchantFromString(item, "mending");
* </pre></blockquote>
* <p>
* Note that if you set your item's meta {@link ItemStack#setItemMeta(ItemMeta)} the enchantment
* will be removed.
* You need to use {@link ItemMeta#addEnchant(Enchantment, int, boolean)} instead.
* You can use the {@link #matchXEnchantment(String)} method in this case.
*
* @param item the item to add the enchantment to.
* @param enchantment the enchantment string containing the enchantment name and level (optional)
*
* @return an enchanted {@link ItemStack} or the item itself without enchantment added if enchantment type is null.
* @see #matchXEnchantment(String)
* @since 1.0.0
*/
@Nonnull
public static ItemStack addEnchantFromString(@Nonnull ItemStack item, @Nullable String enchantment) {
Objects.requireNonNull(item, "Cannot add enchantment to null ItemStack");
if (Strings.isNullOrEmpty(enchantment) || enchantment.equalsIgnoreCase("none")) return item;

String[] split = StringUtils.split(StringUtils.deleteWhitespace(enchantment), ',');
if (split.length == 0) split = StringUtils.split(enchantment, ' ');

Optional<XEnchantment> enchantOpt = matchXEnchantment(split[0]);
if (!enchantOpt.isPresent()) return item;
Enchantment enchant = enchantOpt.get().enchantment;
if (enchant == null) return item;

int lvl = 1;
if (split.length > 1) lvl = NumberUtils.toInt(split[1]);

item.addUnsafeEnchantment(enchant, lvl);
return item;
}

/**
* Gets the enchanted book of this enchantment.
*
Expand Down Expand Up @@ -352,7 +306,9 @@ public boolean isSupported() {
@Override
@Nonnull
public String toString() {
return WordUtils.capitalize(this.name().replace('_', ' ').toLowerCase(Locale.ENGLISH));
return Arrays.stream(name().split("_"))
.map(t -> t.charAt(0) + t.substring(1).toLowerCase())
.collect(Collectors.joining(" "));
}

/**
Expand All @@ -377,4 +333,4 @@ private static final class Data {
ISFLAT = flat;
}
}
}
}
Loading

0 comments on commit 47ae3af

Please sign in to comment.