Last active
December 18, 2021 09:13
-
-
Save mattnicee7/ea08173b9a7de8b1dddf822f3c4c3948 to your computer and use it in GitHub Desktop.
itemstack adapter with configsection
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
package com.github.mattnicee7.myplugin.adapter; | |
public interface Adapter<T, V> { | |
T adapt(V data); | |
} |
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
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); | |
} | |
} |
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
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' |
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
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; | |
} | |
} |
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
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(); | |
} | |
} |
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
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