kmccmk9

ZombieSiege

Jul 13th, 2013
36
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.kmccmk9.ZombieSiege;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.util.Properties;
  8. import java.util.Random;
  9.  
  10. import org.bukkit.Bukkit;
  11. import org.bukkit.ChatColor;
  12. import org.bukkit.Location;
  13. import org.bukkit.Server;
  14. import org.bukkit.World;
  15. import org.bukkit.block.Block;
  16. import org.bukkit.command.Command;
  17. import org.bukkit.command.CommandSender;
  18. import org.bukkit.entity.Creature;
  19. import org.bukkit.entity.CreatureType;
  20. import org.bukkit.entity.Entity;
  21. import org.bukkit.entity.EntityType;
  22. import org.bukkit.entity.LivingEntity;
  23. import org.bukkit.entity.Player;
  24. import org.bukkit.plugin.PluginDescriptionFile;
  25. import org.bukkit.plugin.java.JavaPlugin;
  26. import org.bukkit.scoreboard.DisplaySlot;
  27. import org.bukkit.scoreboard.Objective;
  28. import org.bukkit.scoreboard.Scoreboard;
  29. import org.bukkit.scoreboard.ScoreboardManager;
  30. import org.bukkit.scoreboard.Team;
  31.  
  32. @SuppressWarnings({ "deprecation", "deprecation" })
  33. public class ZombieSiege extends JavaPlugin {
  34.     //Global Variables
  35.     Server server;
  36.     static String mainDirectory = "plugins/ZombieSiege"; //sets the main directory for easy reference
  37.     static File settings = new File(mainDirectory + File.separator + "plugin.settings"); //the file separator is the / sign, this will create a new Zones.dat files in the mainDirectory variable listed above, if no Zones directory exists then it will automatically be made along with the file.
  38.     static Properties settingsproperty = new Properties(); //creates a new properties file
  39.     Location tower1=new Location(null,5.0,5.0,5.0);
  40.     Location tower2=new Location(null,5.0,5.0,5.0);
  41.     Location tower3=new Location(null,5.0,5.0,5.0);
  42.     Location tower4=new Location(null,5.0,5.0,5.0);
  43.     Location tower5=new Location(null,5.0,5.0,5.0);
  44.     double tower1x=5;
  45.     double tower2x=5;
  46.     double tower3x=5;
  47.     double tower4x=5;
  48.     double tower5x=5;
  49.     double tower1y=5;
  50.     double tower2y=5;
  51.     double tower3y=5;
  52.     double tower4y=5;
  53.     double tower5y=5;
  54.     double tower1z=5;
  55.     double tower2z=5;
  56.     double tower3z=5;
  57.     double tower4z=5;
  58.     double tower5z=5;
  59.     Location tower = null;
  60.     Entity chicken;
  61.     double x,y,z;
  62.     double areaRadius = 20; //Or any radius you want
  63.     double minRadius = 10; // This would give me a radius between 500 and 1000
  64.     double t = Math.random() * Math.PI;
  65.     double radius = Math.random()*(areaRadius - minRadius) + minRadius;
  66.     ScoreboardManager manager;
  67.     Team team;
  68.     Objective objective;
  69.     Scoreboard board;
  70.    
  71.     //Enable
  72.     public void onEnable() {
  73.         server = this.getServer();
  74.         manager = Bukkit.getScoreboardManager();
  75.         board = manager.getNewScoreboard();
  76.         team = board.registerNewTeam("Defenders:");
  77.         objective = board.registerNewObjective("zombies", "totalKillCount");
  78.         objective.setDisplaySlot(DisplaySlot.SIDEBAR);
  79.         objective.setDisplayName("Zombies:");
  80.         PluginDescriptionFile pdfFile = this.getDescription();
  81.         System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
  82.         new File(mainDirectory).mkdir(); //makes the Zones directory/folder in the plugins directory
  83.         if(!settings.exists()){ //Checks to see if the zones file exists, defined above, if it doesn't exist then it will do the following. the ! turns the whole statement around, checking that the file doesn't exist instead of if it exists.
  84.             try { //try catch clause explained below in tutorial
  85.                 settings.createNewFile(); //creates the file zones.dat
  86.                 FileOutputStream out = new FileOutputStream(settings); //creates a new output steam needed to write to the file
  87.                 settingsproperty.put("tower1x", "0"); //put the property ZoneCount with a value of 0 into the properties file, this will show up as ZoneCount=0 in the properties file.
  88.                 settingsproperty.put("tower1y", "0");
  89.                 settingsproperty.put("tower1z", "0");
  90.                 settingsproperty.put("tower2x", "0");
  91.                 settingsproperty.put("tower2y", "0");
  92.                 settingsproperty.put("tower2z", "0");
  93.                 settingsproperty.put("tower3x", "0");
  94.                 settingsproperty.put("tower3y", "0");
  95.                 settingsproperty.put("tower3z", "0");
  96.                 settingsproperty.put("tower4x", "0");
  97.                 settingsproperty.put("tower4y", "0");
  98.                 settingsproperty.put("tower4z", "0");
  99.                 settingsproperty.put("tower5x", "0");
  100.                 settingsproperty.put("tower5y", "0");
  101.                 settingsproperty.put("tower5z", "0");
  102.                 settingsproperty.store(out, "Feel free to edit this config!"); //You need this line! It stores what you just put into the file and adds a comment.
  103.                 out.flush();  //Explained below in tutorial
  104.                 out.close(); //Closes the output stream as it is not needed anymore.
  105.             } catch (IOException ex) {
  106.                 ex.printStackTrace(); //explained below.
  107.             }
  108.  
  109.         } else {
  110.            
  111.             settingsLoad();
  112.  
  113.         }
  114.     }
  115.     //Disable
  116.     public void onDisable() {
  117.         PluginDescriptionFile pdfFile = this.getDescription();
  118.         System.out.println( pdfFile.getName() + " is disabled!" );
  119.     }
  120.     //ReadFile
  121.     public void settingsLoad() {
  122.         try {
  123.             FileInputStream in = new FileInputStream(settings);
  124.             settingsproperty.load(in);
  125.             tower1x = Double.valueOf(settingsproperty.getProperty("tower1x"));
  126.             tower2x = Double.valueOf(settingsproperty.getProperty("tower2x"));
  127.             tower3x = Double.valueOf(settingsproperty.getProperty("tower3x"));
  128.             tower4x = Double.valueOf(settingsproperty.getProperty("tower4x"));
  129.             tower5x = Double.valueOf(settingsproperty.getProperty("tower5x"));
  130.             tower1y = Double.valueOf(settingsproperty.getProperty("tower1y"));
  131.             tower2y = Double.valueOf(settingsproperty.getProperty("tower2y"));
  132.             tower3y = Double.valueOf(settingsproperty.getProperty("tower3y"));
  133.             tower4y = Double.valueOf(settingsproperty.getProperty("tower4y"));
  134.             tower5y = Double.valueOf(settingsproperty.getProperty("tower5y"));
  135.             tower1z = Double.valueOf(settingsproperty.getProperty("tower1z"));
  136.             tower2z = Double.valueOf(settingsproperty.getProperty("tower2z"));
  137.             tower3z = Double.valueOf(settingsproperty.getProperty("tower3z"));
  138.             tower4z = Double.valueOf(settingsproperty.getProperty("tower4z"));
  139.             tower5z = Double.valueOf(settingsproperty.getProperty("tower5z"));
  140.             tower1.setX(tower1x);
  141.             tower1.setY(tower1y);
  142.             tower1.setZ(tower1z);
  143.             tower2.setX(tower2x);
  144.             tower2.setY(tower2y);
  145.             tower2.setZ(tower2z);
  146.             tower3.setX(tower3x);
  147.             tower3.setY(tower3y);
  148.             tower3.setZ(tower3z);
  149.             tower4.setX(tower4x);
  150.             tower4.setY(tower4y);
  151.             tower4.setZ(tower4z);
  152.             tower5.setX(tower5x);
  153.             tower5.setY(tower5y);
  154.             tower5.setZ(tower5z);
  155.             in.close();
  156.         } catch (IOException ex)
  157.         {
  158.             ex.printStackTrace();
  159.         }
  160.     }
  161.    
  162.     public boolean onCommand(final CommandSender sender, Command command, String commandLabel, String[] args) {
  163.         Player player = (Player) sender;
  164.         if (commandLabel.equalsIgnoreCase("zs")) {
  165.             if (args.length == 0) {
  166.                 sender.sendMessage(ChatColor.BLUE + "ZombieSiege Help:\n----------------\n1. /zs start - Starts the siege!\n2. /zs stop - Stops teh siege.\n3. /zs set1 - Sets block you are looking at as tower 1 location.\n4. /zs set2 - Sets block you are looking at as tower 2 location.\n5. /zs set3 - Sets the block you are looking at as tower 3 location.\n6. /zs set4 - Sets the block you are looking at as tower 4.\n7. /zs set5 - Sets the block you are looking at as tower 5.");
  167.             }
  168.             else if (args.length == 1) {
  169.                 if (args[0].equalsIgnoreCase("set1")) {
  170.                     Block b = player.getTargetBlock(null,200);
  171.                     tower1.setX(b.getX());
  172.                     tower1.setY(b.getY());
  173.                     tower1.setZ(b.getZ());
  174.                     settingsproperty.setProperty("tower1x", String.valueOf(b.getX()));
  175.                     settingsproperty.setProperty("tower1y", String.valueOf(b.getY()));
  176.                     settingsproperty.setProperty("tower1z", String.valueOf(b.getZ()));
  177.                     player.sendMessage("Tower 1 has been set.");
  178.                 }
  179.                 else if (args[0].equalsIgnoreCase("join")) {
  180.                     team.addPlayer(player);
  181.                     team.setPrefix("ZS-");
  182.                     team.setAllowFriendlyFire(false);
  183.                     player.setScoreboard(board);
  184.                     player.sendMessage(ChatColor.YELLOW + "You have been adde to the team!");
  185.                 }
  186.                 else if (args[0].equalsIgnoreCase("set2")) {
  187.                     Block b = player.getTargetBlock(null,200);
  188.                     tower2.setX(b.getX());
  189.                     tower2.setY(b.getY());
  190.                     tower2.setZ(b.getZ());
  191.                     settingsproperty.setProperty("tower2x", String.valueOf(b.getX()));
  192.                     settingsproperty.setProperty("tower2y", String.valueOf(b.getY()));
  193.                     settingsproperty.setProperty("tower2z", String.valueOf(b.getZ()));
  194.                     player.sendMessage("Tower 2 has been set.");
  195.                 }
  196.                 else if (args[0].equalsIgnoreCase("set3")) {
  197.                     Block b = player.getTargetBlock(null,200);
  198.                     tower3.setX(b.getX());
  199.                     tower3.setY(b.getY());
  200.                     tower3.setZ(b.getZ());
  201.                     settingsproperty.setProperty("tower3x", String.valueOf(b.getX()));
  202.                     settingsproperty.setProperty("tower3y", String.valueOf(b.getY()));
  203.                     settingsproperty.setProperty("tower3z", String.valueOf(b.getZ()));
  204.                     player.sendMessage("Tower 3 has been set.");
  205.                 }
  206.                 else if (args[0].equalsIgnoreCase("set4")) {
  207.                     Block b = player.getTargetBlock(null,200);
  208.                     tower4.setX(b.getX());
  209.                     tower4.setY(b.getY());
  210.                     tower4.setZ(b.getZ());
  211.                     settingsproperty.setProperty("tower4x", String.valueOf(b.getX()));
  212.                     settingsproperty.setProperty("tower4y", String.valueOf(b.getY()));
  213.                     settingsproperty.setProperty("tower4z", String.valueOf(b.getZ()));
  214.                     player.sendMessage("Tower 4 has been set.");
  215.                 }
  216.                 else if (args[0].equalsIgnoreCase("set5")) {
  217.                     Block b = player.getTargetBlock(null,200);
  218.                     tower5.setX(b.getX());
  219.                     tower5.setY(b.getY());
  220.                     tower5.setZ(b.getZ());
  221.                     settingsproperty.setProperty("tower5x", String.valueOf(b.getX()));
  222.                     settingsproperty.setProperty("tower5y", String.valueOf(b.getY()));
  223.                     settingsproperty.setProperty("tower5z", String.valueOf(b.getZ()));
  224.                     player.sendMessage("Tower 5 has been set.");
  225.                 }
  226.                 else if (args[0].equalsIgnoreCase("start")) {
  227.                     for(Player online : Bukkit.getOnlinePlayers()){
  228.                         board.resetScores(online);
  229.                         }
  230.                     server.broadcastMessage("ZombieSiege will be starting!");
  231.                     //Generate Random Number
  232.                     Random rn = new Random();
  233.                     int i = rn.nextInt(5) + 1;
  234.                     server.broadcastMessage(ChatColor.YELLOW + "They are attacking tower #" + i);
  235.                     //Spawn mobs
  236.                     areaRadius = 15; //Or any radius you want
  237.                     minRadius = 10; // This would give me a radius between 500 and 1000
  238.                     t = Math.random() * Math.PI;
  239.                     radius = Math.random()*(areaRadius - minRadius) + minRadius;
  240.                     x = Math.cos(t) * radius;
  241.                     y = Math.sin(t) * radius;
  242.                     z = Math.cos(t) * radius;
  243.                     switch (i) {
  244.                     case 1: tower = tower1.clone();
  245.                     break;
  246.                     case 2: tower = tower2.clone();
  247.                     break;
  248.                     case 3: tower = tower3.clone();
  249.                     break;
  250.                     case 4: tower = tower4.clone();
  251.                     break;
  252.                     case 5: tower = tower5.clone();
  253.                     break;
  254.                     default: server.broadcastMessage("Something went wrong!");
  255.                     break;
  256.                     }
  257.                     final World world = player.getWorld();
  258.                     chicken = world.spawnEntity(tower, EntityType.CHICKEN);
  259.                     entityListener eListener = new entityListener(server, chicken);
  260.                     Bukkit.getServer().getPluginManager().registerEvents(eListener , this);
  261.                     server.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
  262.                         public void run() {
  263.                             Location to = tower;
  264.                             to.add(x, y, z); // Its just a matter of adding the x, y and z
  265.                             int i = 0;
  266.                             while (i <= 4) {
  267.                             Entity zombie = world.spawnEntity(to, EntityType.PIG_ZOMBIE);
  268.                             ((Creature) zombie).setTarget((LivingEntity) chicken);
  269.                             areaRadius = 15; //Or any radius you want
  270.                             minRadius = 10; // This would give me a radius between 500 and 1000
  271.                             t = Math.random() * Math.PI;
  272.                             radius = Math.random()*(areaRadius - minRadius) + minRadius;
  273.                             x = Math.cos(t) * radius;
  274.                             y = Math.sin(t) * radius;
  275.                             z = Math.cos(t) * radius;
  276.                             i++;
  277.                             }
  278.                         }
  279.                     }, 5 * 20L);
  280.                     }
  281.                 else
  282.                     player.sendMessage(ChatColor.RED + "Arguments mismatch. Use the /zs command to see all options");
  283.             }
  284.             else
  285.                 sender.sendMessage(ChatColor.RED + "Arguments mismatch. Use the /zs command to see all options");
  286.         }
  287.        
  288.         return true;
  289.        
  290.     }
  291.  
  292. }
RAW Paste Data