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.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import net.md_5.bungee.api.ChatColor;
- import net.md_5.bungee.api.chat.ClickEvent;
- import net.md_5.bungee.api.chat.TextComponent;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Entity;
- import org.bukkit.entity.Player;
- import org.bukkit.metadata.FixedMetadataValue;
- import org.bukkit.plugin.java.JavaPlugin;
- public class Main extends JavaPlugin {
- @Override
- public void onEnable() {
- getLogger().info("BetterTeleport was started!");
- }
- @Override
- public void onDisable() {
- getLogger().info("LobbyPvP was stoped!");
- }
- @Override
- public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
- if (!(sender instanceof Player)) {
- Bukkit.getLogger().info("Use this command in game");
- return false;
- }
- //tpdeny
- if (cmd.getName().equals("tpdeny") && sender.hasPermission("essentials.tpdeny")) {
- Player tpTo = (Player) sender;
- Entry<String, Long> player = null;
- //has requests
- if (tpTo.hasMetadata("request")) {
- //requests hashmap
- LinkedHashMap<String, Long> requests = (LinkedHashMap<String, Long>) tpTo.getMetadata("requests").get(0).value();
- if (requests.isEmpty()) {
- tpTo.sendMessage(ChatColor.translateAlternateColorCodes('&', "&cУ вас відсутні запити"));
- return false;
- }
- //get last added element
- if (args.length == 0) {
- List<Entry<String, Long>> entryList
- = new ArrayList<Map.Entry<String, Long>>(requests.entrySet());
- player = entryList.get(entryList.size() - 1);
- requests = removePlayer(requests, player.getKey());
- } else if ("all".equals(args[0])) {
- requests.clear();
- } else {
- String name = args[0];
- player = getPlayer(requests, name);
- if (player == null) {
- tpTo.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4Гравець" + name + "&4не давав запит"));
- } else {
- requests = removePlayer(requests, player.getKey());
- }
- }
- //check is player online
- if (Bukkit.getServer().getPlayer(player.getKey()) != null) {
- Bukkit.getServer().getPlayer(player.getKey()).sendMessage(ChatColor.translateAlternateColorCodes('&', "&5Запит відхилено"));
- tpTo.sendMessage(ChatColor.translateAlternateColorCodes('&', "&5Запит відхилено"));
- }
- tpTo.setMetadata("requests", new FixedMetadataValue(this, requests));
- } else {
- return false;
- }
- }
- //tpaccept
- if (cmd.getName().equals("tpaccept") && sender.hasPermission("essentials.tpaccept")) {
- Player tpTo = (Player) sender;
- Entry<String, Long> player = null;
- if (tpTo.hasMetadata("requests")) {
- LinkedHashMap<String, Long> requests = (LinkedHashMap<String, Long>) tpTo.getMetadata("requests").get(0).value();
- if (requests.isEmpty()) {
- tpTo.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4У вас відсутні запити"));
- return false;
- }
- //get last element added
- if (args.length == 0) {
- List<Entry<String, Long>> entryList
- = new ArrayList<Map.Entry<String, Long>>(requests.entrySet());
- player = entryList.get(entryList.size() - 1);
- requests = removePlayer(requests, player.getKey());
- return true;
- //tp all
- } else if ("all".equals(args[0])) {
- if (safeLocation(tpTo) == null) {
- requests.clear();
- tpTo.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4Небезпечне телепортування!"));
- return true;
- } else {
- for (Map.Entry<String, Long> entry : requests.entrySet()) {
- if(entry !=null) {
- tryTp(entry, tpTo);
- }
- }
- requests.clear();
- tpTo.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Телепортовано усіх з черги!"));
- return true;
- }
- } else {
- String name = args[0];
- player = getPlayer(requests, name);
- if (player == null) {
- tpTo.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4Гравець " + name + " &4не давав запит"));
- return false;
- } else {
- tryTp(player, tpTo);
- requests = removePlayer(requests, name);
- return true;
- }
- }
- tpTo.setMetadata("requests", new FixedMetadataValue(this, requests));
- } else {
- return false;
- }
- }
- if (cmd.getName().equals("call") && sender.hasPermission("essentials.tpa")) {
- Player teleportFrom = (Player) sender;
- LinkedHashMap<String, Long> requests = new LinkedHashMap();
- if (args.length != 1) {
- teleportFrom.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6/call &c<Player>"));
- return true;
- }
- String teleportTo = args[0];
- if (teleportFrom.getName().equalsIgnoreCase(teleportTo)) {
- teleportFrom.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4Неможливо телепортуватись до самого себе"));
- return false;
- }
- //getting player
- if (Bukkit.getServer().getPlayer(teleportTo) != null) {
- Player destination = Bukkit.getServer().getPlayer(teleportTo);
- //Message creating
- teleportFrom.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Запит надіслано гравцю ") + destination.getDisplayName());
- TextComponent message = new TextComponent(teleportFrom.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.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.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);
- destination.spigot().sendMessage(message);
- //saving hashmap
- if (destination.hasMetadata("requests")) {
- requests = (LinkedHashMap<String, Long>) destination.getMetadata("requests").get(0).value();
- }
- if (requests.isEmpty() || !requests.containsKey(teleportFrom.getName())) {
- requests.put(teleportFrom.getName(), System.currentTimeMillis());
- } else {
- requests.replace(teleportFrom.getName(), System.currentTimeMillis());
- }
- destination.setMetadata("requests", new FixedMetadataValue(this, requests));
- }
- }
- return true;
- }
- //check is location safe for teleport
- private 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
- private Entry<String, Long> getPlayer(LinkedHashMap<String, Long> map, String name) {
- Entry<String, Long> player = null;
- for (Entry<String, Long> entry : map.entrySet()) {
- if (entry.getKey().equals(name)) {
- player = entry;
- break;
- }
- }
- return player;
- }
- private LinkedHashMap<String, Long> removePlayer(LinkedHashMap<String, Long> map, String name) {
- for (Entry<String, Long> entry : map.entrySet()) {
- if (entry.getKey().equals(name)) {
- map.remove(entry.getKey());
- break;
- }
- }
- return map;
- }
- private void tryTp(Entry<String,Long> player, Player tpTo) {
- long timestamp = player.getValue();
- long diff = System.currentTimeMillis() - timestamp;
- //timeout for teleport
- if (diff / 1000 > 90) {
- tpTo.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4Час очікування вичерпано"));
- } else if (Bukkit.getPlayer(player.getKey()) != null) {
- Player tpFrom = Bukkit.getServer().getPlayer(player.getKey());
- tpFrom.sendMessage(ChatColor.translateAlternateColorCodes('&', tpTo.getDisplayName() + "&6 прийняв ваш запит на телепортацію"));
- tpFrom.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Телепортування до гравця " + tpTo.getDisplayName()));
- if (safeLocation(tpTo) != null) {
- tpFrom.teleport(safeLocation(tpTo));
- } else {
- tpFrom.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4Небезпечне телепортування!"));
- tpTo.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4Небезпечне телепортування!"));
- }
- } else {
- tpTo.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4Гравця не знайдено!"));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement