Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.Coolboy.BetterTeleport;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- import net.md_5.bungee.api.ChatColor;
- import net.md_5.bungee.api.chat.ClickEvent;
- import net.md_5.bungee.api.chat.ComponentBuilder;
- import net.md_5.bungee.api.chat.HoverEvent;
- import net.md_5.bungee.api.chat.TextComponent;
- import org.bukkit.Bukkit;
- import org.bukkit.Effect;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.SkullType;
- import org.bukkit.entity.Entity;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.meta.ItemMeta;
- import org.bukkit.inventory.meta.SkullMeta;
- import org.bukkit.metadata.FixedMetadataValue;
- import org.bukkit.scheduler.BukkitRunnable;
- public class Util {
- public static Location safeLocation(Entity dest) {
- Location tpLoc = dest.getLocation();
- while (!tpLoc.getBlock().getType().isSolid()) {
- tpLoc.setY(tpLoc.getBlockY());
- tpLoc = tpLoc.subtract(0, 1, 0);
- if (tpLoc.getBlockY() == 0) {
- return null;
- }
- if (tpLoc.getBlock().getType().equals(Material.LAVA)
- || tpLoc.getBlock().getType().equals(Material.STATIONARY_LAVA)
- || tpLoc.getBlock().getType().equals(Material.MAGMA)
- || tpLoc.getBlock().getType().equals(Material.CACTUS)) {
- return null;
- }
- if (tpLoc.getBlock().isLiquid()) {
- break;
- }
- }
- tpLoc.add(0, 1, 0);
- return tpLoc;
- }
- //get entry with player
- public static Map.Entry<String, Long> getPlayer(LinkedHashMap<String, Long> map, String name) {
- Map.Entry<String, Long> player = null;
- for (Map.Entry<String, Long> entry : map.entrySet()) {
- if (entry.getKey().equals(name)) {
- player = entry;
- break;
- }
- }
- return player;
- }
- public static LinkedHashMap<String, Long> removePlayer(LinkedHashMap<String, Long> map, String name) {
- for (Map.Entry<String, Long> entry : map.entrySet()) {
- if (entry.getKey().equals(name)) {
- map.remove(entry.getKey());
- break;
- }
- }
- return map;
- }
- public static void tryTp(Map.Entry<String, Long> player, Player tpTo) {
- long timestamp = player.getValue();
- long diff = System.currentTimeMillis() - timestamp;
- //timeout for teleport
- if (diff / 1000 > 90) {
- sendMessage(tpTo, "&4Час очікування вичерпано");
- } else if (Bukkit.getPlayer(player.getKey()) != null) {
- Player tpFrom = Bukkit.getServer().getPlayer(player.getKey());
- sendMessage(tpFrom, tpTo.getDisplayName() + "&6 прийняв ваш запит на телепортацію");
- sendMessage(tpFrom, "&6Телепортування до гравця &f" + tpTo.getDisplayName());
- Location loc = safeLocation(tpTo);
- if (loc != null) {
- tpFrom.teleport(loc);
- loc.getWorld().playEffect(loc, Effect.ENDER_SIGNAL, 0);
- } else {
- sendMessage(tpFrom, "&4Небезпечне телепортування!");
- sendMessage(tpTo, "&4Небезпечне телепортування!");
- }
- } else {
- sendMessage(tpTo, "&4Гравця не знайдено!");
- }
- }
- public static void sendMessage(Player player, String msg) {
- msg = ChatColor.translateAlternateColorCodes('&', msg);
- if (player.isOnline()) {
- player.sendMessage(msg);
- }
- }
- public static ItemStack setLore(ItemStack stack, String lore) {
- lore = ChatColor.translateAlternateColorCodes('&', lore);
- ItemMeta meta = stack.getItemMeta();
- meta.setLore(Arrays.asList(lore));
- stack.setItemMeta(meta);
- return stack;
- }
- public static ItemStack setLore(ItemStack stack, String... lore) {
- List<String> loreList = new ArrayList<>();
- for (String str : lore) {
- str = ChatColor.translateAlternateColorCodes('&', str);
- loreList.add(str);
- }
- ItemMeta meta = stack.getItemMeta();
- meta.setLore(loreList);
- stack.setItemMeta(meta);
- return stack;
- }
- public static ItemStack setName(ItemStack stack, String name) {
- name = ChatColor.translateAlternateColorCodes('&', name);
- ItemMeta meta = stack.getItemMeta();
- meta.setDisplayName(name);
- stack.setItemMeta(meta);
- return stack;
- }
- public static ItemStack setItemMeta(ItemStack stack, String name, String lore) {
- name = ChatColor.translateAlternateColorCodes('&', name);
- lore = ChatColor.translateAlternateColorCodes('&', lore);
- ItemMeta meta = stack.getItemMeta();
- meta.setDisplayName(name);
- meta.setLore(Arrays.asList(lore));
- stack.setItemMeta(meta);
- return stack;
- }
- public static Inventory addButtons(Inventory inv, int page) {
- int size = inv.getSize();
- int o = Bukkit.getServer().getOnlinePlayers().size();
- ItemStack info = new ItemStack(Material.SIGN, 1);
- setName(info, "&5Інформація");
- setLore(info,
- Util.encodeHiddenLore(String.valueOf(page)),
- "&6Список усіх гравців серверу",
- "&6Щоб телепортуватись, натисніть по голові гравця"
- );
- ItemStack next = new ItemStack(Material.PAPER, 1);
- setName(next, "&5Наступна сторінка");
- ItemStack previous = new ItemStack(Material.PAPER, 1);
- setName(previous, "&5Попередня сторінка");
- ItemStack empty = new ItemStack(Material.BARRIER, 1);
- setName(empty, "&5Пусто");
- if (o == 1) {
- inv.setItem(4, empty);
- }
- inv.setItem(size - 5, info);
- if (o > 45) {
- inv.setItem(size - 9, previous);
- inv.setItem(size - 1, next);
- }
- return inv;
- }
- public static List<ItemStack> getHeads(Player player) {
- List<ItemStack> heads = new ArrayList<>();
- for (Player pl : Bukkit.getOnlinePlayers()) {
- if (pl.equals(player)) {
- continue;
- }
- ItemStack head = new ItemStack(Material.SKULL_ITEM, 1, (short) SkullType.PLAYER.ordinal());
- SkullMeta meta = (SkullMeta) head.getItemMeta();
- meta.setOwner(pl.getName());
- head.setItemMeta(meta);
- String world = pl.getLocation().getWorld().getName();
- head = setItemMeta(head, pl.getDisplayName(), "&8Світ: " + getWorldColor(world) + world);
- heads.add(head);
- }
- return heads;
- }
- public static ChatColor getWorldColor(String world) {
- if (world.contains("nether")) {
- return ChatColor.DARK_RED;
- }
- if (world.contains("end")) {
- return ChatColor.BLUE;
- }
- if (world.contains("space")) {
- return ChatColor.GRAY;
- }
- return ChatColor.DARK_GREEN;
- }
- public static ItemStack[] getPage(int i, Player player) {
- List<ItemStack> heads = getHeads(player);
- if (heads.isEmpty()) {
- return null;
- }
- List<ItemStack> page;
- double pages = Math.floor(heads.size() / 45) + 1;
- if (i > pages) {
- throw new IndexOutOfBoundsException();
- }
- if (pages != 1) {
- if (i != pages) {
- page = heads.subList(i * 45, i * 45 + 45);
- } else {
- page = heads.subList(i * 45, heads.size());
- }
- return page.toArray(new ItemStack[page.size()]);
- }
- return heads.toArray(new ItemStack[heads.size()]);
- }
- public static Inventory makeInventory(ItemStack[] stacks, Player player, String title) {
- int o = Bukkit.getServer().getOnlinePlayers().size();
- int size = 18;
- if (o > 45) {
- size = 54;
- } else {
- for (int i = 0; true; i++) {
- if ((o + i) % 9 == 0) {
- size = o + i + 9;
- break;
- }
- if (i > 10) {
- break;
- }
- }
- }
- Inventory inv = Bukkit.createInventory(null, size, title);
- if (stacks != null) {
- inv.setContents(getPage(0, player));
- }
- addButtons(inv, 0);
- return inv;
- }
- public static String setColor(String msg) {
- msg = ChatColor.translateAlternateColorCodes('&', msg);
- return msg;
- }
- public static int getPageNumber(Inventory inv) {
- ItemStack info = inv.getItem(inv.getSize() - 5);
- String lore = info.getItemMeta().getLore().get(0);
- return Integer.decode(Util.decodeHiddenLore(lore));
- }
- public static LinkedHashMap<String, Long> readMetadata(Player player) {
- LinkedHashMap<String, Long> requests = new LinkedHashMap<>();
- if (player.hasMetadata("requests")) {
- requests = (LinkedHashMap<String, Long>) player.getMetadata("requests").get(0).value();
- }
- return requests;
- }
- public static void call(Player tpTo, Player tpFrom) {
- //Message creating
- if (tpFrom.getName().equalsIgnoreCase(tpTo.getName())) {
- Util.sendMessage(tpFrom, "&4Неможливо телепортуватись до самого себе");
- return;
- }
- LinkedHashMap<String, Long> requests = readMetadata(tpTo);
- Util.sendMessage(tpFrom, "&6Запит надіслано гравцю &f" + tpTo.getDisplayName());
- TextComponent message = new TextComponent(tpFrom.getDisplayName());
- TextComponent msg1 = new TextComponent(" просить дозволу телепортуватись до вас \n");
- msg1.setColor(ChatColor.GOLD);
- message.addExtra(msg1);
- TextComponent msg2 = new TextComponent("Щоб телепортуватись, використайте ");
- msg2.setColor(ChatColor.GOLD);
- message.addExtra(msg2);
- TextComponent accept = new TextComponent("/tpaccept \n");
- accept.setColor(ChatColor.RED);
- accept.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Натисніть, щоб прийняти").create()));
- accept.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/tpaccept"));
- message.addExtra(accept);
- TextComponent msg3 = new TextComponent("Щоб відхилити, використайте ");
- msg3.setColor(ChatColor.GOLD);
- message.addExtra(msg3);
- TextComponent decline = new TextComponent("/tpdeny \n");
- decline.setColor(ChatColor.RED);
- decline.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Натисніть, щоб відмовити").create()));
- decline.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/tpdeny"));
- message.addExtra(decline);
- TextComponent msg4 = new TextComponent("Заявка буде автоматично скасована через ");
- msg4.setColor(ChatColor.GOLD);
- message.addExtra(msg4);
- TextComponent msg5 = new TextComponent("90 секунд");
- msg5.setColor(ChatColor.RED);
- message.addExtra(msg5);
- tpTo.spigot().sendMessage(message);
- //saving hashmap
- if (requests.isEmpty() || !requests.containsKey(tpFrom.getName())) {
- requests.put(tpFrom.getName(), System.currentTimeMillis());
- } else {
- requests.replace(tpFrom.getName(), System.currentTimeMillis());
- }
- tpTo.setMetadata("requests", new FixedMetadataValue(BetterTeleport.getInstance(), requests));
- new BukkitRunnable() {
- @Override
- public void run() {
- LinkedHashMap<String, Long> requests = Util.readMetadata(tpTo);
- if(requests.containsKey(tpFrom.getName())) {
- tpFrom.sendMessage(Util.setColor(tpTo.getDisplayName() + " &4не підтвердив запит"));
- requests.remove(tpFrom.getName());
- }
- }
- }.runTaskLater(BetterTeleport.getInstance(), 1800);
- }
- //inserts a formatting character between every character, making it invisible when displayed as lore
- public static String encodeHiddenLore(String lore) {
- for (int i = 0; i < lore.length(); i += 2) {
- lore = lore.substring(0, i) + org.bukkit.ChatColor.COLOR_CHAR + lore.substring(i, lore.length());
- }
- return lore;
- }
- //removes the formatting characters
- public static String decodeHiddenLore(String lore) {
- while (lore.contains(org.bukkit.ChatColor.COLOR_CHAR + "")) {
- lore = lore.substring(0, lore.indexOf(org.bukkit.ChatColor.COLOR_CHAR)) + lore.substring(lore.indexOf(org.bukkit.ChatColor.COLOR_CHAR) + 1, lore.length());
- }
- return lore;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement