BeshoGraphix

Untitled

Jun 28th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.67 KB | None | 0 0
  1. package me.beshographix.tsigns;
  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.ChatColor;
  9. import org.bukkit.Location;
  10. import org.bukkit.Material;
  11. import org.bukkit.World;
  12. import org.bukkit.block.Block;
  13. import org.bukkit.block.Sign;
  14. import org.bukkit.command.Command;
  15. import org.bukkit.command.CommandSender;
  16. import org.bukkit.configuration.ConfigurationSection;
  17. import org.bukkit.entity.Player;
  18. import org.bukkit.event.EventHandler;
  19. import org.bukkit.event.Listener;
  20. import org.bukkit.event.block.Action;
  21. import org.bukkit.event.player.PlayerInteractEvent;
  22. import org.bukkit.plugin.java.JavaPlugin;
  23.  
  24. public class Tsigns extends JavaPlugin implements Listener{
  25.  
  26.     private ArrayList<Signs> signs;
  27.    
  28.     public void onEnable(){
  29.         this.signs = new ArrayList<Signs>();
  30.         saveDefaultConfig();
  31.         for ( String str : getConfig().getKeys(false)){
  32.             ConfigurationSection s = getConfig().getConfigurationSection(str);
  33.             ConfigurationSection l = s.getConfigurationSection("loc");
  34.             World w = Bukkit.getServer().getWorld(l.getString("world"));
  35.             double x = l.getDouble("x"), y = l.getDouble("y"), z = l.getDouble("z");
  36.             Location loc = new Location(w, x, y, z);
  37.             if (loc.getBlock() == null){
  38.                 getConfig().set(str, null);
  39.             }
  40.             else{
  41.                 signs.add(new Signs(loc, s.getString("name"), s.getString("ip"), s.getInt("port")));
  42.             }
  43.         }
  44.        
  45.         Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
  46.            
  47.             public void run() {
  48.                 for (Signs s : signs){
  49.                     s.update();
  50.                 }
  51.                
  52.             }
  53.         }, 0, 20);
  54.        
  55.         Bukkit.getServer().getPluginManager().registerEvents(this, this);
  56.         Bukkit.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
  57.        
  58.     }
  59.    
  60.     @EventHandler
  61.     public void onPlayerInteract(PlayerInteractEvent e){
  62.         if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
  63.         Block block = e.getClickedBlock();
  64.         if (block.getType() != Material.SIGN && block.getType() != Material.SIGN_POST && block.getType() != Material.WALL_SIGN) return;
  65.         for (Signs s : signs) {
  66.             if (s.getLocation().equals(block.getLocation())){
  67.                 try {
  68.                     ByteArrayOutputStream b = new ByteArrayOutputStream();
  69.                     DataOutputStream out = new DataOutputStream(b);
  70.  
  71.                     out.writeUTF("Connect");
  72.                     out.writeUTF(s.getName());
  73.                    
  74.                     e.getPlayer().sendPluginMessage(this, "BungeeCord", b.toByteArray());
  75.             } catch (Exception ex) {
  76.                     ex.printStackTrace();
  77.             }
  78.                
  79.             }
  80.     }
  81.     }
  82.    
  83.     @Override
  84.     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  85.         if (!(sender instanceof Player)){
  86.             sender.sendMessage("§3Only Players can do that !");
  87.             return true;
  88.            
  89.         }
  90.        
  91.         Player p = (Player) sender;
  92.        
  93.         if (cmd.getName().equalsIgnoreCase("titanssigns")){
  94.             if (args.length < 3){
  95.                 p.sendMessage("§3/titanssigns §a<ip> <port> <name>");
  96.                 return true;
  97.                
  98.             }
  99.             String ip = args[0];
  100.             int port;
  101.             String name = args[2];
  102.            
  103.             try {
  104.                     port = Integer.valueOf(args[1]);
  105.             } catch (Exception e) {
  106.                     p.sendMessage(ChatColor.RED + "Port is not a number.");
  107.                     return true;
  108.             }
  109.            
  110.            
  111.             Block b = p.getTargetBlock(null, 10);
  112.             if (b == null){
  113.                 p.sendMessage("§3Please look at a Sign !");
  114.                 return true;
  115.             }
  116.             if (b.getType() != Material.SIGN && b.getType() != Material.SIGN_POST && b.getType() != Material.WALL_SIGN){
  117.                 p.sendMessage("§3Please look at a Sign !");
  118.                 return true;
  119.             }
  120.            
  121.             Signs tsigns = new Signs(b.getLocation(), name, ip, port);
  122.             signs.add(tsigns);
  123.             save(tsigns);
  124.         }
  125.        
  126.        
  127.        
  128.         return true;
  129.     }
  130.  
  131.     private void save(Signs tsigns) {
  132.         int size = getConfig().getKeys(false).size() + 1;
  133.        
  134.         getConfig().set(size + ".loc.world", tsigns.getLocation().getWorld().getName());
  135.         getConfig().set(size + ".loc.x", tsigns.getLocation().getX());
  136.         getConfig().set(size + ".loc.y", tsigns.getLocation().getY());
  137.         getConfig().set(size + ".loc.z", tsigns.getLocation().getZ());
  138.        
  139.         getConfig().set(size + ".name", tsigns.getName());
  140.         getConfig().set(size + ".ip", tsigns.getIP());
  141.         getConfig().set(size + ".port", tsigns.getPort());
  142.        
  143.         saveConfig();
  144.        
  145.     }
  146.    
  147.    
  148.    
  149. }
Advertisement
Add Comment
Please, Sign In to add comment