Advertisement
agentsix1

random

Feb 4th, 2018
40,038
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.46 KB | None | 0 0
  1. package me.yows.teleportblocks;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6.  
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.ChatColor;
  9. import org.bukkit.Effect;
  10. import org.bukkit.Location;
  11. import org.bukkit.Material;
  12. import org.bukkit.block.Block;
  13. import org.bukkit.command.Command;
  14. import org.bukkit.command.CommandSender;
  15. import org.bukkit.enchantments.Enchantment;
  16. import org.bukkit.entity.Player;
  17. import org.bukkit.event.EventHandler;
  18. import org.bukkit.event.Listener;
  19. import org.bukkit.event.block.BlockBreakEvent;
  20. import org.bukkit.event.block.BlockEvent;
  21. import org.bukkit.event.block.BlockPlaceEvent;
  22. import org.bukkit.event.block.BlockRedstoneEvent;
  23. import org.bukkit.event.player.PlayerMoveEvent;
  24. import org.bukkit.inventory.ItemStack;
  25. import org.bukkit.inventory.ShapedRecipe;
  26. import org.bukkit.inventory.meta.ItemMeta;
  27. import org.bukkit.plugin.PluginDescriptionFile;
  28. import org.bukkit.plugin.java.JavaPlugin;
  29. import org.bukkit.scheduler.BukkitScheduler;
  30.  
  31.  
  32.  
  33. public class main extends JavaPlugin  implements Listener{
  34.     public HashMap<Location, Location> teleports = new HashMap<Location, Location>();
  35.     public HashMap<String, Integer> teleportsId = new HashMap<String, Integer>();
  36.     public HashMap<Integer, Location> teleportsIdLoc = new HashMap<Integer, Location>();
  37.    
  38.     @SuppressWarnings("deprecation")
  39.     @Override
  40.     public void onEnable() {
  41.         Bukkit.getServer().getPluginManager().registerEvents(this, this);
  42.         ItemStack tpBlockItem = new ItemStack(Material.SMOOTH_BRICK, 1, (byte) 3);
  43.         tpBlockItem = addName(tpBlockItem, "Teleporter Block");
  44.         tpBlockItem = addEnchant(tpBlockItem, "OXYGEN@1");
  45.         tpBlockItem = addLore(tpBlockItem, "This block needs coordinates added before you can place it.");
  46.          
  47.         ShapedRecipe tpBlock = new ShapedRecipe(tpBlockItem);
  48.          
  49.         tpBlock.shape("*D*","%B%","CCC");
  50.          
  51.         tpBlock.setIngredient('*', Material.STONE);
  52.         tpBlock.setIngredient('%', Material.GOLD_BLOCK);
  53.         tpBlock.setIngredient('B', Material.ENDER_PEARL);
  54.         tpBlock.setIngredient('C', Material.BLAZE_POWDER);
  55.         tpBlock.setIngredient('D', Material.DIAMOND);
  56.          
  57.         getServer().addRecipe(tpBlock);
  58.         getConfig().options().copyDefaults(true);
  59.         saveConfig();
  60.         refreshBlocks();
  61.         BukkitScheduler scheduler = getServer().getScheduler();
  62.         scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
  63.             @Override
  64.             public void run() {
  65.  
  66.                 for (Location a : teleports.keySet())
  67.                 a.getWorld().playEffect(a, Effect.MOBSPAWNER_FLAMES, 2006);
  68.             }
  69.         }, 0L, 20L);
  70.     }
  71.    
  72.     @Override
  73.     public void onDisable() {
  74.  
  75.     }
  76.  
  77.    
  78.     @SuppressWarnings("deprecation")
  79.     public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args) {
  80.         if (sender instanceof Player) {
  81.             Player p = (Player) sender;
  82.             if (args.length == 1) {
  83.                 if (args[0].equalsIgnoreCase("test") && p.isOp()) {
  84.                     ItemStack tpBlockItem = new ItemStack(Material.SMOOTH_BRICK, 1, (byte) 3);
  85.                     tpBlockItem = addName(tpBlockItem, "Teleporter Block");
  86.                     tpBlockItem = addEnchant(tpBlockItem, "OXYGEN@1");
  87.                     tpBlockItem = addLore(tpBlockItem, "This block needs coordinates added before you can place it.");
  88.                     p.getInventory().addItem(tpBlockItem);
  89.                 }
  90.                 if (args[0].equalsIgnoreCase("addcords")) {
  91.                     if (p.hasPermission("teleporterblocks.user") || p.hasPermission("teleporterblocks.user.addcords")) {
  92.                         try {
  93.                             for (String t : p.getItemInHand().getItemMeta().getLore()) {
  94.                                 if (p.getWorld().getName().equalsIgnoreCase("world")) {
  95.                                     if (t.equalsIgnoreCase("This block needs coordinates added before you can place it.")) {
  96.                                         if (!(p.getInventory().firstEmpty() == -1)) {
  97.                                             ItemStack tpBlockItem = new ItemStack(Material.SMOOTH_BRICK, p.getItemInHand().getAmount()-1, (byte) 3);
  98.                                             tpBlockItem = addName(tpBlockItem, "Teleporter Block");
  99.                                             tpBlockItem = addEnchant(tpBlockItem, "OXYGEN@1");
  100.                                             tpBlockItem = addLore(tpBlockItem, "This block needs coordinates added before you can place it.");
  101.                                             p.getInventory().setItemInMainHand(tpBlockItem);
  102.                                             tpBlockItem = new ItemStack(Material.SMOOTH_BRICK, 1, (byte) 3);
  103.                                             tpBlockItem = addName(tpBlockItem, "Teleporter Block");
  104.                                             tpBlockItem = addEnchant(tpBlockItem, "OXYGEN@1");
  105.                                             tpBlockItem = addLore(tpBlockItem, "X:" + p.getLocation().getBlockX() + " Y:" + p.getLocation().getBlockY() + " Z:" + p.getLocation().getBlockZ() + " W:" + p.getLocation().getWorld().getName());
  106.                                             p.getInventory().addItem(tpBlockItem);
  107.                                             p.sendMessage(ct("&aSuccess! It appears we have set the coordinates to the block in your inventory. You are free to place this where ever you please! Enjoy."));
  108.                                         } else {
  109.                                             p.sendMessage(ct("&cIt appears your inventory is full and we could not complete the process of adding the coordinates to this item. Clear up some space and try again."));
  110.                                         }
  111.                                        
  112.                                     } else {
  113.                                         if (p.getItemInHand().getItemMeta().hasEnchant(Enchantment.OXYGEN) && p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("Teleporter Block") && p.getItemInHand().getType().equals(Material.SMOOTH_BRICK)) {
  114.                                             p.sendMessage(ct("&cIt appears this item already contains a set of coordinates. Type &a'/tpb changecords' &cto set a new set of coordinates."));
  115.                                         } else {
  116.                                             p.sendMessage(ct("&cIt appears you are not holding a block that is compatible with this plugin. Please try again with the proper block. Type &a'/tpb recipe' &cfor a compatible block."));
  117.                                         }
  118.                                        
  119.                                     }
  120.                                 } else {
  121.                                     p.sendMessage(ct("&cat this time we are not allowing cross dimensional teleporation. Sorry :'("));
  122.                                 }
  123.                                
  124.                             }
  125.                         } catch (NullPointerException ex) {
  126.                             p.sendMessage(ct("&cIt appears you are not holding a block that is compatible with this plugin. Please try again with the proper block. Type &a'/tpb recipe' &cfor a compatible block."));
  127.                         }
  128.                        
  129.                     }
  130.                 }
  131.                
  132.             }
  133.            
  134.         }
  135.         return false;
  136.     }
  137.    
  138.    
  139.     @SuppressWarnings("deprecation")
  140.     @EventHandler
  141.     public void onBlockPlace(BlockPlaceEvent e) {
  142.             Player p = (Player) e.getPlayer();
  143.            
  144.             try {
  145.                 for (String t : p.getItemInHand().getItemMeta().getLore()) {
  146.                     if (t.equalsIgnoreCase("This block needs coordinates added before you can place it.")) {
  147.                         p.sendMessage(ct("&cOh sorry you need to type &a'/tpb addcords' &cbefore you can place this block."));
  148.                         e.setCancelled(true);
  149.                         return;
  150.                     } else if (t.contains("X:") && t.contains(" Y:") && t.contains(" Z:") && t.contains(" W:")) {
  151.                         if (!p.getWorld().getName().equalsIgnoreCase("world")) {
  152.                             p.sendMessage(ct("&cat this time we are not allowing cross dimensional teleporation. Sorry :'("));
  153.                             e.setCancelled(true);
  154.                             return;
  155.                         }
  156.                         getConfig().set("Blocks." + getConfig().getInt("Count") + ".From", "X:" + e.getBlock().getLocation().getBlockX() + " Y:" + e.getBlock().getLocation().getBlockY() + " Z:" + e.getBlock().getLocation().getBlockZ() + " W:" + e.getBlock().getLocation().getWorld().getName());
  157.                         saveConfig();
  158.                         reloadConfig();
  159.                         getConfig().set("Blocks." + getConfig().getInt("Count") + ".Teleport", p.getItemInHand().getItemMeta().getLore().get(0));
  160.                         saveConfig();
  161.                         reloadConfig();
  162.                         getConfig().set("Blocks." + getConfig().getInt("Count") + ".Player", p.getUniqueId().toString());
  163.                         saveConfig();
  164.                         reloadConfig();
  165.                         getConfig().set("Count", getConfig().getInt("Count") + 1);
  166.                         saveConfig();
  167.                         reloadConfig();
  168.                         p.sendMessage(ct("&aSuccess! It appears your teleported has been linked and created. You are free to use it as you want. Just remember anyone can use it."));
  169.                         refreshBlocks();
  170.                     }
  171.                 }
  172.             } catch (NullPointerException ex) {
  173.                
  174.             }
  175.            
  176.     }
  177.    
  178.     @EventHandler
  179.     public void onBlockBreak(BlockBreakEvent e) {
  180.         if (teleports.containsKey(e.getBlock().getLocation())) {
  181.             System.out.println(1);
  182.             Location found = e.getBlock().getLocation();
  183.             try {
  184.                 for (int id : teleportsIdLoc.keySet()) {
  185.                     System.out.println(2);
  186.                     if (teleportsIdLoc.get(id).equals(found)) {
  187.                         e.setCancelled(true);
  188.                         e.getBlock().setType(Material.AIR);
  189.                        
  190.                         getConfig().set("Blocks." + id, "");
  191.                         saveConfig();
  192.                         reloadConfig();
  193.                         refreshBlocks();
  194.                         e.getPlayer().sendMessage(ct("&6This teleporter has been broken. A new teleporter block has been dropped."));
  195.                         ItemStack tpBlockItem = new ItemStack(Material.SMOOTH_BRICK, 1, (byte) 3);
  196.                         tpBlockItem = addName(tpBlockItem, "Teleporter Block");
  197.                         tpBlockItem = addEnchant(tpBlockItem, "OXYGEN@1");
  198.                         tpBlockItem = addLore(tpBlockItem, "This block needs coordinates added before you can place it.");
  199.                                            
  200.                         e.getBlock().getLocation().getWorld().dropItemNaturally(e.getBlock().getLocation(), tpBlockItem);
  201.                     }
  202.                 }
  203.             } catch (Exception ex) {
  204.                
  205.             }
  206.            
  207.            
  208.                
  209.         }
  210.     }
  211.    
  212.     @EventHandler
  213.     public void onPlayerMove(PlayerMoveEvent e) {
  214.         Player p = (Player) e.getPlayer();
  215.         Location p1U = new Location(p.getWorld(), p.getLocation().getX(), p.getLocation().getY()-1, p.getLocation().getZ());
  216.         for (Location a : teleports.keySet()) {
  217.             if (p1U.distance(a) < 1) {
  218.                 System.out.println("test");
  219.                 teleports.get(a).getWorld().loadChunk(teleports.get(a).getBlockX(), teleports.get(a).getBlockY());
  220.                 p.teleport(teleports.get(a));
  221.             }
  222.         }
  223.     }
  224.    
  225.    
  226.     public  ItemStack addEnchant(ItemStack it, String enchantList) {
  227.         if (!enchantList.equalsIgnoreCase("none")) {
  228.             String[] e = enchantList.split(",");
  229.             for (String preEnchant : e) {
  230.                 String[] enchant = preEnchant.split("@");
  231.                 it.addUnsafeEnchantment(Enchantment.getByName(enchant[0]), Integer.parseInt(enchant[1]));
  232.             }
  233.             return it;
  234.         } else {
  235.             return it;
  236.         }
  237.        
  238.     }
  239.  
  240.     public  ItemStack addLore(ItemStack it, String loreList) {
  241.         if (!loreList.equalsIgnoreCase("none")) {
  242.             ItemMeta m = it.getItemMeta();
  243.             List<String> lores = new ArrayList<String>();
  244.             String[] preLores = loreList.split(",");
  245.             for (String lore : preLores) { lores.add(ct(lore)); }
  246.             m.setLore(lores);
  247.             it.setItemMeta(m);
  248.             return it;
  249.         } else {
  250.             return it;
  251.         }
  252.     }
  253.    
  254.     public  ItemStack addName(ItemStack it, String name) {
  255.         if (!name.equalsIgnoreCase("none")) {
  256.             ItemMeta m = it.getItemMeta();
  257.             m.setDisplayName(ct(name));
  258.             it.setItemMeta(m);
  259.             return it;
  260.         } else {
  261.             return it;
  262.         }
  263.        
  264.     }
  265.    
  266.     public String ct(String text) {
  267.         String finalText = text.replace("&0", ChatColor.BLACK + "").replace("&1", ChatColor.DARK_BLUE + "").replace("&2", ChatColor.DARK_GREEN + "").replace("&3", ChatColor.DARK_AQUA + "").replace("&4", ChatColor.DARK_RED + "").replace("&5", ChatColor.DARK_PURPLE + "").replace("&6", ChatColor.GOLD + "").replace("&7", ChatColor.GRAY + "").replace("&8", ChatColor.DARK_GRAY + "").replace("&9", ChatColor.BLUE + "").replace("&a", ChatColor.GREEN + "").replace("&b", ChatColor.AQUA + "").replace("&c", ChatColor.RED + "").replace("&d", ChatColor.LIGHT_PURPLE + "").replace("&e", ChatColor.YELLOW + "").replace("&f", ChatColor.WHITE + "").replace("&l", ChatColor.BOLD + "").replace("&m", ChatColor.STRIKETHROUGH + "").replace("&n", ChatColor.UNDERLINE + "").replace("&o", ChatColor.ITALIC + "").replace("&r", ChatColor.RESET + "");
  268.         return finalText;
  269.     }
  270.    
  271.     public void refreshBlocks() {
  272.         int i = 0;
  273.         teleports.clear();
  274.         teleportsId.clear();
  275.         teleportsIdLoc.clear();
  276.         do {
  277.             try {
  278.             System.out.println(i);
  279.             String prefrom = getConfig().getString("Blocks." + i + ".From");
  280.             String preteleport = getConfig().getString("Blocks." + i + ".Teleport");
  281.             String playerUUID = getConfig().getString("Blocks." + i + ".Player");
  282.             //Location From
  283.             String[] froms = prefrom.split(" ");
  284.             Location From = new Location(Bukkit.getWorld(froms[3].split("W:")[1]), Double.parseDouble(froms[0].split("X:")[1]), Double.parseDouble(froms[1].split("Y:")[1]), Double.parseDouble(froms[2].split("Z:")[1]));
  285.             String[] teleported = preteleport.split(" ");
  286.             Location Teleport = new Location(Bukkit.getWorld(teleported[3].split("W:")[1]), Double.parseDouble(teleported[0].split("X:")[1]), Double.parseDouble(teleported[1].split("Y:")[1]), Double.parseDouble(teleported[2].split("Z:")[1]));
  287.             teleportsIdLoc.put(i, From);
  288.             teleportsId.put(playerUUID, i);
  289.             teleports.put(From, Teleport);
  290.             } catch (Exception ex) {
  291.                
  292.             }
  293.             i++;
  294.         } while (i < getConfig().getInt("Count"));     
  295.     }
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement