Advertisement
Guest User

Untitled

a guest
May 23rd, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. package com.voone.serversigns;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.DataOutputStream;
  5. import java.util.ArrayList;
  6.  
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.Location;
  9. import org.bukkit.Material;
  10. import org.bukkit.World;
  11. import org.bukkit.block.Block;
  12. import org.bukkit.configuration.ConfigurationSection;
  13. import org.bukkit.event.EventHandler;
  14. import org.bukkit.event.Listener;
  15. import org.bukkit.event.block.Action;
  16. import org.bukkit.event.block.SignChangeEvent;
  17. import org.bukkit.event.player.PlayerInteractEvent;
  18. import org.bukkit.plugin.java.JavaPlugin;
  19.  
  20. public class ServerSigns extends JavaPlugin implements Listener{
  21.  
  22. public ArrayList<ConnectionSign> signs;
  23.  
  24. public void onEnable(){
  25. signs = new ArrayList<ConnectionSign>();
  26.  
  27. for (String str : getConfig().getKeys(false)) {
  28. ConfigurationSection s = getConfig().getConfigurationSection(str);
  29.  
  30. ConfigurationSection l = s.getConfigurationSection("loc");
  31. World w = Bukkit.getServer().getWorld(l.getString("world"));
  32. double x = l.getDouble("x"), y = l.getDouble("y"), z = l.getDouble("z");
  33. Location loc = new Location(w, x, y, z);
  34.  
  35. if (loc.getBlock() == null) {
  36. getConfig().set(str, null);
  37. } else {
  38. signs.add(new ConnectionSign(loc, s.getString("name"), s.getString("ip"), s.getInt("port")));
  39. }
  40. }
  41.  
  42. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
  43. public void run() {
  44. for (ConnectionSign s : signs) {
  45. s.update();
  46. }
  47. }
  48. }, 0, 20);
  49. Bukkit.getPluginManager().registerEvents(this, this);
  50. Bukkit.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
  51.  
  52. saveDefaultConfig();
  53. }
  54.  
  55. private void save(ConnectionSign sign) {
  56. int size = getConfig().getKeys(false).size() + 1;
  57. getConfig().set(size + ".loc.world", sign.getLocation().getWorld().getName());
  58. getConfig().set(size + ".loc.x", sign.getLocation().getX());
  59. getConfig().set(size + ".loc.y", sign.getLocation().getY());
  60. getConfig().set(size + ".loc.z", sign.getLocation().getZ());
  61. getConfig().set(size + ".name", sign.getName());
  62. getConfig().set(size + ".ip", sign.getIP());
  63. getConfig().set(size + ".port", sign.getPort());
  64. saveConfig();
  65. }
  66.  
  67. @EventHandler
  68. public void onSignChange(SignChangeEvent e){
  69. if(e.getLine(0).equalsIgnoreCase("[ServerSigns]")){
  70. ConnectionSign connectionSign = new ConnectionSign(e.getBlock().getLocation(), e.getLine(1), e.getLine(2), Integer.parseInt(e.getLine(3)));
  71. signs.add(connectionSign);
  72. save(connectionSign);
  73. }
  74. }
  75.  
  76. @EventHandler
  77. public void onPlayerInteract(PlayerInteractEvent e) {
  78. if (e.getAction() == Action.RIGHT_CLICK_BLOCK){
  79. Block block = e.getClickedBlock();
  80. if (block.getType() == Material.SIGN && block.getType() == Material.SIGN_POST && block.getType() == Material.WALL_SIGN){
  81. for (ConnectionSign s : signs) {
  82. if (s.getLocation().equals(block.getLocation())) {
  83. try {
  84. ByteArrayOutputStream b = new ByteArrayOutputStream();
  85. DataOutputStream out = new DataOutputStream(b);
  86.  
  87. out.writeUTF("Connect");
  88. out.writeUTF(s.getName());
  89.  
  90. e.getPlayer().sendPluginMessage(this, "BungeeCord", b.toByteArray());
  91. } catch (Exception ex) {
  92. ex.printStackTrace();
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement