Advertisement
turt2live

ServerPlayerListener.java

Nov 5th, 2011
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. package mw;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileFilter;
  6. import java.io.FileReader;
  7.  
  8. import org.bukkit.Location;
  9. import org.bukkit.event.player.PlayerInteractEvent;
  10. import org.bukkit.event.player.PlayerListener;
  11.  
  12. public class ServerPlayerListener extends PlayerListener{
  13.     public static WorldLauncher plugin;
  14.    
  15.     public ServerPlayerListener(WorldLauncher ams){
  16.         plugin = ams;
  17.     }
  18.    
  19.     public void onPlayerInteract(PlayerInteractEvent event){
  20.         if(event.getClickedBlock() != null){
  21.             Location location = event.getClickedBlock().getLocation();
  22.             if(isPortBlock(location)){
  23.                 String worldname = getPortBlock(location);
  24.                 if(worldname!=null){
  25.                     plugin.port(worldname, event.getPlayer());
  26.                 }
  27.             }
  28.         }
  29.     }
  30.  
  31.     public boolean isPortBlock(Location location){
  32.         boolean isPort = false;
  33.         File dir = new File("plugins/MultiWorld/");
  34.         File files[] = dir.listFiles(new FileFilter(){
  35.             public boolean accept(File file){
  36.                 boolean acc = false;
  37.                 String parts[] = file.toString().split("\\.");
  38.                 if(parts[parts.length-1].equalsIgnoreCase("portal")){
  39.                     acc = true;
  40.                 }
  41.                 return acc;
  42.             }
  43.         });
  44.         boolean found = false;
  45.         for(File file : files){
  46.             try{
  47.                 BufferedReader in = new BufferedReader(new FileReader(file));
  48.                 String line;
  49.                 while((line = in.readLine()) != null){
  50.                     if(line.endsWith(location.getWorld().getName())){
  51.                         String parts[] = line.split("\\.\\.");
  52.                         double x = Double.parseDouble(parts[0]);
  53.                         double y = Double.parseDouble(parts[1]);
  54.                         double z = Double.parseDouble(parts[2]);
  55.                         Location port = new Location(location.getWorld(), x, y, z);
  56.                         if(port.distance(location) < 1){
  57.                             found = true;
  58.                             break;
  59.                         }
  60.                     }
  61.                 }
  62.                 in.close();
  63.             }catch (Exception e){
  64.                 ChatLog.log_error(e.getMessage());
  65.             }
  66.             if(found){
  67.                 break;
  68.             }
  69.         }
  70.         isPort = found;
  71.         return isPort;
  72.     }
  73.    
  74.     public String getPortBlock(Location location){
  75.         File dir = new File("plugins/MultiWorld/");
  76.         File files[] = dir.listFiles(new FileFilter(){
  77.             public boolean accept(File file){
  78.                 boolean acc = false;
  79.                 String parts[] = file.toString().split("\\.");
  80.                 if(parts[parts.length-1].equalsIgnoreCase("portal")){
  81.                     acc = true;
  82.                 }
  83.                 return acc;
  84.             }
  85.         });
  86.         String retWorld = "";
  87.         boolean found = false;
  88.         for(File file : files){
  89.             try{
  90.                 BufferedReader in = new BufferedReader(new FileReader(file));
  91.                 String line;
  92.                 while((line = in.readLine()) != null){
  93.                     if(line.endsWith(location.getWorld().getName())){
  94.                         String parts[] = line.split("\\.\\.");
  95.                         double x = Double.parseDouble(parts[0]);
  96.                         double y = Double.parseDouble(parts[1]);
  97.                         double z = Double.parseDouble(parts[2]);
  98.                         Location port = new Location(location.getWorld(), x, y, z);
  99.                         if(port.distance(location) < 1){
  100.                             String world = "";
  101.                             parts = file.toString().split("MultiWorld");
  102.                             world = parts[parts.length-1].replaceAll("\\.PORTAL", "").replaceAll("\\\\", "").replaceAll("/", "");
  103.                             retWorld = world;
  104.                             found = true;
  105.                             break;
  106.                         }
  107.                     }
  108.                 }
  109.                 in.close();
  110.             }catch (Exception e){
  111.                 ChatLog.log_error(e.getMessage());
  112.             }
  113.             if(found){
  114.                 break;
  115.             }
  116.         }
  117.         return retWorld;
  118.     }
  119. }
  120.  
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement