Advertisement
Guest User

AlarmMessages

a guest
Jul 18th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.17 KB | None | 0 0
  1. #### Alarm Messages ###
  2. package me.Skill4Kill12.alarm;
  3.  
  4. import java.util.HashMap;
  5. import java.util.logging.Logger;
  6.  
  7. import org.bukkit.Location;
  8. import org.bukkit.plugin.PluginDescriptionFile;
  9. import org.bukkit.plugin.PluginManager;
  10. import org.bukkit.plugin.java.JavaPlugin;
  11.  
  12. public class AlarmMessage extends JavaPlugin{
  13.     public final BlockListener BlockListener = new BlockListener();
  14.     public final Logger logger = Logger.getLogger("Minecraft");
  15.     public static AlarmMessage plugin;
  16.    
  17.     public static HashMap<Location, String> hm = new HashMap<Location, String>();
  18.  
  19.     @Override
  20.     public void onDisable() {
  21.         Data.Save();
  22.         this.logger.info("[AlarmMessage] The Plugin Has Been Disabled!");
  23.     }
  24.  
  25.     @Override
  26.     public void onEnable() {
  27.         PluginManager pm = getServer().getPluginManager();
  28.         pm.registerEvents(this.BlockListener, this);
  29.         PluginDescriptionFile pdfFile = this.getDescription();
  30.                 Data.Load();
  31.         this.logger.info("[AlarmMessage]" + pdfFile.getName() + " Version: " + pdfFile.getVersion() + " Has Been Enabled!");
  32.     }
  33. }
  34.  
  35. ######################################################################################################################
  36.  
  37. ## Data ##
  38.  
  39. package me.Skill4Kill12.alarm;
  40.  
  41. import java.io.BufferedReader;
  42. import java.io.BufferedWriter;
  43. import java.io.File;
  44. import java.io.FileReader;
  45. import java.io.FileWriter;
  46. import java.io.IOException;
  47. import java.util.Iterator;
  48.  
  49. import org.bukkit.Bukkit;
  50. import org.bukkit.Location;
  51. import org.bukkit.World;
  52.  
  53. public class Data {
  54.     static File file = new File("plugins/AlarmMessage/data");
  55.     static File folder = new File("plugins/AlarmMessage/");
  56.     public static AlarmMessage plugin;
  57.    
  58.     @SuppressWarnings("unused")
  59.     public static boolean Load() {
  60.         // check if file exists;
  61.         if (!file.exists()) {
  62.             try {
  63.                 folder.mkdirs();
  64.                 file.createNewFile();
  65.             } catch (IOException e) {
  66.                 return false;
  67.             }
  68.             return true;
  69.         }
  70.  
  71.         // Read data!;
  72.         try {
  73.             BufferedReader in = new BufferedReader(new FileReader(file));
  74.             String string;
  75.             while ((string = in.readLine()) != null) {
  76.                 if (string != null) {
  77.                     AlarmMessage log = new AlarmMessage();
  78.                     log.logger.info("[AlarmMessage] Reading file");
  79.                     stringToHashMap(string);
  80.                 }
  81.             }
  82.             in.close();
  83.         } catch (IOException e) {
  84.             AlarmMessage core = new AlarmMessage();
  85.             core.logger.warning("Can not Load the File.");
  86.             return false;
  87.         }
  88.         BufferedReader in;
  89.         return true;
  90.     }
  91.  
  92.     // Save data!;
  93.     @SuppressWarnings("unused")
  94.     public static boolean Save() {
  95.         if (!file.exists()) {
  96.             try {
  97.                 folder.mkdirs();
  98.                 file.createNewFile();
  99.             } catch (IOException e) {
  100.                 return false;
  101.             }
  102.         }
  103.         try {
  104.             BufferedWriter out = new BufferedWriter(new FileWriter(file));
  105.             @SuppressWarnings("resource")
  106.             BufferedReader in = new BufferedReader(new FileReader(file));
  107.             Iterator<Location> iterator = AlarmMessage.hm.keySet().iterator();
  108.             while(iterator.hasNext()){
  109.                 Location location = iterator.next();
  110.                 String line234 = AlarmMessage.hm.get(location);
  111.             out.write(LocationToString(location));
  112.             out.write(" - " + line234);
  113.             out.newLine();
  114.         }
  115.             out.close();
  116.         } catch (IOException e) {
  117.             return false;
  118.         }
  119.         BufferedWriter out;
  120.         return true;
  121.  
  122.     }
  123.  
  124.     private static void stringToHashMap(String string) {       
  125.     String[] split = string.split(" - ");
  126.        
  127.         try {
  128.             World world = Bukkit.getWorld(split[0]);
  129.             double x = Double.parseDouble(split[1]);
  130.             double y = Double.parseDouble(split[2]);
  131.             double z = Double.parseDouble(split[3]);
  132.             Location location = new Location (world, x, y, z);
  133.            
  134.             String tekst = split[4];
  135.            
  136.             AlarmMessage.hm.put(location, tekst);
  137.         } catch (Exception e) {
  138.             AlarmMessage lugin = new AlarmMessage();
  139.             lugin.logger.warning("Error Converting File");
  140.             lugin.logger.warning("Make shure that it look like (World - Double x - Double y - Double z - String)");
  141.             lugin.logger.info("World is just a name, Doubles are cordinates, String is tekst");
  142.             lugin.logger.info("If the file looks correct and you get the error please make a ticket, and attach your file");
  143.         }
  144.     }
  145.  
  146.     private static String LocationToString(Location location) {
  147.         String world = location.getWorld().getName().trim();
  148.         double x = location.getX();
  149.         double y = location.getY();
  150.         double z = location.getZ();
  151.         String sx = Double.toString(x);
  152.         String sy = Double.toString(y);
  153.         String sz = Double.toString(z);
  154.         String tekst = world + " - " + sx + " - " + sy + " - " + sz;
  155.  
  156.         return tekst;
  157.     }
  158. }
  159.  
  160. ###################################################################################################################
  161.  
  162. ## Block Listener ##
  163.  
  164. package me.Skill4Kill12.alarm;
  165.  
  166. import java.util.Arrays;
  167. import java.util.Iterator;
  168. import java.util.List;
  169.  
  170. import org.bukkit.Bukkit;
  171. import org.bukkit.ChatColor;
  172. import org.bukkit.Location;
  173. import org.bukkit.block.Sign;
  174. import org.bukkit.entity.Player;
  175. import org.bukkit.event.EventHandler;
  176. import org.bukkit.event.Listener;
  177. import org.bukkit.event.block.BlockBreakEvent;
  178. import org.bukkit.event.block.BlockIgniteEvent;
  179. import org.bukkit.event.block.BlockRedstoneEvent;
  180. import org.bukkit.event.block.SignChangeEvent;
  181.  
  182. public class BlockListener implements Listener {
  183.     public static AlarmMessage plugin;
  184.  
  185.     @EventHandler
  186.     public void onSignChange(SignChangeEvent event) {
  187.         String line1 = event.getLine(0);
  188.         String line2 = event.getLine(1);
  189.         String line3 = event.getLine(2);
  190.         String line4 = event.getLine(3);
  191.         String line234 = "An Emergency Has Occured " + line2 + line3 + line4;
  192.         Location location = event.getBlock().getLocation();
  193.         if (!line1.equals("[Alarm]")) {
  194.             return;
  195.         }
  196.         try {
  197.             AlarmMessage.hm.put(location, line234);
  198.             event.setLine(0, "[!Alarm!]");
  199.         } catch (Exception e) {
  200.             event.setLine(0, "");
  201.             event.setLine(1, "");
  202.             event.setLine(2, "");
  203.             event.setLine(3, "");
  204.             AlarmMessage alarmmessage = new AlarmMessage();
  205.                         alarmmessage.logger.warning("Error Adding Alarm Sign Please Make A Ticket");
  206.                         alarmmessage.logger.warning("" + e);
  207.                 }
  208.     }
  209.    
  210.     @EventHandler
  211.     public void onSignIgnite(BlockIgniteEvent event){
  212.         if(AlarmMessage.hm.containsKey(event.getBlock().getLocation())){
  213.             event.setCancelled(true);
  214.         }
  215.     }
  216.    
  217.     @EventHandler
  218.     public void onSignRemove(BlockBreakEvent event){
  219.         if(AlarmMessage.hm.containsKey(event.getBlock().getLocation())){
  220.             AlarmMessage.hm.remove(event.getBlock().getLocation());
  221.         }
  222.     }
  223.    
  224.     @EventHandler
  225.     public void onBlockPower(BlockRedstoneEvent event) {
  226.  
  227.         if (!(event.getBlock().getState() instanceof Sign)) {
  228.             return;
  229.         }
  230.  
  231.         Location location = event.getBlock().getLocation();
  232.         if(AlarmMessage.hm.containsKey(location)){
  233.             if (event.getBlock().isBlockPowered() || event.getBlock().isBlockIndirectlyPowered()) {
  234.                 List<Player> onlineplayers = Arrays.asList(Bukkit.getServer().getOnlinePlayers());
  235.                 Iterator<Player> iterator = onlineplayers.iterator();
  236.                 String string = AlarmMessage.hm.get(event.getBlock().getLocation());
  237.             while (iterator.hasNext()) {
  238.                 Player onlineplayer = iterator.next();
  239.                 onlineplayer.sendMessage(ChatColor.RED + string);
  240.             }
  241.                      }
  242.         }
  243.     }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement