Advertisement
Guest User

Main.class

a guest
May 18th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.35 KB | None | 0 0
  1. package wcs;
  2.  
  3. import java.io.File;
  4.  
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.Location;
  7. import org.bukkit.Material;
  8. import org.bukkit.block.Block;
  9. import org.bukkit.block.BlockState;
  10. import org.bukkit.block.Sign;
  11. import org.bukkit.event.EventHandler;
  12. import org.bukkit.event.Listener;
  13. import org.bukkit.event.block.SignChangeEvent;
  14. import org.bukkit.event.player.PlayerChangedWorldEvent;
  15. import org.bukkit.event.player.PlayerJoinEvent;
  16. import org.bukkit.event.player.PlayerQuitEvent;
  17. import org.bukkit.plugin.java.JavaPlugin;
  18. //import java.util.UUID;
  19. //import org.bukkit.World;
  20.  
  21. public class Main extends JavaPlugin implements Listener {
  22.     public void onEnable() {
  23.     getServer().getPluginManager().registerEvents(this, this);
  24.     //File configFile = new File(getDataFolder() + "/config.yml");
  25.     File signsFile = new File(getDataFolder() + "/Signs.yml");
  26.     //if (!configFile.exists()) {
  27.         //saveDefaultConfig();
  28. //  }
  29.     if(!signsFile.exists()){
  30.         Files.saveDefaultConfig(this);
  31.     }
  32.     getConfig();
  33.     File pluginFolder = new File("./plugins/WorldCountSigns/");
  34.     if (!pluginFolder.exists()) {
  35.         pluginFolder.mkdir();
  36.     }
  37.     //if ((pluginFolder.exists()) && (!configFile.exists())){
  38.         //saveDefaultConfig(); }
  39.     if((pluginFolder.exists()) && (!signsFile.exists())){
  40.         Files.saveDefaultConfig(this); }
  41.     }
  42.    
  43.  
  44.     public void onDisable() {
  45.  
  46.     }
  47.  
  48.  
  49.     @EventHandler
  50.     public void onSignCreate(SignChangeEvent event) {
  51.             if (event.getBlock().getType() == Material.SIGN_POST
  52.                     || event.getBlock().getType() == Material.WALL_SIGN) {
  53.                 String worldname = event.getLine(0);
  54.                 if(event.getLine(1).equalsIgnoreCase("[WCS]")){                
  55.                     if(!event.getLine(0).isEmpty()){
  56.                         if(Bukkit.getWorld(worldname) != null){
  57.                         event.setLine(1, "[" + worldname + "]" );
  58.                         int PlayersOnline = Bukkit.getWorld(worldname).getPlayers().size();
  59.                         String players = Integer.toString(PlayersOnline);
  60.                         event.setLine(2, "Players: " + players);
  61.                         Location loc = event.getBlock().getLocation();
  62.                         int x = (int) loc.getX();
  63.                         int y = (int) loc.getY();
  64.                         int z = (int) loc.getZ();
  65.                         String world = event.getBlock().getWorld().getName();
  66.                         int signnumber = Files.getCustomConfig(this).getInt("signnumber") + 1;
  67.                         Files.getCustomConfig(this).set("signs." + signnumber + ".x", x);
  68.                         Files.getCustomConfig(this).set("signs." + signnumber + ".y", y);
  69.                         Files.getCustomConfig(this).set("signs." + signnumber + ".z", z);
  70.                         Files.getCustomConfig(this).set("signs." + signnumber + ".world", world);
  71.                         Files.getCustomConfig(this).set("signs." + signnumber + ".worldb", worldname);
  72.                         Files.getCustomConfig(this).set("signnumber", signnumber);
  73.                         Files.saveCustomConfig(this);
  74.                         //Files.reloadCustomConfig(this);
  75.                         this.getLogger().info("World Sign Created At " + x + ", " + y + ", " + z + " in " + world);
  76.                         event.setLine(0, "");
  77.                         }
  78.                     }else if(event.getLine(0).isEmpty()){
  79.                         event.setLine(1, "[" + event.getPlayer().getWorld().getName() + "]" );
  80.                         int PlayersOnline = event.getPlayer().getWorld().getPlayers().size();
  81.                         String players = Integer.toString(PlayersOnline);
  82.                         event.setLine(2, "Players: " + players);
  83.                         Location loc = event.getBlock().getLocation();
  84.                         int x = (int) loc.getX();
  85.                         int y = (int) loc.getY();
  86.                         int z = (int) loc.getZ();
  87.                         String world = event.getBlock().getWorld().getName();
  88.                         int signnumber = Files.getCustomConfig(this).getInt("signnumber") + 1;
  89.                         Files.getCustomConfig(this).set("signs." + signnumber + ".x", x);
  90.                         Files.getCustomConfig(this).set("signs." + signnumber + ".y", y);
  91.                         Files.getCustomConfig(this).set("signs." + signnumber + ".z", z);
  92.                         Files.getCustomConfig(this).set("signs." + signnumber + ".world", world);
  93.                         Files.saveCustomConfig(this);
  94.                         //Files.reloadCustomConfig(this);
  95.                         this.getLogger().info("World Sign Created At " + x + ", " + y + ", " + z + " in " + world);
  96.                    
  97.                
  98.                     }
  99.             }
  100.             }
  101.             }
  102.    
  103.     @EventHandler  
  104.     public void onPlayerMoveWorld(PlayerChangedWorldEvent event) {
  105.             int sign = Files.getCustomConfig(this).getInt("signnumber") + 1;
  106.            
  107.             for(int currentSign = 1; currentSign<sign; currentSign++)
  108.             {
  109.                 int x = Files.getCustomConfig(this).getInt("signs." + currentSign + ".x");
  110.                 int y = Files.getCustomConfig(this).getInt("signs." + currentSign + ".y");
  111.                 int z = Files.getCustomConfig(this).getInt("signs." + currentSign + ".z");
  112.                 String worldname = Files.getCustomConfig(this).getString("signs." + currentSign + ".worldb");
  113.                 String world1 = Files.getCustomConfig(this).getString("signs." + currentSign + ".world");
  114.                 Block block = Bukkit.getWorld(world1).getBlockAt(x, y, z);
  115.                 block.getChunk().load();
  116.                 BlockState state = block.getState();
  117.                 int PlayersOnline = Bukkit.getWorld(worldname).getPlayers().size();
  118.                 String players = Integer.toString(PlayersOnline);    
  119.                 if (state instanceof Sign){
  120.                     Sign s = (Sign) state;
  121.                     s.setLine(2, "Players: " + players);
  122.                     s.update(true);
  123.                    
  124.                 }
  125.             }
  126.         }
  127.     @EventHandler
  128.     public void onPlayerLeave(PlayerQuitEvent event){
  129.         int sign = Files.getCustomConfig(this).getInt("signnumber") + 1;
  130.        
  131.         for(int currentSign = 1; currentSign<sign; currentSign++)
  132.         {
  133.             int x = Files.getCustomConfig(this).getInt("signs." + currentSign + ".x");
  134.             int y = Files.getCustomConfig(this).getInt("signs." + currentSign + ".y");
  135.             int z = Files.getCustomConfig(this).getInt("signs." + currentSign + ".z");
  136.             String world1 = Files.getCustomConfig(this).getString("signs." + currentSign + ".world");
  137.             String worldname = Files.getCustomConfig(this).getString("signs." + currentSign + ".worldb");
  138.             Block block = Bukkit.getWorld(world1).getBlockAt(x, y, z);
  139.             block.getChunk().load();
  140.             BlockState state = block.getState();
  141.             int PlayersOnline = Bukkit.getWorld(worldname).getPlayers().size();
  142.             String players = null;
  143.             if(worldname.equals(state.getWorld().getName())){
  144.             players = Integer.toString(PlayersOnline - 1);
  145.             }else{
  146.             players = Integer.toString(PlayersOnline); 
  147.             }
  148.             if (state instanceof Sign){
  149.                 Sign s = (Sign) state;
  150.                 s.setLine(2, "Players: " + players);
  151.                 s.update(true);
  152.             }
  153.         }
  154.     }
  155.     @EventHandler
  156.     public void onPlayerJoin(PlayerJoinEvent event){
  157.         int sign = Files.getCustomConfig(this).getInt("signnumber") + 1;
  158.        
  159.         for(int currentSign = 1; currentSign<sign; currentSign++)
  160.         {
  161.            
  162.             int x = Files.getCustomConfig(this).getInt("signs." + currentSign + ".x");
  163.             int y = Files.getCustomConfig(this).getInt("signs." + currentSign + ".y");
  164.             int z = Files.getCustomConfig(this).getInt("signs." + currentSign + ".z");
  165.             String world1 = Files.getCustomConfig(this).getString("signs." + currentSign + ".world");
  166.             String worldname = Files.getCustomConfig(this).getString("signs." + currentSign + ".worldb");
  167.             Block block = Bukkit.getWorld(world1).getBlockAt(x, y, z);
  168.             block.getChunk().load();
  169.             BlockState state = block.getState();
  170.             int PlayersOnline = Bukkit.getWorld(worldname).getPlayers().size();
  171.             String players = null;
  172.             if(worldname.equals(state.getWorld().getName())){
  173.             players = Integer.toString(PlayersOnline + 1);
  174.             }else{
  175.             players = Integer.toString(PlayersOnline); 
  176.             }
  177.             if (state instanceof Sign){
  178.                 Sign s = (Sign) state;
  179.                 s.setLine(2, "Players: " + players);
  180.                 s.update(true);
  181.             }
  182.         }
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement