Skip to content

Instantly share code, notes, and snippets.

@mattnicee7
Last active December 18, 2021 09:13
Show Gist options
  • Save mattnicee7/ea08173b9a7de8b1dddf822f3c4c3948 to your computer and use it in GitHub Desktop.
Save mattnicee7/ea08173b9a7de8b1dddf822f3c4c3948 to your computer and use it in GitHub Desktop.
itemstack adapter with configsection
package com.github.mattnicee7.myplugin.adapter;
public interface Adapter<T, V> {
T adapt(V data);
}
package com.github.mattnicee7.myplugin.util.chat;
import lombok.experimental.UtilityClass;
import org.bukkit.ChatColor;
@UtilityClass
public class ChatUtils {
public String colorize(String message) {
return ChatColor.translateAlternateColorCodes('&', message);
}
}
Item:
Material: 'DIAMOND_PICKAXE'
Name: '&aPicareta Especial'
Amount: 1
Glow: true
Lore:
- '&7Uma lore legal.'
- '&7E bonita.'
Enchants:
- 'DIG_SPEED:5'
- 'LOOT_BONUS_BLOCKS:3'
- 'DURABILITY:3'
package com.github.mattnicee7.myplugin.util;
import com.github.mattnicee7.myplugin.util.chat.ChatUtils;
import lombok.val;
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.Arrays;
import java.util.List;
public class ItemBuilder {
private ItemStack item;
private ItemBuilder(Material material) {
item = new ItemStack(material);
}
public static ItemBuilder of(Material material) {
return new ItemBuilder(material);
}
public ItemBuilder setName(String name) {
ItemMeta itemMeta = item.getItemMeta();
itemMeta.setDisplayName(ChatUtils.colorize(name));
item.setItemMeta(itemMeta);
return this;
}
public ItemBuilder setLore(List<String> lores) {
ItemMeta itemMeta = item.getItemMeta();
itemMeta.setLore(lores);
item.setItemMeta(itemMeta);
return this;
}
public ItemBuilder setLore(String... lores) {
return setLore(Arrays.asList(lores));
}
public ItemBuilder setGlow(boolean state) {
if (!state)
return this;
item.addUnsafeEnchantment(Enchantment.DURABILITY, 1);
ItemMeta itemMeta = item.getItemMeta();
itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
item.setItemMeta(itemMeta);
return this;
}
public ItemBuilder setAmount(int amount) {
item.setAmount(amount);
return this;
}
public ItemBuilder addEnchant(Enchantment enchantment, int level) {
item.addUnsafeEnchantment(enchantment, level);
return this;
}
public ItemBuilder addEnchants(List<String> data) {
if (data.size() < 1) return this;
for (String string : data) {
val split = string.split(":");
item.addUnsafeEnchantment(Enchantment.getByName(split[0]), Integer.parseInt(split[1]));
}
return this;
}
public ItemStack build() {
return item;
}
}
package com.github.mattnicee7.myplugin.adapter.impl;
import com.github.mattnicee7.myplugin.adapter.Adapter;
import com.github.mattnicee7.myplugin.util.ItemBuilder;
import com.github.mattnicee7.myplugin.util.chat.ChatUtils;
import lombok.val;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
import java.util.stream.Collectors;
public class ItemStackAdapter implements Adapter<ItemStack, ConfigurationSection> {
@Override
public ItemStack adapt(ConfigurationSection data) {
val material = Material.valueOf(data.getString("Material"));
val name = ChatUtils.colorize(data.getString("Name"));
val amount = data.getInt("Amount");
val glow = data.getBoolean("Glow");
val lore = data.getStringList("Lore").stream().map(ColorUtils::colorize).collect(Collectors.toList());
val enchants = data.getStringList("Enchants");
return ItemBuilder.of(material)
.setName(name)
.setAmount(amount)
.setGlow(glow)
.setLore(lore)
.addEnchants(enchants)
.build();
}
}
package com.github.mattnicee7.myplugin;
import com.github.mattnicee7.myplugin.adapter.impl.ItemStackAdapter;
import lombok.val;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
public final class MyPlugin extends JavaPlugin {
private ItemStackAdapter itemStackAdapter;
@Override
public void onEnable() {
saveDefaultConfig();
loadModules();
itemExample();
}
private void loadModules() {
itemStackAdapter = new ItemStackAdapter();
}
private void itemExample() {
val section = getConfig().getConfigurationSection("Item");
ItemStack itemStack = itemStackAdapter.adapt(section);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment