mirror of
https://github.com/HibiscusMC/HMCCosmetics.git
synced 2025-12-30 20:39:13 +00:00
Initial commit
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package io.github.fisher2911.hmccosmetics.util;
|
||||
|
||||
import io.github.fisher2911.hmccosmetics.message.Adventure;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class StringUtils {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param message message being translated
|
||||
* @param placeholders placeholders applied
|
||||
* @return message with placeholders applied
|
||||
*/
|
||||
|
||||
public static String applyPlaceholders(String message, final Map<String, String> placeholders) {
|
||||
for (final Map.Entry<String, String> entry : placeholders.entrySet()) {
|
||||
message = message.replace(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param parsed message to be parsed
|
||||
* @return MiniMessage parsed string
|
||||
*/
|
||||
|
||||
public static Component parse(final String parsed) {
|
||||
return Adventure.MINI_MESSAGE.parse(parsed);
|
||||
}
|
||||
|
||||
public static String parseStringToString(final String parsed) {
|
||||
return Adventure.SERIALIZER.serialize(Adventure.MINI_MESSAGE.parse(parsed));
|
||||
}
|
||||
}
|
||||
105
src/main/java/io/github/fisher2911/hmccosmetics/util/Utils.java
Normal file
105
src/main/java/io/github/fisher2911/hmccosmetics/util/Utils.java
Normal file
@@ -0,0 +1,105 @@
|
||||
package io.github.fisher2911.hmccosmetics.util;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class Utils {
|
||||
|
||||
/**
|
||||
* @param original Object to be checked if null
|
||||
* @param replacement Object returned if original is null
|
||||
* @return original if not null, otherwise replacement
|
||||
*/
|
||||
|
||||
public static <T> T replaceIfNull(final @Nullable T original, final @NotNull T replacement) {
|
||||
return replaceIfNull(original, replacement, t -> {});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param original Object to be checked if null
|
||||
* @param replacement Object returned if original is null
|
||||
* @param consumer accepts the original object, can be used for logging
|
||||
* @return original if not null, otherwise replacement
|
||||
*/
|
||||
|
||||
public static <T> T replaceIfNull(final @Nullable T original, final T replacement, final @NotNull Consumer<T> consumer) {
|
||||
if (original == null) {
|
||||
consumer.accept(replacement);
|
||||
return replacement;
|
||||
}
|
||||
consumer.accept(original);
|
||||
return original;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param t object being checked
|
||||
* @param consumer accepted if t is not null
|
||||
* @param <T> type
|
||||
*/
|
||||
|
||||
public static <T> void doIfNotNull(final @Nullable T t, final @NotNull Consumer<T> consumer) {
|
||||
if (t == null) {
|
||||
return;
|
||||
}
|
||||
consumer.accept(t);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param t object being checked
|
||||
* @param function applied if t is not null
|
||||
* @param <T> type
|
||||
* @return
|
||||
*/
|
||||
|
||||
public static <T> Optional<T> returnIfNotNull(final @Nullable T t, final @NotNull Function<T, T> function) {
|
||||
if (t == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(function.apply(t));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param enumAsString Enum value as a string to be parsed
|
||||
* @param enumClass enum type enumAsString is to be converted to
|
||||
* @param defaultEnum default value to be returned
|
||||
* @return enumAsString as an enum, or default enum if it could not be parsed
|
||||
*/
|
||||
|
||||
public static <E extends Enum<E>> E stringToEnum(final @NotNull String enumAsString,
|
||||
final @NotNull Class<E> enumClass,
|
||||
E defaultEnum) {
|
||||
return stringToEnum(enumAsString, enumClass, defaultEnum, e -> {});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param enumAsString Enum value as a string to be parsed
|
||||
* @param enumClass enum type enumAsString is to be converted to
|
||||
* @param defaultEnum default value to be returned
|
||||
* @param consumer accepts the returned enum, can be used for logging
|
||||
* @return enumAsString as an enum, or default enum if it could not be parsed
|
||||
*/
|
||||
|
||||
public static <E extends Enum<E>> E stringToEnum(final @NotNull String enumAsString,
|
||||
@NotNull final Class<E> enumClass,
|
||||
final E defaultEnum,
|
||||
final @NotNull Consumer<E> consumer) {
|
||||
try {
|
||||
final E value = Enum.valueOf(enumClass, enumAsString);
|
||||
consumer.accept(value);
|
||||
return value;
|
||||
} catch (final IllegalArgumentException exception) {
|
||||
consumer.accept(defaultEnum);
|
||||
return defaultEnum;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
package io.github.fisher2911.hmccosmetics.util.builder;
|
||||
|
||||
import io.github.fisher2911.hmccosmetics.message.Adventure;
|
||||
import io.github.fisher2911.hmccosmetics.util.StringUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class ItemBuilder {
|
||||
|
||||
protected Material material;
|
||||
protected int amount;
|
||||
protected ItemMeta itemMeta;
|
||||
|
||||
/**
|
||||
* @param material builder material
|
||||
*/
|
||||
|
||||
ItemBuilder(final Material material) {
|
||||
this.material = material;
|
||||
this.itemMeta = Bukkit.getItemFactory().getItemMeta(material);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param itemStack builder ItemStack
|
||||
*/
|
||||
|
||||
ItemBuilder(final ItemStack itemStack) {
|
||||
this.material = itemStack.getType();
|
||||
this.itemMeta = itemStack.hasItemMeta() ? itemStack.getItemMeta() : Bukkit.getItemFactory().getItemMeta(this.material);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param material builder material
|
||||
* @return
|
||||
*/
|
||||
|
||||
public static ItemBuilder from(final Material material) {
|
||||
return new ItemBuilder(material);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param itemStack builder ItemStack
|
||||
* @return
|
||||
*/
|
||||
|
||||
public static ItemBuilder from(final ItemStack itemStack) {
|
||||
return new ItemBuilder(itemStack);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param amount ItemStack amount
|
||||
* @return this
|
||||
*/
|
||||
|
||||
public ItemBuilder amount(final int amount) {
|
||||
this.amount = Math.min(Math.max(1, amount), 64);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name ItemStack name
|
||||
* @return this
|
||||
*/
|
||||
|
||||
public ItemBuilder name(final String name) {
|
||||
this.itemMeta.setDisplayName(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets placeholders to the item's name
|
||||
*
|
||||
* @param placeholders placeholders
|
||||
*/
|
||||
|
||||
public ItemBuilder namePlaceholders(final Map<String, String> placeholders) {
|
||||
final String name = StringUtils.
|
||||
applyPlaceholders(this.itemMeta.getDisplayName(), placeholders);
|
||||
this.itemMeta.displayName(
|
||||
Adventure.MINI_MESSAGE.parse(
|
||||
name));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param lore ItemStack lore
|
||||
* @return this
|
||||
*/
|
||||
|
||||
public ItemBuilder lore(final List<String> lore) {
|
||||
this.itemMeta.setLore(lore);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets placeholders to the item's lore
|
||||
*
|
||||
* @param placeholders placeholders
|
||||
*/
|
||||
|
||||
|
||||
public ItemBuilder lorePlaceholders(final Map<String, String> placeholders) {
|
||||
final List<String> lore = new ArrayList<>();
|
||||
|
||||
final List<String> previousLore = this.itemMeta.getLore();
|
||||
|
||||
if (previousLore == null) {
|
||||
return this;
|
||||
}
|
||||
|
||||
for (final String line : previousLore) {
|
||||
lore.add(StringUtils.applyPlaceholders(
|
||||
line, placeholders
|
||||
));
|
||||
}
|
||||
|
||||
this.itemMeta.setLore(lore);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param unbreakable whether the ItemStack is unbreakable
|
||||
* @return this
|
||||
*/
|
||||
|
||||
public ItemBuilder unbreakable(final boolean unbreakable) {
|
||||
this.itemMeta.setUnbreakable(unbreakable);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder glow(final boolean glow) {
|
||||
if (glow) {
|
||||
this.itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
|
||||
this.itemMeta.addEnchant(Enchantment.LUCK, 1, true);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param enchantments enchants to be added to the ItemStack
|
||||
* @param ignoreLeveLRestrictions whether to ignore enchantment level restrictions
|
||||
* @return this
|
||||
*/
|
||||
|
||||
public ItemBuilder enchants(final Map<Enchantment, Integer> enchantments, boolean ignoreLeveLRestrictions) {
|
||||
enchantments.forEach((enchantment, level) -> this.itemMeta.addEnchant(enchantment, level, ignoreLeveLRestrictions));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param itemFlags ItemStack ItemFlags
|
||||
* @return this
|
||||
*/
|
||||
|
||||
public ItemBuilder itemFlags(final Set<ItemFlag> itemFlags) {
|
||||
this.itemMeta.addItemFlags(itemFlags.toArray(new ItemFlag[0]));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param modelData ItemStack modelData
|
||||
* @return this
|
||||
*/
|
||||
|
||||
public ItemBuilder modelData(final int modelData) {
|
||||
this.itemMeta.setCustomModelData(modelData);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return built ItemStack
|
||||
*/
|
||||
|
||||
public ItemStack build() {
|
||||
final ItemStack itemStack = new ItemStack(this.material, Math.max(this.amount, 1));
|
||||
itemStack.setItemMeta(itemMeta);
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package io.github.fisher2911.hmccosmetics.util.builder;
|
||||
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.LeatherArmorMeta;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class LeatherArmorBuilder extends ItemBuilder{
|
||||
|
||||
private static final Set<Material> VALID_ARMOR = EnumSet.of(Material.LEATHER_BOOTS,
|
||||
Material.LEATHER_LEGGINGS, Material.LEATHER_CHESTPLATE, Material.LEATHER_HELMET);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param material ItemStack material
|
||||
*/
|
||||
|
||||
LeatherArmorBuilder(final Material material) {
|
||||
super(material);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param itemStack ItemStack
|
||||
*/
|
||||
|
||||
LeatherArmorBuilder(final ItemStack itemStack) {
|
||||
super(itemStack);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param material ItemStack material
|
||||
* @return this
|
||||
* @throws IllegalArgumentException thrown if material is not leather armor
|
||||
*/
|
||||
|
||||
public static LeatherArmorBuilder from(final Material material) throws IllegalArgumentException {
|
||||
if (!VALID_ARMOR.contains(material)) {
|
||||
throw new IllegalArgumentException(material.name() + " is not leather armor!");
|
||||
}
|
||||
return new LeatherArmorBuilder(material);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param itemStack ItemStack
|
||||
* @return this
|
||||
* @throws IllegalArgumentException thrown if itemStack's type is not leather armor
|
||||
*/
|
||||
|
||||
public static LeatherArmorBuilder from(final ItemStack itemStack) throws IllegalArgumentException {
|
||||
final Material material = itemStack.getType();
|
||||
if (!VALID_ARMOR.contains(material)) {
|
||||
throw new IllegalArgumentException(material.name() + " is not leather armor!");
|
||||
}
|
||||
return new LeatherArmorBuilder(itemStack);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param color armor color
|
||||
* @return this
|
||||
*/
|
||||
|
||||
public LeatherArmorBuilder color(final Color color) {
|
||||
if (itemMeta instanceof final LeatherArmorMeta meta) {
|
||||
meta.setColor(color);
|
||||
this.itemMeta = meta;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param material checked material
|
||||
* @return true if is leather armor, else false
|
||||
*/
|
||||
|
||||
public static boolean isLeatherArmor(final Material material) {
|
||||
return VALID_ARMOR.contains(material);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package io.github.fisher2911.hmccosmetics.util.builder;
|
||||
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import com.mojang.authlib.properties.Property;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.SkullMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Some parts taken from https://github.com/TriumphTeam/triumph-gui/blob/master/core/src/main/java/dev/triumphteam/gui/builder/item/SkullBuilder.java
|
||||
*/
|
||||
|
||||
public class SkullBuilder extends ItemBuilder {
|
||||
|
||||
private static final Field PROFILE_FIELD;
|
||||
|
||||
static {
|
||||
Field field;
|
||||
|
||||
try {
|
||||
final SkullMeta skullMeta = (SkullMeta) new ItemStack(Material.PLAYER_HEAD).getItemMeta();
|
||||
field = skullMeta.getClass().getDeclaredField("profile");
|
||||
field.setAccessible(true);
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
field = null;
|
||||
}
|
||||
|
||||
PROFILE_FIELD = field;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param material The material
|
||||
*/
|
||||
|
||||
SkullBuilder(final Material material) {
|
||||
super(material);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new SkullBuilder instance
|
||||
* @return this
|
||||
*/
|
||||
|
||||
public static SkullBuilder create() {
|
||||
return new SkullBuilder(Material.PLAYER_HEAD);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param player skull owner
|
||||
* @return this
|
||||
*/
|
||||
public SkullBuilder owner(final OfflinePlayer player) {
|
||||
if (this.itemMeta instanceof final SkullMeta skullMeta) {
|
||||
skullMeta.setOwningPlayer(player);
|
||||
this.itemMeta = skullMeta;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param texture skull texture
|
||||
* @return this
|
||||
*/
|
||||
|
||||
public SkullBuilder texture(@NotNull final String texture) {
|
||||
if (PROFILE_FIELD == null) {
|
||||
return this;
|
||||
}
|
||||
|
||||
final SkullMeta skullMeta = (SkullMeta) this.itemMeta;
|
||||
final GameProfile profile = new GameProfile(UUID.randomUUID(), null);
|
||||
profile.getProperties().put("textures", new Property("textures", texture));
|
||||
|
||||
try {
|
||||
PROFILE_FIELD.set(skullMeta, profile);
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
this.itemMeta = skullMeta;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user