Advertisement
Mattie

Untitled

Mar 14th, 2011
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.58 KB | None | 0 0
  1. package nl.Matthijs.Mattie.RedstoneSponge;
  2.  
  3. import java.io.*;
  4. import java.util.HashMap;
  5. import java.util.Properties;
  6. import java.util.logging.*;
  7.  
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.Server;
  10. import org.bukkit.event.Event.Priority;
  11.  
  12. import org.bukkit.event.Event;
  13. import org.bukkit.plugin.PluginDescriptionFile;
  14. import org.bukkit.plugin.PluginLoader;
  15. import org.bukkit.plugin.java.JavaPlugin;
  16.  
  17. /**
  18.  * RedstoneSponge for Bukkit
  19.  *
  20.  * @author Matthijs
  21.  */
  22. public class RedstoneSponge extends JavaPlugin
  23. {
  24.     private final RedstoneSpongeBlockListener   blockListener       = new RedstoneSpongeBlockListener(this);
  25.     private final HashMap<Player, Boolean>      debugees            = new HashMap<Player, Boolean>();
  26.     final Logger                                logger              = Logger.getLogger("Minecraft");
  27.     public long                                 startTimeStamp;
  28.     private boolean                             spongeWater;
  29.     private boolean                             spongeLava;
  30.     private int                                 spongeRadius;
  31.     private boolean                             redstoneSponge;
  32.     private boolean                             convertWhenPowered  = false;
  33.     private int                                 convertID;
  34.     private int                                 woolColourCode;
  35.     private boolean                             blockWhenPowered;
  36.  
  37. //  public RedstoneSponge(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader)
  38. //  {
  39. //      super();
  40. //      // TODO: Place any custom initialisation code here
  41. //
  42. //      // NOTE: Event registration should be done in onEnable not here as all events are unregistered when a plugin is disabled
  43. //  }
  44.    
  45.     public void init()
  46.     {
  47.         onEnable();
  48.     }
  49.  
  50.     public void onEnable()
  51.     {
  52.         setup();
  53.         // TODO: Place any custom enable code here including the registration of any events
  54.  
  55.         startTimeStamp = System.currentTimeMillis();
  56.  
  57.         // Register our events
  58.         getServer().getPluginManager().registerEvent(Event.Type.BLOCK_PLACED, new RedstoneSpongeBlockListener(this), Priority.Normal, this);
  59.         getServer().getPluginManager().registerEvent(Event.Type.BLOCK_PHYSICS, new RedstoneSpongeBlockListener(this), Priority.Normal, this);
  60.  
  61.         if (redstoneSponge)//only register this event when set in the config           
  62.         {
  63.             getServer().getPluginManager().registerEvent(Event.Type.REDSTONE_CHANGE, new RedstoneSpongeBlockListener(this), Priority.Normal, this);
  64.         }
  65.         // EXAMPLE: Custom code, here we just output some info so we can check all is well
  66.         PluginDescriptionFile pdfFile = this.getDescription();
  67. //      System.out.println(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!");
  68.         logger.log(Level.INFO, pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!");
  69.     }
  70.  
  71.     public void setup()
  72.     {
  73.         //config file stuff here
  74.         //use some pre-defined values for now
  75.  
  76.         Properties prop = new Properties();
  77.  
  78.         String sep = System.getProperty("file.separator");
  79.         String CBpluginDir = "plugins";
  80.         String pluginDir = "RedstoneSponge";
  81.         String configFileName = "RedstoneSponge.properties";
  82.  
  83.         File f = new File(CBpluginDir + sep + pluginDir + sep + configFileName);
  84.         File dir = new File(CBpluginDir + sep + pluginDir);
  85.  
  86.         if (!dir.exists())
  87.         {
  88.             dir.mkdir();
  89.             logger.log(Level.INFO, "RedstoneSponge Directory does not exists... creating...");
  90.         }
  91.         if (!f.exists())
  92.         {
  93.             try
  94.             {
  95.                 //set the properties value
  96.                 prop.setProperty("water-sponge", "true");
  97.                 prop.setProperty("lava-sponge", "true");
  98.                 prop.setProperty("sponge-radius", "3");
  99.                 prop.setProperty("redstone-sponge", "true");
  100.                 prop.setProperty("convert-when-powered", "false");
  101.                 prop.setProperty("convert-id", "35");
  102.                 prop.setProperty("wool-colour", "YELLOW");
  103.                 prop.setProperty("block-when-powered", "false");
  104.  
  105.                 //save properties to project root folder
  106.                 prop.store(new FileOutputStream(CBpluginDir + sep + pluginDir + sep + configFileName), "Minecraft bukkit server plugin properties\n#RedstoneSponge Plugin!");
  107.                 logger.log(Level.INFO, "RedstoneSponge Config-file does not exists... creating...");
  108.             } catch (IOException ex)
  109.             {
  110.                 logger.log(Level.SEVERE, "RedstoneSponge Directory and/or File can not be found and can not be ceated!");
  111.             }
  112.         }
  113.  
  114.         //and now load the values
  115.  
  116.         try
  117.         {
  118.             //load a properties file
  119.             prop.load(new FileInputStream(CBpluginDir + sep + pluginDir + sep + configFileName));
  120.  
  121.             //get the property value and print it out
  122.  
  123.             spongeWater = Boolean.parseBoolean(prop.getProperty("water-sponge", "true"));
  124.             spongeLava = Boolean.parseBoolean(prop.getProperty("lava-sponge", "true"));
  125.             spongeRadius = (Integer.parseInt(prop.getProperty("sponge-radius", "3")) - 1);
  126.             redstoneSponge = Boolean.parseBoolean(prop.getProperty("redstone-sponge", "true"));
  127.             convertWhenPowered = Boolean.parseBoolean(prop.getProperty("convert-when-powered", "false"));
  128.             convertID = Integer.parseInt(prop.getProperty("convert-id", "35"));
  129.             blockWhenPowered = Boolean.parseBoolean(prop.getProperty("block-when-powered", "false"));
  130. //          woolColour = Integer.parseInt(prop.getProperty("wool-colour", "4"));
  131.             WoolColour c;
  132.             c = WoolColour.valueOf(prop.getProperty("wool-colour", "YELLOW"));
  133.             switch (c)
  134.             {
  135.             case WHITE :
  136.                 woolColourCode = 0;
  137.             break;
  138.             case ORANGE :
  139.                 woolColourCode = 1;
  140.             break;
  141.             case MAGENTA :
  142.                 woolColourCode = 2;
  143.             break;
  144.             case LIGHTBLUE :
  145.                 woolColourCode = 3;
  146.             case YELLOW :
  147.                 woolColourCode = 4;
  148.             break;
  149.             case LIME :
  150.                 woolColourCode = 5;
  151.             break;
  152.             case PINK :
  153.                 woolColourCode = 6;
  154.             break;
  155.             case GRAY :
  156.                 woolColourCode = 7;
  157.             break;
  158.             case LIGHTGRAY :
  159.                 woolColourCode = 8;
  160.             break;
  161.             case CYAN :
  162.                 woolColourCode = 9;
  163.             break;
  164.             case PURPLE :
  165.                 woolColourCode = 10;
  166.             break;
  167.             case BLUE :
  168.                 woolColourCode = 11;
  169.             break;
  170.             case BROWN :
  171.                 woolColourCode = 12;
  172.             break;
  173.             case GREEN :
  174.                 woolColourCode = 13;
  175.             break;
  176.             case RED :
  177.                 woolColourCode = 14;
  178.             break;
  179.             case BLACK :
  180.                 woolColourCode = 15;
  181.             break;
  182.             default:
  183.                 woolColourCode = 4; // default == yellow == 4
  184.             break;
  185.             }
  186.  
  187.         } catch (IOException ex)
  188.         {
  189.             logger.log(Level.SEVERE, "Could not load values from the RedstoneSponge config file!");
  190.         }
  191.  
  192.     }
  193.  
  194.     public void onDisable()
  195.     {
  196.         // TODO: Place any custom disable code here
  197.  
  198.         // NOTE: All registered events are automatically unregistered when a plugin is disabled
  199.  
  200.         // EXAMPLE: Custom code, here we just output some info so we can check all is well
  201.         PluginDescriptionFile pdfFile = this.getDescription();
  202. //      System.out.println("Goodbye world! " + pdfFile.getName() + " version " + pdfFile.getVersion() + " is now disabled!");
  203.         logger.log(Level.INFO, pdfFile.getName() + " version " + pdfFile.getVersion() + " is disabled!");
  204.     }
  205.  
  206.     public boolean isDebugging(final Player player)
  207.     {
  208.         if (debugees.containsKey(player))
  209.         {
  210.             return debugees.get(player);
  211.         }
  212.         else
  213.         {
  214.             return false;
  215.         }
  216.     }
  217.  
  218.     public void setDebugging(final Player player, final boolean value)
  219.     {
  220.         debugees.put(player, value);
  221.     }
  222.  
  223.     public boolean isWaterSpongeEnabled()
  224.     {
  225.         return spongeWater;
  226.     }
  227.  
  228.     public boolean isLavaSpongeEnabled()
  229.     {
  230.         return spongeLava;
  231.     }
  232.  
  233.     public int getRadius()
  234.     {
  235.         return spongeRadius;
  236.     }
  237.  
  238.     public boolean isRedstoneSponge()
  239.     {
  240.         return redstoneSponge;
  241.     }
  242.  
  243.     public boolean isConvertWhenPoweredEnabled()
  244.     {
  245.         return convertWhenPowered;
  246.     }
  247.  
  248.     public int getConvertID()
  249.     {
  250.         return convertID;
  251.     }
  252.  
  253.     public int getWoolColour()
  254.     {
  255.         return woolColourCode;
  256.     }
  257.  
  258.     public boolean isblockWhenPoweredEnabled()
  259.     {
  260.         return blockWhenPowered;
  261.     }
  262.  
  263.     private enum WoolColour
  264.     {
  265.     WHITE, ORANGE, MAGENTA, LIGHTBLUE, YELLOW, LIME, PINK, GRAY, LIGHTGRAY, CYAN, PURPLE, BLUE, BROWN, GREEN, RED, BLACK
  266.     }
  267.  
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement