Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.yows.teleportblocks;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.Effect;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.block.Block;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.enchantments.Enchantment;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.block.BlockBreakEvent;
- import org.bukkit.event.block.BlockEvent;
- import org.bukkit.event.block.BlockPlaceEvent;
- import org.bukkit.event.block.BlockRedstoneEvent;
- import org.bukkit.event.player.PlayerMoveEvent;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.ShapedRecipe;
- import org.bukkit.inventory.meta.ItemMeta;
- import org.bukkit.plugin.PluginDescriptionFile;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.scheduler.BukkitScheduler;
- public class main extends JavaPlugin implements Listener{
- public HashMap<Location, Location> teleports = new HashMap<Location, Location>();
- public HashMap<String, Integer> teleportsId = new HashMap<String, Integer>();
- public HashMap<Integer, Location> teleportsIdLoc = new HashMap<Integer, Location>();
- @SuppressWarnings("deprecation")
- @Override
- public void onEnable() {
- Bukkit.getServer().getPluginManager().registerEvents(this, this);
- ItemStack tpBlockItem = new ItemStack(Material.SMOOTH_BRICK, 1, (byte) 3);
- tpBlockItem = addName(tpBlockItem, "Teleporter Block");
- tpBlockItem = addEnchant(tpBlockItem, "OXYGEN@1");
- tpBlockItem = addLore(tpBlockItem, "This block needs coordinates added before you can place it.");
- ShapedRecipe tpBlock = new ShapedRecipe(tpBlockItem);
- tpBlock.shape("*D*","%B%","CCC");
- tpBlock.setIngredient('*', Material.STONE);
- tpBlock.setIngredient('%', Material.GOLD_BLOCK);
- tpBlock.setIngredient('B', Material.ENDER_PEARL);
- tpBlock.setIngredient('C', Material.BLAZE_POWDER);
- tpBlock.setIngredient('D', Material.DIAMOND);
- getServer().addRecipe(tpBlock);
- getConfig().options().copyDefaults(true);
- saveConfig();
- refreshBlocks();
- BukkitScheduler scheduler = getServer().getScheduler();
- scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
- @Override
- public void run() {
- for (Location a : teleports.keySet())
- a.getWorld().playEffect(a, Effect.MOBSPAWNER_FLAMES, 2006);
- }
- }, 0L, 20L);
- }
- @Override
- public void onDisable() {
- }
- @SuppressWarnings("deprecation")
- public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args) {
- if (sender instanceof Player) {
- Player p = (Player) sender;
- if (args.length == 1) {
- if (args[0].equalsIgnoreCase("test") && p.isOp()) {
- ItemStack tpBlockItem = new ItemStack(Material.SMOOTH_BRICK, 1, (byte) 3);
- tpBlockItem = addName(tpBlockItem, "Teleporter Block");
- tpBlockItem = addEnchant(tpBlockItem, "OXYGEN@1");
- tpBlockItem = addLore(tpBlockItem, "This block needs coordinates added before you can place it.");
- p.getInventory().addItem(tpBlockItem);
- }
- if (args[0].equalsIgnoreCase("addcords")) {
- if (p.hasPermission("teleporterblocks.user") || p.hasPermission("teleporterblocks.user.addcords")) {
- try {
- for (String t : p.getItemInHand().getItemMeta().getLore()) {
- if (p.getWorld().getName().equalsIgnoreCase("world")) {
- if (t.equalsIgnoreCase("This block needs coordinates added before you can place it.")) {
- if (!(p.getInventory().firstEmpty() == -1)) {
- ItemStack tpBlockItem = new ItemStack(Material.SMOOTH_BRICK, p.getItemInHand().getAmount()-1, (byte) 3);
- tpBlockItem = addName(tpBlockItem, "Teleporter Block");
- tpBlockItem = addEnchant(tpBlockItem, "OXYGEN@1");
- tpBlockItem = addLore(tpBlockItem, "This block needs coordinates added before you can place it.");
- p.getInventory().setItemInMainHand(tpBlockItem);
- tpBlockItem = new ItemStack(Material.SMOOTH_BRICK, 1, (byte) 3);
- tpBlockItem = addName(tpBlockItem, "Teleporter Block");
- tpBlockItem = addEnchant(tpBlockItem, "OXYGEN@1");
- tpBlockItem = addLore(tpBlockItem, "X:" + p.getLocation().getBlockX() + " Y:" + p.getLocation().getBlockY() + " Z:" + p.getLocation().getBlockZ() + " W:" + p.getLocation().getWorld().getName());
- p.getInventory().addItem(tpBlockItem);
- 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."));
- } else {
- 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."));
- }
- } else {
- if (p.getItemInHand().getItemMeta().hasEnchant(Enchantment.OXYGEN) && p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("Teleporter Block") && p.getItemInHand().getType().equals(Material.SMOOTH_BRICK)) {
- p.sendMessage(ct("&cIt appears this item already contains a set of coordinates. Type &a'/tpb changecords' &cto set a new set of coordinates."));
- } else {
- 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."));
- }
- }
- } else {
- p.sendMessage(ct("&cat this time we are not allowing cross dimensional teleporation. Sorry :'("));
- }
- }
- } catch (NullPointerException ex) {
- 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."));
- }
- }
- }
- }
- }
- return false;
- }
- @SuppressWarnings("deprecation")
- @EventHandler
- public void onBlockPlace(BlockPlaceEvent e) {
- Player p = (Player) e.getPlayer();
- try {
- for (String t : p.getItemInHand().getItemMeta().getLore()) {
- if (t.equalsIgnoreCase("This block needs coordinates added before you can place it.")) {
- p.sendMessage(ct("&cOh sorry you need to type &a'/tpb addcords' &cbefore you can place this block."));
- e.setCancelled(true);
- return;
- } else if (t.contains("X:") && t.contains(" Y:") && t.contains(" Z:") && t.contains(" W:")) {
- if (!p.getWorld().getName().equalsIgnoreCase("world")) {
- p.sendMessage(ct("&cat this time we are not allowing cross dimensional teleporation. Sorry :'("));
- e.setCancelled(true);
- return;
- }
- 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());
- saveConfig();
- reloadConfig();
- getConfig().set("Blocks." + getConfig().getInt("Count") + ".Teleport", p.getItemInHand().getItemMeta().getLore().get(0));
- saveConfig();
- reloadConfig();
- getConfig().set("Blocks." + getConfig().getInt("Count") + ".Player", p.getUniqueId().toString());
- saveConfig();
- reloadConfig();
- getConfig().set("Count", getConfig().getInt("Count") + 1);
- saveConfig();
- reloadConfig();
- 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."));
- refreshBlocks();
- }
- }
- } catch (NullPointerException ex) {
- }
- }
- @EventHandler
- public void onBlockBreak(BlockBreakEvent e) {
- if (teleports.containsKey(e.getBlock().getLocation())) {
- System.out.println(1);
- Location found = e.getBlock().getLocation();
- try {
- for (int id : teleportsIdLoc.keySet()) {
- System.out.println(2);
- if (teleportsIdLoc.get(id).equals(found)) {
- e.setCancelled(true);
- e.getBlock().setType(Material.AIR);
- getConfig().set("Blocks." + id, "");
- saveConfig();
- reloadConfig();
- refreshBlocks();
- e.getPlayer().sendMessage(ct("&6This teleporter has been broken. A new teleporter block has been dropped."));
- ItemStack tpBlockItem = new ItemStack(Material.SMOOTH_BRICK, 1, (byte) 3);
- tpBlockItem = addName(tpBlockItem, "Teleporter Block");
- tpBlockItem = addEnchant(tpBlockItem, "OXYGEN@1");
- tpBlockItem = addLore(tpBlockItem, "This block needs coordinates added before you can place it.");
- e.getBlock().getLocation().getWorld().dropItemNaturally(e.getBlock().getLocation(), tpBlockItem);
- }
- }
- } catch (Exception ex) {
- }
- }
- }
- @EventHandler
- public void onPlayerMove(PlayerMoveEvent e) {
- Player p = (Player) e.getPlayer();
- Location p1U = new Location(p.getWorld(), p.getLocation().getX(), p.getLocation().getY()-1, p.getLocation().getZ());
- for (Location a : teleports.keySet()) {
- if (p1U.distance(a) < 1) {
- System.out.println("test");
- teleports.get(a).getWorld().loadChunk(teleports.get(a).getBlockX(), teleports.get(a).getBlockY());
- p.teleport(teleports.get(a));
- }
- }
- }
- public ItemStack addEnchant(ItemStack it, String enchantList) {
- if (!enchantList.equalsIgnoreCase("none")) {
- String[] e = enchantList.split(",");
- for (String preEnchant : e) {
- String[] enchant = preEnchant.split("@");
- it.addUnsafeEnchantment(Enchantment.getByName(enchant[0]), Integer.parseInt(enchant[1]));
- }
- return it;
- } else {
- return it;
- }
- }
- public ItemStack addLore(ItemStack it, String loreList) {
- if (!loreList.equalsIgnoreCase("none")) {
- ItemMeta m = it.getItemMeta();
- List<String> lores = new ArrayList<String>();
- String[] preLores = loreList.split(",");
- for (String lore : preLores) { lores.add(ct(lore)); }
- m.setLore(lores);
- it.setItemMeta(m);
- return it;
- } else {
- return it;
- }
- }
- public ItemStack addName(ItemStack it, String name) {
- if (!name.equalsIgnoreCase("none")) {
- ItemMeta m = it.getItemMeta();
- m.setDisplayName(ct(name));
- it.setItemMeta(m);
- return it;
- } else {
- return it;
- }
- }
- public String ct(String text) {
- 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 + "");
- return finalText;
- }
- public void refreshBlocks() {
- int i = 0;
- teleports.clear();
- teleportsId.clear();
- teleportsIdLoc.clear();
- do {
- try {
- System.out.println(i);
- String prefrom = getConfig().getString("Blocks." + i + ".From");
- String preteleport = getConfig().getString("Blocks." + i + ".Teleport");
- String playerUUID = getConfig().getString("Blocks." + i + ".Player");
- //Location From
- String[] froms = prefrom.split(" ");
- 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]));
- String[] teleported = preteleport.split(" ");
- 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]));
- teleportsIdLoc.put(i, From);
- teleportsId.put(playerUUID, i);
- teleports.put(From, Teleport);
- } catch (Exception ex) {
- }
- i++;
- } while (i < getConfig().getInt("Count"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement