Last active
May 31, 2021 17:16
-
-
Save noobmobile/3d44341bc15917126bc11ef9640ccbf2 to your computer and use it in GitHub Desktop.
Classe para facilitar a criação de itens, incluindo itens com listeners
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.dont.modelo.utils; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.InvocationTargetException; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.UUID; | |
import java.util.function.Consumer; | |
import java.util.stream.Collectors; | |
import org.apache.commons.codec.binary.Base64; | |
import org.bukkit.Bukkit; | |
import org.bukkit.ChatColor; | |
import org.bukkit.Material; | |
import org.bukkit.enchantments.Enchantment; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.player.PlayerInteractEvent; | |
import org.bukkit.inventory.ItemFlag; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import org.bukkit.inventory.meta.SkullMeta; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import com.mojang.authlib.GameProfile; | |
import com.mojang.authlib.properties.Property; | |
public class ItemComposer { | |
private ItemStack item; | |
public ItemComposer(ItemStack item) { | |
this.item = item; | |
} | |
public ItemComposer(Material material) { | |
this.item = new ItemStack(material); | |
} | |
public ItemComposer(Material material, int amount, int durability) { | |
this.item = new ItemStack(material, amount, (short) durability); | |
} | |
public ItemComposer(String url) { | |
ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) 3); | |
if (url == null || url.isEmpty()) { | |
this.item = skull; | |
return; | |
} | |
if (!url.startsWith("http://textures.minecraft.net/texture/")) url = "http://textures.minecraft.net/texture/" + url; | |
try { | |
SkullMeta skullMeta = (SkullMeta) skull.getItemMeta(); | |
GameProfile profile = new GameProfile(UUID.nameUUIDFromBytes(url.getBytes()), null); | |
profile.getProperties().put("textures", new Property("textures", new String(Base64.encodeBase64(String.format("{textures:{SKIN:{url:\"%s\"}}}", url).getBytes())))); | |
Field profileField = skullMeta.getClass().getDeclaredField("profile"); | |
profileField.setAccessible(true); | |
profileField.set(skullMeta, profile); | |
skull.setItemMeta(skullMeta); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
this.item = skull; | |
} | |
public static ItemComposer of(Material material) { | |
return new ItemComposer(material); | |
} | |
public static ItemComposer of(Material material, int amount, int durability) { | |
return new ItemComposer(material, amount, durability); | |
} | |
public static ItemComposer of(ItemStack item) { | |
return new ItemComposer(item); | |
} | |
public static ItemComposer of(String url) { | |
return new ItemComposer(url); | |
} | |
public ItemComposer compose(Consumer<ItemStack> consumer) { | |
consumer.accept(item); | |
return this; | |
} | |
/** | |
créditos a: https://github.com/TheMFjulio/MFLib/blob/master/src/main/java/com/mateus/mflib/util/ItemBuilder.java | |
*/ | |
public ItemComposer setNBTTag(String key, String value) { | |
try { | |
Object nmsCopy = NMSReflect.getCraftBukkitClass("inventory", "CraftItemStack").getMethod("asNMSCopy", ItemStack.class).invoke(null, item); | |
Object nbtTagCompound = NMSReflect.getNMSClass("NBTTagCompound").getConstructor().newInstance(); | |
boolean b = nmsCopy.getClass().getMethod("getTag").invoke(nmsCopy) != null; | |
Object nbtTag = b ? nmsCopy.getClass().getMethod("getTag").invoke(nmsCopy) : nbtTagCompound; | |
Constructor nbsString = NMSReflect.getNMSClass("NBTTagString").getConstructor(String.class); | |
nbtTag.getClass().getMethod("set", String.class, NMSReflect.getNMSClass("NBTBase")) | |
.invoke(nbtTag ,key, nbsString.newInstance(value)); | |
nmsCopy.getClass().getMethod("setTag", NMSReflect.getNMSClass("NBTTagCompound")).invoke(nmsCopy, nbtTag); | |
this.item = (ItemStack) NMSReflect.getCraftBukkitClass("inventory", "CraftItemStack").getMethod("asBukkitCopy", NMSReflect.getNMSClass("ItemStack")) | |
.invoke(null,nmsCopy); | |
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | InstantiationException e) { | |
e.printStackTrace(); | |
} | |
return this; | |
} | |
public ItemComposer composeMeta(Consumer<ItemMeta> consumer) { | |
ItemMeta meta = item.getItemMeta(); | |
consumer.accept(meta); | |
item.setItemMeta(meta); | |
return this; | |
} | |
public ItemComposer setName(String name) { | |
if (name == null || name.equalsIgnoreCase("nulo")) return this; | |
composeMeta(meta -> meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name))); | |
return this; | |
} | |
public ItemComposer setLore(List<String> lore) { | |
if (lore == null || lore.isEmpty() || lore.get(0).equalsIgnoreCase("nulo")) return this; | |
composeMeta(meta -> meta.setLore(lore.stream().map(string -> ChatColor.translateAlternateColorCodes('&', string)).collect(Collectors.toList()))); | |
return this; | |
} | |
public ItemComposer setLore(String... lore) { | |
return setLore(Arrays.asList(lore)); | |
} | |
public ItemComposer addLore(String... lore){ | |
return addLore(Arrays.asList(lore)); | |
} | |
public ItemComposer addLore(List<String> lore){ | |
composeMeta(meta -> { | |
List<String> newLore = meta.getLore(); | |
newLore.addAll((lore)); | |
meta.setLore(newLore); | |
}); | |
return this; | |
} | |
public ItemComposer addGlow(boolean glow){ | |
if (!glow) return this; | |
compose(it -> it.addUnsafeEnchantment(Enchantment.ARROW_DAMAGE,1)); | |
composeMeta(meta -> meta.addItemFlags(ItemFlag.HIDE_ENCHANTS)); | |
return this; | |
} | |
public ItemComposer setAmount(int amount) { | |
compose(it -> it.setAmount(amount)); | |
return this; | |
} | |
public ItemComposer setDurability(int durability) { | |
compose(it -> it.setDurability((short) durability)); | |
return this; | |
} | |
public ItemComposer addEnchantment(Enchantment enchantment, int level) { | |
compose(it -> it.addUnsafeEnchantment(enchantment, level)); | |
return this; | |
} | |
public ItemComposer addEnchantments(HashMap<Enchantment, Integer> enchantments) { | |
compose(it -> it.addUnsafeEnchantments(enchantments)); | |
return this; | |
} | |
public ItemComposer addItemFlag(ItemFlag... itemflag) { | |
composeMeta(meta -> meta.addItemFlags(itemflag)); | |
return this; | |
} | |
public ItemComposer setSkullOwner(String owner) { | |
composeMeta(meta -> { | |
SkullMeta skullMeta = (SkullMeta) meta; | |
skullMeta.setOwner(owner); | |
}); | |
return this; | |
} | |
public ItemComposer setClickListener(JavaPlugin plugin, Consumer<PlayerInteractEvent> consumer) { | |
Bukkit.getPluginManager().registerEvents(new Listener() { | |
@EventHandler | |
public void onInteract(PlayerInteractEvent e) { | |
if (e.hasItem() && e.getItem().isSimilar(item)) { | |
consumer.accept(e); | |
} | |
} | |
}, plugin); | |
return this; | |
} | |
/** | |
créditos a: https://github.com/TheMFjulio/MFLib/blob/master/src/main/java/com/mateus/mflib/util/NBTGetter.java | |
*/ | |
public static String getNBTTag(ItemStack item, String key){ | |
try { | |
Object nmsCopy = NMSReflect.getCraftBukkitClass("inventory", "CraftItemStack").getMethod("asNMSCopy", ItemStack.class).invoke(null,item); | |
if (nmsCopy.getClass().getMethod("getTag").invoke(nmsCopy) != null) { | |
Object tagCompound = nmsCopy.getClass().getMethod("getTag").invoke(nmsCopy); | |
return (String) tagCompound.getClass().getMethod("getString", String.class).invoke(tagCompound, key); | |
} | |
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
public ItemStack toItemStack() { | |
return item; | |
} | |
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
// criando itens normais | |
inventory.addItem(ItemComposer.of(Material.DIAMOND).setName("&6eae").setLore("&3eae", "&5tudo bem").toItemStack()); | |
inventory.addItem(ItemComposer.of(Material.DIAMOND_SWORD).addEnchantment(Enchantment.ARROW_DAMAGE, 4).addEnchantment(Enchantment.ARROW_FIRE, 3).toItemStack()); | |
inventory.addItem(ItemComposer.of(Material.SKULL_ITEM).setDurability(3).setSkullOwner("LurionK").toItemStack()); | |
inventory.addItem(ItemComposer.of("fa8887814578ce7c540d7ab8cc6b2a2e22a7492cc86c65a7e839c887b2ed62").setName("§2Cabeça personalizada").toItemStack()); | |
// itens com listeners só podem ser instanciados uma vez | |
public class ItemManager { | |
public final ItemStack analisador; | |
public final ItemStack quebrador; | |
public ItemManager(){ | |
Main main = Main.getPlugin(Main.class); | |
analisador = ItemComposer.of(Material.STICK).setName("§6Analisador").setClickListener(main, e -> { | |
if (e.hasBlock()) { | |
e.getPlayer().sendMessage("§6Nome desse bloco: §f" + e.getClickedBlock().getType().name()); | |
} | |
}).toItemStack(); | |
quebrador = ItemComposer.of(Material.STICK).setName("§3Quebrador").setClickListener(main, e -> { | |
if (e.hasBlock()) { | |
e.getBlock().breakNaturally(); | |
} | |
}).toItemStack(); | |
} | |
} | |
public class Example implements CommandExecutor{ | |
@Override | |
public boolean onCommand(CommandSender sender, Command command, String arg2, String[] args) { | |
if (sender instanceof Player) { | |
Player player = (Player) sender; | |
player.getInventory().addItem(Main.getItemManager().analisador); | |
player.getInventory().addItem(Main.getItemManager().quebrador); | |
} | |
return false; | |
} | |
} | |
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.dont.modelo.utils; | |
import org.bukkit.Bukkit; | |
import org.bukkit.entity.Player; | |
import org.bukkit.inventory.ItemStack; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.HashMap; | |
/** | |
créditos a: https://raw.githubusercontent.com/TheMFjulio/MFLib/master/src/main/java/com/mateus/mflib/nms/NMSReflect.java | |
adaptado por: dont | |
*/ | |
public class NMSReflect { | |
private static final String VERSION = Bukkit.getServer().getClass().getName().split("\\.")[3]; | |
private static final HashMap<String, Class> CLASSES = new HashMap<>(); | |
public static Class getNMSClass(String name) { | |
if (!CLASSES.containsKey(name)) { | |
try { | |
CLASSES.put(name, Class.forName("net.minecraft.server." + VERSION + "." + name)); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
return CLASSES.get(name); | |
} | |
public static Class getCraftBukkitClass(String prefix, String name) { | |
if (!CLASSES.containsKey(prefix+"."+name)){ | |
try { | |
CLASSES.put(prefix+"."+name, Class.forName("org.bukkit.craftbukkit." + VERSION + "." + prefix + "." + name)); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
return CLASSES.get(prefix+"."+name); | |
} | |
public static void sendPacket(Player player, Object packet) { | |
try { | |
Object handle = player.getClass().getMethod("getHandle").invoke(player); | |
Object playerConnection = handle.getClass().getField("playerConnection").get(handle); | |
playerConnection.getClass().getMethod("sendPacket", getNMSClass("Packet")) | |
.invoke(playerConnection, packet); | |
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | NoSuchFieldException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment