Created
August 17, 2016 20:28
-
-
Save alexisvisco/787ce65db122015502a1111754f59f1b to your computer and use it in GitHub Desktop.
Somes builders
This file contains hidden or 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 fr.kwizzy.deathdash.util; | |
| /** | |
| * Par Alexis le 04/05/2016. | |
| */ | |
| import java.util.ArrayList; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import javax.annotation.CheckForNull; | |
| import fr.kwizzy.deathdash.DeathDashPlugin; | |
| import org.bukkit.Bukkit; | |
| import org.bukkit.Material; | |
| import org.bukkit.entity.Player; | |
| import org.bukkit.event.EventHandler; | |
| import org.bukkit.event.Listener; | |
| import org.bukkit.event.inventory.InventoryClickEvent; | |
| import org.bukkit.event.inventory.InventoryCloseEvent; | |
| import org.bukkit.event.player.PlayerQuitEvent; | |
| import org.bukkit.inventory.Inventory; | |
| import org.bukkit.inventory.ItemStack; | |
| import org.bukkit.plugin.Plugin; | |
| import org.bukkit.scheduler.BukkitRunnable; | |
| public abstract class GUIBuilder implements Listener | |
| { | |
| public static HashMap<Player, Inventory> invManager = new HashMap<>(); | |
| public static HashMap<Player, GUIBuilder> guiManager = new HashMap<>(); | |
| private static int DO_REGISTER = 1; | |
| private Plugin instance = DeathDashPlugin.i(); | |
| private int size; | |
| private String title; | |
| public List<Inventory> inv = new ArrayList<>(); | |
| private int nbPage = 0; | |
| private Player p; | |
| /** | |
| * Without pagination | |
| * | |
| * @param title | |
| * Title of the | |
| */ | |
| public GUIBuilder(String title, Player p) { | |
| this.p = p; | |
| this.title = title; | |
| } | |
| /** | |
| * | |
| * @param title | |
| * title | |
| * @param nbPage | |
| * nb of page(s) | |
| */ | |
| public GUIBuilder(String title, int nbPage, Player p) { | |
| this(title, p); | |
| this.nbPage = nbPage; | |
| } | |
| @CheckForNull | |
| public ItemStack getItemBySlot(int slot, int page) { | |
| Inventory i = inv.get(page); | |
| if (i != null) { | |
| return i.getContents()[slot]; | |
| } | |
| return null; | |
| } | |
| /** | |
| * Setup gui | |
| * | |
| * @param p | |
| * player | |
| */ | |
| public void showGUI(int size, Player p, boolean hasPage) { | |
| register(); | |
| this.size = size * 9; | |
| if (size > 6) | |
| this.size = 6 * 9; | |
| if (!hasPage) | |
| inv.add(Bukkit.createInventory(null, this.size, title)); | |
| else { | |
| for (int i = 0; i <= nbPage; i++) { | |
| inv.add(Bukkit.createInventory(null, this.size, title)); | |
| } | |
| placePageIndicator(); | |
| } | |
| p.openInventory(inv.get(0)); | |
| putValueInv(p, inv.get(0)); | |
| putValueGui(p, this); | |
| } | |
| /** | |
| * add 1 page | |
| * | |
| */ | |
| public void addPage() { | |
| nbPage++; | |
| inv.add(Bukkit.createInventory(null, size, title)); | |
| placePageIndicator(); | |
| } | |
| public void addPageDiffSize(int size) { | |
| nbPage++; | |
| if (size > 6) | |
| size = 6 * 9; | |
| inv.add(Bukkit.createInventory(null, size * 9, title)); | |
| placePageIndicator(); | |
| } | |
| private void putValueInv(Player p, Inventory value) { | |
| if (invManager.containsKey(p)) { | |
| invManager.remove(p); | |
| } | |
| invManager.put(p, value); | |
| } | |
| private void putValueGui(Player p, GUIBuilder value) { | |
| if (guiManager.containsKey(p)) { | |
| guiManager.remove(p); | |
| } | |
| guiManager.put(p, value); | |
| } | |
| /** | |
| * add {@param i} page(s) | |
| * | |
| * @param i | |
| */ | |
| public void addPage(int i) { | |
| nbPage += i; | |
| for (int x = 0; x < nbPage; x++) { | |
| inv.add(Bukkit.createInventory(null, size, title)); | |
| } | |
| placePageIndicator(); | |
| } | |
| /** | |
| * Set a item to a position and on the default page | |
| * | |
| * @param is | |
| * itemstack | |
| * @param position | |
| * slot | |
| */ | |
| public void setItemPosition(ItemStack is, int position) { | |
| new BukkitRunnable() { | |
| @Override | |
| public void run() { | |
| inv.get(0).setItem(position, is); | |
| } | |
| }.runTaskLater(instance, 1); | |
| } | |
| /** | |
| * Set a item to a position with a page | |
| * | |
| * @param is | |
| * itemstack | |
| * @param position | |
| * slot | |
| * @param page | |
| * page | |
| */ | |
| public void setItemPosition(ItemStack is, int position, int page) { | |
| if (nbPage > 0) | |
| if (position > size - 10) | |
| return; | |
| new BukkitRunnable() { | |
| @Override | |
| public void run() { | |
| inv.get(page).setItem(position, is); | |
| } | |
| }.runTaskLater(instance, 1); | |
| } | |
| /** | |
| * Set a item to a position with a page | |
| * | |
| * @param is | |
| * itemstack | |
| * @param position | |
| * slot | |
| * @param page | |
| * page | |
| */ | |
| private void setItemPositionBypass(ItemStack is, int position, int page) { | |
| new BukkitRunnable() { | |
| @Override | |
| public void run() { | |
| inv.get(page).setItem(position, is); | |
| } | |
| }.runTaskLater(instance, 1); | |
| } | |
| /** | |
| * Add item to the default page | |
| * | |
| * @param is | |
| * itemstack | |
| */ | |
| @Deprecated | |
| public void addItem(ItemStack is) { | |
| new BukkitRunnable() { | |
| @Override | |
| public void run() { | |
| inv.get(0).addItem(is); | |
| } | |
| }.runTaskLater(instance, 1); | |
| } | |
| /** | |
| * Add item to the page | |
| * | |
| * @param is | |
| * itemstack | |
| * @param page | |
| * page | |
| */ | |
| @Deprecated | |
| public void addItem(ItemStack is, int page) { | |
| new BukkitRunnable() { | |
| @Override | |
| public void run() { | |
| inv.get(page).addItem(is); | |
| } | |
| }.runTaskLater(instance, 1); | |
| } | |
| /** | |
| * | |
| * @return size of list<inv> | |
| */ | |
| private int size() { | |
| return inv.size(); | |
| } | |
| /** | |
| * register the current class | |
| */ | |
| private void register() { | |
| if (DO_REGISTER > 0) | |
| instance.getServer().getPluginManager().registerEvents(this, instance); | |
| DO_REGISTER = 0; | |
| } | |
| /** | |
| * | |
| * @return next item | |
| */ | |
| private ItemStack next() { | |
| ItemBuilder im = new ItemBuilder(Material.STONE_BUTTON, 1, "§eSuivant >"); | |
| return im.build(); | |
| } | |
| /** | |
| * | |
| * @return back item | |
| */ | |
| private ItemStack back() { | |
| ItemBuilder im = new ItemBuilder(Material.STONE_BUTTON, 1, "§e< Précédent"); | |
| return im.build(); | |
| } | |
| /** | |
| * | |
| * @param page | |
| * int | |
| * @return midle page | |
| */ | |
| private ItemStack midle(int page) { | |
| ItemBuilder im = new ItemBuilder(Material.ENDER_PEARL, 1, "§ePage : " + page); | |
| im.lore("§7" + page + "/" + nbPage); | |
| im.glow(); | |
| return im.build(); | |
| } | |
| /** | |
| * set indicator for page < . > | |
| */ | |
| private void placePageIndicator() { | |
| for (int i = 0; i < inv.size(); i++) { | |
| if (i != inv.size() - 1) | |
| setItemPositionBypass(next(), size - 1, i); | |
| if (i != 0) | |
| setItemPositionBypass(back(), size - 9, i); | |
| setItemPositionBypass(midle(i), size - 5, i); | |
| } | |
| } | |
| /** | |
| * go to the next page | |
| * | |
| * @param page | |
| * page | |
| * @param p | |
| * player | |
| */ | |
| private void pageNext(int page, Player p) { | |
| if (size() > page + 1) { | |
| p.openInventory(inv.get(page + 1)); | |
| putValueInv(p, inv.get(page + 1)); | |
| putValueGui(p, this); | |
| return; | |
| } | |
| p.sendMessage("§cIl n'y a pas de page suivante !"); | |
| } | |
| /** | |
| * go to the next page | |
| * | |
| * @param page | |
| * page | |
| * @param p | |
| * player | |
| */ | |
| private void pageBack(int page, Player p) { | |
| if (page != 0) { | |
| p.openInventory(inv.get(page - 1)); | |
| putValueInv(p, inv.get(page - 1)); | |
| putValueGui(p, this); | |
| return; | |
| } | |
| p.sendMessage("§cIl n'y a pas de page précédente !"); | |
| } | |
| public int getNbPage() | |
| { | |
| return nbPage; | |
| } | |
| public abstract void onClick(InventoryClickEvent e); | |
| @EventHandler | |
| public void onInventoryClick(InventoryClickEvent e) { | |
| if (!guiManager.containsKey(e.getWhoClicked())) | |
| return; | |
| Player p = (Player) e.getWhoClicked(); | |
| if (!e.getInventory().getTitle().equals(invManager.get(p).getTitle())) | |
| return; | |
| Inventory inventory = e.getInventory(); | |
| if (e.getCurrentItem() == null) | |
| return; | |
| ItemStack is = e.getCurrentItem(); | |
| GUIBuilder g = guiManager.get(p); | |
| int page = 0; | |
| if (g.nbPage > 0) { | |
| if (inventory.getItem(g.size - 5) != null) | |
| page = Integer.parseInt(inventory.getItem(g.size - 5).getItemMeta().getDisplayName().replace("§ePage : ", "")); | |
| } | |
| e.setCancelled(true); | |
| if (is != null && is.isSimilar(back())) { | |
| g.pageBack(page, p); | |
| } else if (is != null && is.isSimilar(next())) { | |
| g.pageNext(page, p); | |
| } | |
| else if (is != null && is.getType().equals(Material.ENDER_PEARL)&& is.getItemMeta().getDisplayName().contains("Page")) | |
| { | |
| e.setCancelled(true); | |
| } | |
| else { | |
| guiManager.get(p).onClick(e); | |
| } | |
| } | |
| public abstract void onCloseGui(InventoryCloseEvent e, List<Inventory> i); | |
| @EventHandler | |
| public void close(InventoryCloseEvent e) { | |
| if(guiManager.containsKey(e.getPlayer())) { | |
| GUIBuilder g = guiManager.get(e.getPlayer()); | |
| String titlet = g.title; | |
| Player pl = g.p; | |
| new BukkitRunnable(){ | |
| @Override | |
| public void run() | |
| { | |
| if (pl.getOpenInventory() == null || !pl.getOpenInventory().getTitle().equalsIgnoreCase(titlet)) { | |
| onCloseGui(e, g.inv); | |
| g.inv.clear(); | |
| g.p = null; | |
| g.nbPage = 0; | |
| } | |
| } | |
| }.runTaskLater(instance, 10); | |
| } | |
| Player p = (Player) e.getPlayer(); | |
| invManager.remove(p); | |
| guiManager.remove(p); | |
| } | |
| @EventHandler | |
| public void onDisconnect(PlayerQuitEvent e) { | |
| Player p = e.getPlayer(); | |
| invManager.remove(p); | |
| guiManager.remove(p); | |
| } | |
| } |
This file contains hidden or 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 fr.kwizzy.deathdash.util; | |
| /** | |
| * Par Alexis le 24/04/2016. | |
| */ | |
| import java.lang.reflect.InvocationTargetException; | |
| import java.lang.reflect.Method; | |
| import java.util.*; | |
| import org.apache.commons.lang.Validate; | |
| import org.bukkit.Bukkit; | |
| import org.bukkit.ChatColor; | |
| 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 org.bukkit.inventory.meta.SkullMeta; | |
| import org.bukkit.material.MaterialData; | |
| public class ItemBuilder { | |
| private ItemStack item; | |
| private ItemMeta meta; | |
| private Material material = Material.STONE; | |
| private int amount = 1; | |
| private MaterialData data; | |
| private short damage = 0; | |
| private Map<Enchantment, Integer> enchantments = new HashMap<>(); | |
| private String displayname; | |
| private List<String> lore = new ArrayList<>(); | |
| private List<ItemFlag> flags = new ArrayList<>(); | |
| private boolean andSymbol = true; | |
| /** | |
| * Initials the ItemBuilder with the Material | |
| * | |
| * @see Material | |
| */ | |
| public ItemBuilder(Material material) { | |
| Validate.notNull(material, "The Material is null."); | |
| this.item = new ItemStack(material); | |
| this.material = material; | |
| } | |
| /** | |
| * Initials the ItemBuilder with the Material and the amount | |
| * | |
| * @author Kev575 | |
| * @see Material | |
| */ | |
| public ItemBuilder(Material material, int amount) { | |
| Validate.notNull(material, "The Material is null."); | |
| this.item = new ItemStack(material, amount); | |
| this.material = material; | |
| this.amount = amount; | |
| } | |
| /** | |
| * Initials the ItemBuilder with the Material, the amount and the displaname | |
| * | |
| * @author Kev575 | |
| * @see Material | |
| */ | |
| public ItemBuilder(Material material, int amount, String displayname) { | |
| Validate.notNull(material, "The Material is null."); | |
| Validate.notNull(displayname, "The Displayname is null."); | |
| this.item = new ItemStack(material, amount); | |
| this.material = material; | |
| this.amount = amount; | |
| this.displayname = displayname; | |
| } | |
| /** | |
| * Initials the ItemBuilder with the Material and the displaname | |
| * | |
| * @see Material | |
| */ | |
| public ItemBuilder(Material material, String displayname) { | |
| Validate.notNull(material, "The Material is null."); | |
| Validate.notNull(displayname, "The Displayname is null."); | |
| this.item = new ItemStack(material); | |
| this.material = material; | |
| this.displayname = displayname; | |
| } | |
| /** | |
| * Initials the ItemBuilder with an already existing ItemStack | |
| * | |
| * @see ItemStack | |
| */ | |
| public ItemBuilder(ItemStack item) { | |
| Validate.notNull(item, "The Item is null."); | |
| this.item = item; | |
| this.meta = item.getItemMeta(); | |
| this.material = item.getType(); | |
| this.amount = item.getAmount(); | |
| this.data = item.getData(); | |
| this.damage = item.getDurability(); | |
| this.enchantments = item.getEnchantments(); | |
| this.displayname = item.getItemMeta().getDisplayName(); | |
| this.lore = item.getItemMeta().getLore(); | |
| for (ItemFlag f : item.getItemMeta().getItemFlags()) { | |
| flags.add(f); | |
| } | |
| } | |
| /** | |
| * Initials the ItemBuilder with an already existing instance of the | |
| * ItemBuilder | |
| * | |
| * @see ItemBuilder | |
| */ | |
| public ItemBuilder(ItemBuilder builder) { | |
| Validate.notNull(builder, "The ItemBuilder is null."); | |
| this.item = builder.item; | |
| this.meta = builder.meta; | |
| this.material = builder.material; | |
| this.amount = builder.amount; | |
| this.damage = builder.damage; | |
| this.data = builder.data; | |
| this.damage = builder.damage; | |
| this.enchantments = builder.enchantments; | |
| this.displayname = builder.displayname; | |
| this.lore = builder.lore; | |
| this.flags = builder.flags; | |
| } | |
| /** | |
| * Sets the amount of the builded ItemStack | |
| * | |
| * @param amount | |
| * (Integer) | |
| */ | |
| public ItemBuilder amount(int amount) { | |
| this.amount = amount; | |
| return this; | |
| } | |
| /** | |
| * Sets the MaterialData of the builded ItemStack | |
| * | |
| * @param data | |
| * (MaterialData) | |
| */ | |
| public ItemBuilder data(MaterialData data) { | |
| Validate.notNull(data, "The Data is null."); | |
| this.data = data; | |
| return this; | |
| } | |
| /** | |
| * Sets the damage (durability) of the builded ItemStack | |
| * | |
| * @param damage | |
| * (Short) | |
| * @deprecated Use ItemBuilder#durability | |
| */ | |
| @Deprecated | |
| public ItemBuilder damage(short damage) { | |
| this.damage = damage; | |
| return this; | |
| } | |
| /** | |
| * Sets the durability (damage) of the builded ItemStack | |
| * | |
| * @param damage | |
| * (Short) | |
| */ | |
| public ItemBuilder durability(short damage) { | |
| this.damage = damage; | |
| return this; | |
| } | |
| /** | |
| * Sets the Material of the builded ItemStack | |
| * | |
| * @param material | |
| * (Material) | |
| */ | |
| public ItemBuilder material(Material material) { | |
| Validate.notNull(material, "The Mater is null."); | |
| this.material = material; | |
| return this; | |
| } | |
| /** | |
| * Sets the ItemMeta of the builded ItemStack | |
| * | |
| * @param meta | |
| * (ItemMeta) | |
| */ | |
| public ItemBuilder meta(ItemMeta meta) { | |
| Validate.notNull(meta, "The Meta is null."); | |
| this.meta = meta; | |
| return this; | |
| } | |
| /** | |
| * Adds the Enchantment of the builded ItemStack | |
| * | |
| * @param enchant | |
| * (Enchantment) | |
| */ | |
| public ItemBuilder enchant(Enchantment enchant, int level) { | |
| Validate.notNull(enchant, "The Enchantment is null."); | |
| enchantments.put(enchant, level); | |
| return this; | |
| } | |
| /** | |
| * Sets the Enchantments of the builded ItemStack | |
| * | |
| * @param enchantments | |
| * (Map<Enchantment, Integer> ) | |
| */ | |
| public ItemBuilder enchant(Map<Enchantment, Integer> enchantments) { | |
| Validate.notNull(enchantments, "The Enchantments is null."); | |
| this.enchantments = enchantments; | |
| return this; | |
| } | |
| /** | |
| * Sets the Displayname of the builded ItemStack | |
| * | |
| * @param displayname | |
| * (Displayname) | |
| */ | |
| public ItemBuilder displayname(String displayname) { | |
| Validate.notNull(displayname, "The Displayname is null."); | |
| if (andSymbol) | |
| this.displayname = ChatColor.translateAlternateColorCodes('&', displayname); | |
| else | |
| this.displayname = displayname; | |
| return this; | |
| } | |
| /** | |
| * Adds the line to the Lore of the builded ItemStack | |
| * | |
| * @param line | |
| * (String) | |
| */ | |
| public ItemBuilder lore(String line) { | |
| Validate.notNull(line, "The Line is null."); | |
| if (andSymbol) | |
| lore.add(ChatColor.translateAlternateColorCodes('&', line)); | |
| else | |
| lore.add(line); | |
| return this; | |
| } | |
| /** | |
| * Sets the lore of the builded ItemStack | |
| * | |
| * @param lore | |
| * (List<String>) | |
| */ | |
| public ItemBuilder lore(List<String> lore) { | |
| Validate.notNull(lore, "The Lore is null."); | |
| this.lore = lore; | |
| return this; | |
| } | |
| /** | |
| * Adds the lines to the Lore of the builded ItemStack | |
| * | |
| * @param lines | |
| * (String...) | |
| * @author Kev575 | |
| */ | |
| public ItemBuilder lores(String... lines) { | |
| Validate.notNull(lines, "The Lines are null."); | |
| for (String line : lines) { | |
| if (andSymbol) { | |
| lore.add(ChatColor.translateAlternateColorCodes('&', line)); | |
| } else { | |
| lore.add(line); | |
| } | |
| } | |
| lore.addAll(Arrays.asList(lines)); | |
| return this; | |
| } | |
| /** | |
| * Adds the line as the counts position as lore | |
| * | |
| * @param line | |
| * (String) | |
| * @param count | |
| * (Integer) | |
| */ | |
| public ItemBuilder lore(String line, int count) { | |
| Validate.notNull(line, "The Line is null."); | |
| if (andSymbol) | |
| lore.set(count, ChatColor.translateAlternateColorCodes('&', line)); | |
| else | |
| lore.set(count, line); | |
| return this; | |
| } | |
| /** | |
| * Adds the ItemFlag to the builded ItemStack | |
| * | |
| * @param flag | |
| * (ItemFlag) | |
| */ | |
| public ItemBuilder flag(ItemFlag flag) { | |
| Validate.notNull(flag, "The Flag is null."); | |
| flags.add(flag); | |
| return this; | |
| } | |
| /** | |
| * Sets the ItemFlags of the builded ItemStack | |
| * | |
| * @param flags | |
| * (List<ItemFlag>) | |
| */ | |
| public ItemBuilder flag(List<ItemFlag> flags) { | |
| Validate.notNull(flags, "The Flags are null."); | |
| this.flags = flags; | |
| return this; | |
| } | |
| /** | |
| * Sets the ItemFlags of the builded ItemStack | |
| * | |
| * @param flags | |
| * (List<ItemFlag>) | |
| */ | |
| public ItemBuilder flag(ItemFlag... flags) { | |
| Validate.notNull(flags, "The Flags are null."); | |
| for (ItemFlag flag : flags) { | |
| this.flags.add(flag); | |
| } | |
| return this; | |
| } | |
| /** | |
| * Makes the Item (un-)breakable | |
| * | |
| * @param unbreakable | |
| * (Boolean) | |
| */ | |
| public ItemBuilder unbreakable(boolean unbreakable) { | |
| meta.spigot().setUnbreakable(unbreakable); | |
| return this; | |
| } | |
| /** | |
| * Makes the Item Glow | |
| */ | |
| public ItemBuilder glow() { | |
| enchant(material != Material.BOW ? Enchantment.ARROW_INFINITE : Enchantment.LUCK, 10); | |
| flag(ItemFlag.HIDE_ENCHANTS); | |
| return this; | |
| } | |
| /** | |
| * Sets the Owner of the Skull This only affects if the Item is a SKULL_ITEM | |
| * or a SKULL | |
| * | |
| * @param user | |
| * (String) | |
| */ | |
| public ItemBuilder owner(String user) { | |
| Validate.notNull(user, "The Username is null."); | |
| if ((material == Material.SKULL_ITEM) || (material == Material.SKULL)) { | |
| SkullMeta smeta = (SkullMeta) meta; | |
| smeta.setOwner(user); | |
| meta = smeta; | |
| } | |
| return this; | |
| } | |
| /** | |
| * Method to access all NBT Methods | |
| */ | |
| public Unsafe unsafe() { | |
| return new Unsafe(this); | |
| } | |
| /** | |
| * Toggles the replace of the ampersand (&) symbol to the section sign (§) | |
| * symbol | |
| * | |
| * @author Kev575 | |
| */ | |
| public ItemBuilder replaceAndSymbol() { | |
| replaceAndSymbol(!andSymbol); | |
| return this; | |
| } | |
| /** | |
| * Enables / disables the replace of the ampersand (&) symbol to the section | |
| * sign (§) symbol | |
| * | |
| * @author Kev575 | |
| */ | |
| public ItemBuilder replaceAndSymbol(boolean replace) { | |
| andSymbol = replace; | |
| return this; | |
| } | |
| /** | |
| * Builds the ItemStack and returns it | |
| * | |
| * @return (ItemStack) | |
| */ | |
| public ItemStack build() { | |
| item.setType(material); | |
| item.setAmount(amount); | |
| item.setDurability(damage); | |
| meta = item.getItemMeta(); | |
| if (data != null) { | |
| item.setData(data); | |
| } | |
| if (enchantments.size() > 0) { | |
| item.addUnsafeEnchantments(enchantments); | |
| } | |
| if (displayname != null) { | |
| meta.setDisplayName(displayname); | |
| } | |
| if (lore.size() > 0) { | |
| meta.setLore(lore); | |
| } | |
| if (flags.size() > 0) { | |
| for (ItemFlag f : flags) { | |
| meta.addItemFlags(f); | |
| } | |
| } | |
| item.setItemMeta(meta); | |
| return item; | |
| } | |
| /** | |
| * This Methods are unsafe and should not be used when you don't know what | |
| * they do. These methods can cause a corruption of the Players Inventory | |
| * File when it contains a invalid Item and causes so that the PLayer can no | |
| * longer join the World with the corrupted Inventory File. | |
| * | |
| * @author Acquized | |
| * @since 1.5 | |
| */ | |
| public class Unsafe { | |
| /** | |
| * Don't access the ReflectionUtils Class using this field. Using | |
| * methods from ReflectionUtils Class is not suggested and may cause a | |
| * major plugin break. | |
| **/ | |
| protected final ReflectionUtils utils = new ReflectionUtils(); | |
| protected final ItemBuilder builder; | |
| /** | |
| * Initials the Unsafe Class with the ItemBuilder | |
| * | |
| * @param builder | |
| * (ItemBuilder) | |
| */ | |
| public Unsafe(ItemBuilder builder) { | |
| this.builder = builder; | |
| } | |
| /** | |
| * Sets a String in the Item NBT Compound | |
| */ | |
| public void setString(String key, String value) { | |
| builder.item = utils.setString(builder.item, key, value); | |
| } | |
| /** | |
| * Gets a String from the Item NBT Compound | |
| */ | |
| public String getString(String key) { | |
| return utils.getString(builder.item, key); | |
| } | |
| /** | |
| * Sets a Integer in the Item NBT Compound | |
| */ | |
| public void setInt(String key, int value) { | |
| builder.item = utils.setInt(builder.item, key, value); | |
| } | |
| /** | |
| * Gets a Integer from the Item NBT Compound | |
| */ | |
| public int getInt(String key) { | |
| return utils.getInt(builder.item, key); | |
| } | |
| /** | |
| * Sets a Double in the Item NBT Compound | |
| */ | |
| public void setDouble(String key, double value) { | |
| builder.item = utils.setDouble(builder.item, key, value); | |
| } | |
| /** | |
| * Gets a Double from the Item NBT Compound | |
| */ | |
| public double getDouble(String key) { | |
| return utils.getDouble(builder.item, key); | |
| } | |
| /** | |
| * Sets a Boolean in the Item NBT Compound | |
| */ | |
| public void setBoolean(String key, boolean value) { | |
| builder.item = utils.setBoolean(builder.item, key, value); | |
| } | |
| /** | |
| * Gets a Boolean from the Item NBT Compound | |
| */ | |
| public boolean getBoolean(String key) { | |
| return utils.getBoolean(builder.item, key); | |
| } | |
| /** | |
| * Checks if the Item NBT Compound contains the Key | |
| */ | |
| public boolean containsKey(String key) { | |
| return utils.hasKey(builder.item, key); | |
| } | |
| /** | |
| * Accesses back the ItemBuilder Class and exits the Unsafe Class | |
| * | |
| * @return ItemBuilder | |
| */ | |
| public ItemBuilder builder() { | |
| return builder; | |
| } | |
| public class ReflectionUtils { | |
| public String getString(ItemStack item, String key) { | |
| Object compound = getNBTTagCompound(getItemAsNMSStack(item)); | |
| if (compound == null) { | |
| compound = getNewNBTTagCompound(); | |
| } | |
| try { | |
| return (String) compound.getClass().getMethod("getString", String.class).invoke(compound, key); | |
| } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { | |
| ex.printStackTrace(); | |
| } | |
| return null; | |
| } | |
| public ItemStack setString(ItemStack item, String key, String value) { | |
| Object nmsItem = getItemAsNMSStack(item); | |
| Object compound = getNBTTagCompound(nmsItem); | |
| if (compound == null) { | |
| compound = getNewNBTTagCompound(); | |
| } | |
| try { | |
| compound.getClass().getMethod("setString", String.class, String.class).invoke(compound, key, value); | |
| nmsItem = setNBTTag(compound, nmsItem); | |
| } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { | |
| ex.printStackTrace(); | |
| } | |
| return getItemAsBukkitStack(nmsItem); | |
| } | |
| public int getInt(ItemStack item, String key) { | |
| Object compound = getNBTTagCompound(getItemAsNMSStack(item)); | |
| if (compound == null) { | |
| compound = getNewNBTTagCompound(); | |
| } | |
| try { | |
| return (Integer) compound.getClass().getMethod("getInt", String.class).invoke(compound, key); | |
| } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { | |
| ex.printStackTrace(); | |
| } | |
| return -1; | |
| } | |
| public ItemStack setInt(ItemStack item, String key, int value) { | |
| Object nmsItem = getItemAsNMSStack(item); | |
| Object compound = getNBTTagCompound(nmsItem); | |
| if (compound == null) { | |
| compound = getNewNBTTagCompound(); | |
| } | |
| try { | |
| compound.getClass().getMethod("setInt", String.class, Integer.class).invoke(compound, key, value); | |
| nmsItem = setNBTTag(compound, nmsItem); | |
| } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { | |
| ex.printStackTrace(); | |
| } | |
| return getItemAsBukkitStack(nmsItem); | |
| } | |
| public double getDouble(ItemStack item, String key) { | |
| Object compound = getNBTTagCompound(getItemAsNMSStack(item)); | |
| if (compound == null) { | |
| compound = getNewNBTTagCompound(); | |
| } | |
| try { | |
| return (Double) compound.getClass().getMethod("getDouble", String.class).invoke(compound, key); | |
| } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { | |
| ex.printStackTrace(); | |
| } | |
| return Double.NaN; | |
| } | |
| public ItemStack setDouble(ItemStack item, String key, double value) { | |
| Object nmsItem = getItemAsNMSStack(item); | |
| Object compound = getNBTTagCompound(nmsItem); | |
| if (compound == null) { | |
| compound = getNewNBTTagCompound(); | |
| } | |
| try { | |
| compound.getClass().getMethod("setDouble", String.class, Double.class).invoke(compound, key, value); | |
| nmsItem = setNBTTag(compound, nmsItem); | |
| } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { | |
| ex.printStackTrace(); | |
| } | |
| return getItemAsBukkitStack(nmsItem); | |
| } | |
| public boolean getBoolean(ItemStack item, String key) { | |
| Object compound = getNBTTagCompound(getItemAsNMSStack(item)); | |
| if (compound == null) { | |
| compound = getNewNBTTagCompound(); | |
| } | |
| try { | |
| return (Boolean) compound.getClass().getMethod("getBoolean", String.class).invoke(compound, key); | |
| } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { | |
| ex.printStackTrace(); | |
| } | |
| return false; | |
| } | |
| public ItemStack setBoolean(ItemStack item, String key, boolean value) { | |
| Object nmsItem = getItemAsNMSStack(item); | |
| Object compound = getNBTTagCompound(nmsItem); | |
| if (compound == null) { | |
| compound = getNewNBTTagCompound(); | |
| } | |
| try { | |
| compound.getClass().getMethod("setBoolean", String.class, Boolean.class).invoke(compound, key, | |
| value); | |
| nmsItem = setNBTTag(compound, nmsItem); | |
| } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { | |
| ex.printStackTrace(); | |
| } | |
| return getItemAsBukkitStack(nmsItem); | |
| } | |
| public boolean hasKey(ItemStack item, String key) { | |
| Object compound = getNBTTagCompound(getItemAsNMSStack(item)); | |
| if (compound == null) { | |
| compound = getNewNBTTagCompound(); | |
| } | |
| try { | |
| return (Boolean) compound.getClass().getMethod("hasKey", String.class).invoke(compound, key); | |
| } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { | |
| e.printStackTrace(); | |
| } | |
| return false; | |
| } | |
| /** <!-- UNSAFE METHODS (DO NOT USE!) !--> **/ | |
| public Object getNewNBTTagCompound() { | |
| String ver = Bukkit.getServer().getClass().getPackage().getName().split(".")[3]; | |
| try { | |
| return Class.forName("net.minecraft.server." + ver + ".NBTTagCompound").newInstance(); | |
| } catch (ClassNotFoundException | IllegalAccessException | InstantiationException ex) { | |
| ex.printStackTrace(); | |
| } | |
| return null; | |
| } | |
| public Object setNBTTag(Object tag, Object item) { | |
| try { | |
| item.getClass().getMethod("setTag", item.getClass()).invoke(item, tag); | |
| return item; | |
| } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { | |
| ex.printStackTrace(); | |
| } | |
| return null; | |
| } | |
| public Object getNBTTagCompound(Object nmsStack) { | |
| try { | |
| return nmsStack.getClass().getMethod("getTag").invoke(nmsStack); | |
| } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { | |
| ex.printStackTrace(); | |
| } | |
| return null; | |
| } | |
| @SuppressWarnings("unchecked") | |
| public Object getItemAsNMSStack(ItemStack item) { | |
| try { | |
| Method m = getCraftItemStackClass().getMethod("asNMSCopy", ItemStack.class); | |
| return m.invoke(getCraftItemStackClass(), item); | |
| } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) { | |
| ex.printStackTrace(); | |
| } | |
| return null; | |
| } | |
| @SuppressWarnings("unchecked") | |
| public ItemStack getItemAsBukkitStack(Object nmsStack) { | |
| try { | |
| Method m = getCraftItemStackClass().getMethod("asCraftMirror", nmsStack.getClass()); | |
| return (ItemStack) m.invoke(getCraftItemStackClass(), nmsStack); | |
| } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) { | |
| ex.printStackTrace(); | |
| } | |
| return null; | |
| } | |
| public Class getCraftItemStackClass() { | |
| String ver = Bukkit.getServer().getClass().getPackage().getName().split(".")[3]; | |
| try { | |
| return Class.forName("org.bukkit.craftbukkit." + ver + ".inventory.CraftItemStack"); | |
| } catch (ClassNotFoundException ex) { | |
| ex.printStackTrace(); | |
| } | |
| return null; | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or 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 fr.kwizzy.deathdash.util; | |
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.Iterator; | |
| import java.util.List; | |
| /** | |
| * Par Alexis le 15/08/2016. | |
| */ | |
| public class ParallelList<T> implements Iterable<List<T>> { | |
| private final List<List<T>> lists; | |
| public ParallelList(List<T>... lists) { | |
| this.lists = new ArrayList<List<T>>(lists.length); | |
| this.lists.addAll(Arrays.asList(lists)); | |
| } | |
| public Iterator<List<T>> iterator() { | |
| return new Iterator<List<T>>() { | |
| private int loc = 0; | |
| public boolean hasNext() { | |
| boolean hasNext = false; | |
| for (List<T> list : lists) { | |
| hasNext |= (loc < list.size()); | |
| } | |
| return hasNext; | |
| } | |
| public List<T> next() { | |
| List<T> vals = new ArrayList<T>(lists.size()); | |
| for (int i=0; i<lists.size(); i++) { | |
| vals.add(loc < lists.get(i).size() ? lists.get(i).get(loc) : null); | |
| } | |
| loc++; | |
| return vals; | |
| } | |
| public void remove() { | |
| for (List<T> list : lists) { | |
| if (loc < list.size()) { | |
| list.remove(loc); | |
| } | |
| } | |
| } | |
| }; | |
| } | |
| } |
This file contains hidden or 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 fr.kwizzy.deathdash.util; | |
| import java.lang.reflect.Field; | |
| import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer; | |
| import org.bukkit.entity.Player; | |
| import net.minecraft.server.v1_8_R3.*; | |
| public class ScoreboardSign { | |
| private boolean created = false; | |
| private final String[] lines = new String[16]; | |
| private final Player player; | |
| private String objectiveName; | |
| public ScoreboardSign(Player player, String objectiveName) { | |
| this.player = player; | |
| this.objectiveName = objectiveName; | |
| } | |
| public void create() { | |
| if (created) | |
| return; | |
| PlayerConnection player = getPlayer(); | |
| player.sendPacket(createObjectivePacket(0, objectiveName)); | |
| player.sendPacket(setObjectiveSlot()); | |
| int i = 0; | |
| while (i < lines.length) | |
| sendLine(i++); | |
| created = true; | |
| } | |
| public void destroy() { | |
| if (!created) | |
| return; | |
| getPlayer().sendPacket(createObjectivePacket(1, null)); | |
| created = false; | |
| } | |
| private PlayerConnection getPlayer() { | |
| return ((CraftPlayer) player).getHandle().playerConnection; | |
| } | |
| private void sendLine(int line) { | |
| if (line > 15) | |
| return; | |
| if (line < 0) | |
| return; | |
| if (!created) | |
| return; | |
| int score = (line * -1) - 1; | |
| String val = lines[line]; | |
| getPlayer().sendPacket(sendScore(val, score)); | |
| } | |
| public void setObjectiveName(String name) { | |
| this.objectiveName = name; | |
| if (created) | |
| getPlayer().sendPacket(createObjectivePacket(2, name)); | |
| } | |
| public void setLine(int line, String value) { | |
| String oldLine = getLine(line); | |
| if (oldLine != null && created) | |
| getPlayer().sendPacket(removeLine(oldLine)); | |
| lines[line] = value; | |
| sendLine(line); | |
| } | |
| public void removeLine(int line) { | |
| String oldLine = getLine(line); | |
| if (oldLine != null && created) | |
| getPlayer().sendPacket(removeLine(oldLine)); | |
| lines[line] = null; | |
| } | |
| public String getLine(int line) { | |
| if (line > 15) | |
| return null; | |
| if (line < 0) | |
| return null; | |
| return lines[line]; | |
| } | |
| /* | |
| * Factories | |
| */ | |
| private PacketPlayOutScoreboardObjective createObjectivePacket(int mode, String displayName) { | |
| PacketPlayOutScoreboardObjective packet = new PacketPlayOutScoreboardObjective(); | |
| try { | |
| // Nom de l'objectif | |
| Field name = packet.getClass().getDeclaredField("a"); | |
| name.setAccessible(true); | |
| name.set(packet, player.getName()); | |
| // Mode | |
| // 0 : créer | |
| // 1 : Supprimer | |
| // 2 : Mettre à jour | |
| Field modeField = packet.getClass().getDeclaredField("d"); | |
| modeField.setAccessible(true); | |
| modeField.set(packet, mode); | |
| if (mode == 0 || mode == 2) { | |
| Field displayNameField = packet.getClass().getDeclaredField("b"); | |
| displayNameField.setAccessible(true); | |
| displayNameField.set(packet, displayName); | |
| Field display = packet.getClass().getDeclaredField("c"); | |
| display.setAccessible(true); | |
| display.set(packet, IScoreboardCriteria.EnumScoreboardHealthDisplay.INTEGER); | |
| } | |
| } catch (NoSuchFieldException | IllegalAccessException e) { | |
| e.printStackTrace(); | |
| } | |
| return packet; | |
| } | |
| private PacketPlayOutScoreboardDisplayObjective setObjectiveSlot() { | |
| PacketPlayOutScoreboardDisplayObjective packet = new PacketPlayOutScoreboardDisplayObjective(); | |
| try { | |
| // Slot de l'objectif | |
| Field position = packet.getClass().getDeclaredField("a"); | |
| position.setAccessible(true); | |
| position.set(packet, 1); // SideBar | |
| Field name = packet.getClass().getDeclaredField("b"); | |
| name.setAccessible(true); | |
| name.set(packet, player.getName()); | |
| } catch (NoSuchFieldException | IllegalAccessException e) { | |
| e.printStackTrace(); | |
| } | |
| return packet; | |
| } | |
| private PacketPlayOutScoreboardScore sendScore(String line, int score) { | |
| PacketPlayOutScoreboardScore packet = new PacketPlayOutScoreboardScore(line); | |
| try { | |
| Field name = packet.getClass().getDeclaredField("b"); | |
| name.setAccessible(true); | |
| name.set(packet, player.getName()); | |
| Field scoreField = packet.getClass().getDeclaredField("c"); | |
| scoreField.setAccessible(true); | |
| scoreField.set(packet, score); // SideBar | |
| Field action = packet.getClass().getDeclaredField("d"); | |
| action.setAccessible(true); | |
| action.set(packet, PacketPlayOutScoreboardScore.EnumScoreboardAction.CHANGE); | |
| } catch (NoSuchFieldException | IllegalAccessException e) { | |
| e.printStackTrace(); | |
| } | |
| return packet; | |
| } | |
| private PacketPlayOutScoreboardScore removeLine(String line) { | |
| return new PacketPlayOutScoreboardScore(line); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment