Advertisement
Guest User

MobCannon class

a guest
Dec 18th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.44 KB | None | 0 0
  1. package me.pookeythekid.MobCannon;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.Map;
  7. import java.util.Random;
  8. import java.util.Set;
  9.  
  10. import org.bukkit.ChatColor;
  11. import org.bukkit.Location;
  12. import org.bukkit.command.Command;
  13. import org.bukkit.command.CommandExecutor;
  14. import org.bukkit.command.CommandSender;
  15. import org.bukkit.entity.Creature;
  16. import org.bukkit.entity.EntityType;
  17. import org.bukkit.entity.Player;
  18. import org.bukkit.permissions.Permission;
  19. import org.bukkit.plugin.PluginManager;
  20. import org.bukkit.plugin.java.JavaPlugin;
  21. import org.bukkit.scheduler.BukkitRunnable;
  22.  
  23. /**
  24.  *
  25.  * @author pookeythekid
  26.  * @version 0.0.1
  27.  *
  28.  */
  29. public class MobCannon implements CommandExecutor {
  30.  
  31.     public static JavaPlugin plugin;
  32.  
  33.     public static HashMap<String, EntityType> usedNames = new HashMap<String, EntityType>();
  34.  
  35.  
  36.     /**
  37.      * Simplest constructor. Will add EntityTypes and mob aliases to the class by itself, instead of having them inputted. Use the other constructor to customize mob aliases.
  38.      *
  39.      * @param plugin - Main plugin class.
  40.      * @param config - Config file to use.
  41.      */
  42.     public MobCannon(JavaPlugin plugin) {
  43.  
  44.         Map<EntityType, Set<String>> map = new HashMap<>();
  45.  
  46.         Set<String> goodNames = new HashSet<>();
  47.  
  48.         map.put(EntityType.BAT, goodNames);
  49.  
  50.         map.put(EntityType.BLAZE, goodNames);
  51.  
  52.         map.put(EntityType.CAVE_SPIDER, goodNames);
  53.  
  54.         map.put(EntityType.CHICKEN, goodNames);
  55.  
  56.         map.put(EntityType.COW, goodNames);
  57.  
  58.         map.put(EntityType.CREEPER, goodNames);
  59.  
  60.         map.put(EntityType.ENDER_DRAGON, goodNames);
  61.  
  62.         map.put(EntityType.ENDERMAN, goodNames);
  63.  
  64.         map.put(EntityType.GHAST, goodNames);
  65.  
  66.         map.put(EntityType.GIANT, goodNames);
  67.  
  68.         map.put(EntityType.HORSE, goodNames);
  69.  
  70.         map.put(EntityType.IRON_GOLEM, goodNames);
  71.  
  72.         map.put(EntityType.MAGMA_CUBE, goodNames);
  73.  
  74.         map.put(EntityType.MUSHROOM_COW, goodNames);
  75.  
  76.         map.put(EntityType.OCELOT, goodNames);
  77.  
  78.         map.put(EntityType.PIG, goodNames);
  79.  
  80.         map.put(EntityType.PIG_ZOMBIE, goodNames);
  81.  
  82.         map.put(EntityType.SHEEP, goodNames);
  83.  
  84.         map.put(EntityType.SILVERFISH, goodNames);
  85.  
  86.         map.put(EntityType.SKELETON, goodNames);
  87.  
  88.         map.put(EntityType.SLIME, goodNames);
  89.  
  90.         map.put(EntityType.SNOWMAN, goodNames);
  91.  
  92.         map.put(EntityType.SPIDER, goodNames);
  93.  
  94.         map.put(EntityType.SQUID, goodNames);
  95.  
  96.         map.put(EntityType.VILLAGER, goodNames);
  97.  
  98.         map.put(EntityType.WITCH, goodNames);
  99.  
  100.         map.put(EntityType.WITHER, goodNames);
  101.  
  102.         map.put(EntityType.WOLF, goodNames);
  103.  
  104.         map.put(EntityType.ZOMBIE, goodNames);
  105.  
  106.  
  107.         new MobCannon(plugin, map, true, true);
  108.  
  109.     }
  110.  
  111.     /**
  112.      * Constructor that has input for alternate mob names and use of boss mobs and giants. Do NOT use null for any parameters.
  113.      *
  114.      * @param plugin - Main plugin class.
  115.      * @param config - Config file to use.
  116.      * @param mobAliases - Keys of actual mobs, values of acceptable mob names. An empty Set<String> will work, since the default names (i.e.: IRON_GOLEM will look like "iron_golem" and "irongolem") are generated regardless.
  117.      * @param bossMobs - Defines whether the plugin allows boss mobs (Wither and Ender Dragon) or not.
  118.      * @param giants - Defines whether the plugin allows giants (hundred-foot-tall zombies) or not.
  119.      */
  120.     public MobCannon(JavaPlugin plugin, Map<EntityType, Set<String>> mobAliases, boolean bossMobs, boolean giants) {
  121.  
  122.         MobCannon.plugin = plugin;
  123.        
  124.         System.out.println(usedNames.toString());
  125.  
  126.         System.out.println(mobAliases.toString());
  127.        
  128.         HashMap<String, EntityType> tempMap = new HashMap<String, EntityType>();
  129.  
  130.         for (EntityType eType : mobAliases.keySet()) {
  131.  
  132.             if (!bossMobs && (eType.equals(EntityType.ENDER_DRAGON) || eType.equals(EntityType.WITHER)))
  133.  
  134.                 continue;
  135.  
  136.             if (!giants && eType.equals(EntityType.GIANT))
  137.  
  138.                 continue;
  139.  
  140.             for (String name : mobAliases.get(eType)) {
  141.  
  142.                 if (!tempMap.containsKey(name.toLowerCase())) {
  143.  
  144.                     tempMap.put(name.toLowerCase(), eType);
  145.  
  146.                 }
  147.  
  148.             }
  149.  
  150.             if (!tempMap.containsKey(eType.toString().toLowerCase()))
  151.  
  152.                 tempMap.put(eType.toString().toLowerCase(), eType);
  153.  
  154.             if (!tempMap.containsKey(eType.toString().replace("_", "").toLowerCase()))
  155.  
  156.                 tempMap.put(eType.toString().replace("_", "").toLowerCase(), eType);
  157.  
  158.         }
  159.        
  160.         System.out.println(tempMap.toString());
  161.        
  162.         usedNames = tempMap;
  163.        
  164.         System.out.println(usedNames.toString());
  165.        
  166.         System.out.println(tempMap.toString());
  167.  
  168.         System.out.println(usedNames.toString());
  169.  
  170.  
  171.         PluginManager pm = plugin.getServer().getPluginManager();
  172.  
  173.         Permission launchCommand = new Permission("mvpgadgets.launchmob");
  174.  
  175.         Permission randomLaunch = new Permission("mvpgadgets.launchmob.random");
  176.  
  177.         pm.addPermission(launchCommand);
  178.  
  179.         pm.addPermission(randomLaunch);
  180.  
  181.         pm.getPermission("mvpgadgets.launchmob").addParent(randomLaunch, true);
  182.  
  183.  
  184.     }
  185.  
  186.  
  187.     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  188.  
  189.         if (cmd.getName().equalsIgnoreCase("launchmob")) {
  190.  
  191.             if (!sender.hasPermission("mvpgadgets.launchmob")) {
  192.  
  193.                 sender.sendMessage(ChatColor.RED + "You don't have permission to launch mobs.");
  194.  
  195.                 return true;
  196.  
  197.             }
  198.  
  199.             if (!(sender instanceof Player))
  200.  
  201.                 sender.sendMessage("Nope. You're a console.");
  202.  
  203.             else {
  204.  
  205.                 if (args.length < 1)
  206.  
  207.                     launchMob("", (Player) sender, usedNames);
  208.  
  209.                 else if (args.length > 0)
  210.  
  211.                     launchMob(args[0], (Player) sender, usedNames);
  212.  
  213.             }
  214.  
  215.         }
  216.  
  217.         return true;
  218.  
  219.     }
  220.  
  221.  
  222.     public void launchMob(String mobName, Player p, HashMap<String, EntityType> map) {
  223.  
  224.         if (!mobName.isEmpty()) {
  225.  
  226.             if (inputNameValid(mobName, map)) {
  227.  
  228.                 if (!p.hasPermission("mvpgadgets.launchmob." + getNameFrom(mobName, map))) {
  229.  
  230.                     p.sendMessage(ChatColor.RED + "You don't have permission to launch a(n) " + mobName);
  231.  
  232.                 }
  233.  
  234.                 final Creature creature;
  235.  
  236.                 switch (getNameFrom(mobName, map)) {
  237.  
  238.                 case "bat":
  239.  
  240.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.BAT);
  241.  
  242.                     creature.setTarget(null);
  243.  
  244.                     break;
  245.  
  246.                 case "blaze":
  247.  
  248.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.BLAZE);
  249.  
  250.                     creature.setTarget(null);
  251.  
  252.                     break;
  253.  
  254.                 case "cave_spider":
  255.  
  256.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.CAVE_SPIDER);
  257.  
  258.                     creature.setTarget(null);
  259.  
  260.                     break;
  261.  
  262.                 case "chicken":
  263.  
  264.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.CHICKEN);
  265.  
  266.                     creature.setTarget(null);
  267.  
  268.                     break;
  269.  
  270.                 case "cow":
  271.  
  272.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.COW);
  273.  
  274.                     creature.setTarget(null);
  275.  
  276.                     break;
  277.  
  278.                 case "creeper":
  279.  
  280.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.CREEPER);
  281.  
  282.                     creature.setTarget(null);
  283.  
  284.                     break;
  285.  
  286.                 case "ender_dragon":
  287.  
  288.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.ENDER_DRAGON);
  289.  
  290.                     creature.setTarget(null);
  291.  
  292.                     break;
  293.  
  294.                 case "enderman":
  295.  
  296.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.ENDERMAN);
  297.  
  298.                     creature.setTarget(null);
  299.  
  300.                     break;
  301.  
  302.                 case "ghast":
  303.  
  304.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.GHAST);
  305.  
  306.                     creature.setTarget(null);
  307.  
  308.                     break;
  309.  
  310.                 case "giant":
  311.  
  312.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.GIANT);
  313.  
  314.                     creature.setTarget(null);
  315.  
  316.                     break;
  317.  
  318.                 case "horse":
  319.  
  320.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.HORSE);
  321.  
  322.                     creature.setTarget(null);
  323.  
  324.                     break;
  325.  
  326.                 case "iron_golem":
  327.  
  328.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.BAT);
  329.  
  330.                     creature.setTarget(null);
  331.  
  332.                     break;
  333.  
  334.                 case "magma_cube":
  335.  
  336.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.MAGMA_CUBE);
  337.  
  338.                     creature.setTarget(null);
  339.  
  340.                     break;
  341.  
  342.                 case "mushroom_cow":
  343.  
  344.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.MUSHROOM_COW);
  345.  
  346.                     creature.setTarget(null);
  347.  
  348.                     break;
  349.  
  350.                 case "ocelot":
  351.  
  352.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.OCELOT);
  353.  
  354.                     creature.setTarget(null);
  355.  
  356.                     break;
  357.  
  358.                 default:
  359.                 case "pig":
  360.  
  361.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.PIG);
  362.  
  363.                     creature.setTarget(null);
  364.  
  365.                     break;
  366.  
  367.                 case "pig_zombie":
  368.  
  369.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.PIG_ZOMBIE);
  370.  
  371.                     creature.setTarget(null);
  372.  
  373.                     break;
  374.  
  375.                 case "sheep":
  376.  
  377.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.SHEEP);
  378.  
  379.                     creature.setTarget(null);
  380.  
  381.                     break;
  382.  
  383.                 case "silverfish":
  384.  
  385.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.SILVERFISH);
  386.  
  387.                     creature.setTarget(null);
  388.  
  389.                     break;
  390.  
  391.                 case "skeleton":
  392.  
  393.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.SKELETON);
  394.  
  395.                     creature.setTarget(null);
  396.  
  397.                     break;
  398.  
  399.                 case "slime":
  400.  
  401.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.SLIME);
  402.  
  403.                     creature.setTarget(null);
  404.  
  405.                     break;
  406.  
  407.                 case "snowman":
  408.  
  409.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.SNOWMAN);
  410.  
  411.                     creature.setTarget(null);
  412.  
  413.                     break;
  414.  
  415.                 case "spider":
  416.  
  417.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.SPIDER);
  418.  
  419.                     creature.setTarget(null);
  420.  
  421.                     break;
  422.  
  423.                 case "squid":
  424.  
  425.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.SQUID);
  426.  
  427.                     creature.setTarget(null);
  428.  
  429.                     break;
  430.  
  431.                 case "villager":
  432.  
  433.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.VILLAGER);
  434.  
  435.                     creature.setTarget(null);
  436.  
  437.                     break;
  438.  
  439.                 case "witch":
  440.  
  441.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.WITCH);
  442.  
  443.                     creature.setTarget(null);
  444.  
  445.                     break;
  446.  
  447.                 case "wither":
  448.  
  449.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.WITHER);
  450.  
  451.                     creature.setTarget(null);
  452.  
  453.                     break;
  454.  
  455.                 case "wolf":
  456.  
  457.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.WOLF);
  458.  
  459.                     creature.setTarget(null);
  460.  
  461.                     break;
  462.  
  463.                 case "zombie":
  464.  
  465.                     creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.ZOMBIE);
  466.  
  467.                     creature.setTarget(null);
  468.  
  469.                     break;
  470.  
  471.                 }
  472.  
  473.                 creature.setVelocity(p.getLocation().getDirection().multiply(3));
  474.  
  475.                 new BukkitRunnable() {
  476.  
  477.                     @Override
  478.                     public void run() {
  479.  
  480.                         Location loc = creature.getLocation();
  481.  
  482.                         creature.getWorld().createExplosion(loc.getX(), loc.getY(), loc.getZ(), 4F, false, false);
  483.  
  484.                         creature.remove();
  485.  
  486.                     }
  487.  
  488.                 }.runTaskLater(plugin, 20);
  489.  
  490.             }
  491.  
  492.             else
  493.  
  494.                 p.sendMessage(ChatColor.RED + "Mob name '" + mobName + "' is invalid.");
  495.  
  496.         }
  497.  
  498.         else {
  499.  
  500.             if (!p.hasPermission("mvpgadgets.launchmob.random")) {
  501.  
  502.                 p.sendMessage(ChatColor.RED + "You must specify a creature to launch.");
  503.  
  504.                 return;
  505.  
  506.             }
  507.  
  508.             ArrayList<EntityType> clist = new ArrayList<EntityType>();
  509.            
  510.             System.out.println(map.values().toString());
  511.            
  512.             System.out.println(map.toString());
  513.  
  514.             for (EntityType eType : map.values()) {
  515.  
  516.                 if (!clist.contains(eType))
  517.  
  518.                     clist.add(eType);
  519.  
  520.             }
  521.            
  522.             System.out.println(clist.toString());
  523.  
  524.             final Creature creature;
  525.  
  526.             creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), clist.get(new Random().nextInt(clist.size())));
  527.  
  528.             creature.setTarget(null);
  529.  
  530.             creature.setVelocity(p.getLocation().getDirection().multiply(3));
  531.  
  532.             new BukkitRunnable() {
  533.  
  534.                 @Override
  535.                 public void run() {
  536.  
  537.                     Location loc = creature.getLocation();
  538.  
  539.                     creature.getWorld().createExplosion(loc.getX(), loc.getY(), loc.getZ(), 4F, false, false);
  540.  
  541.                     creature.remove();
  542.  
  543.                 }
  544.  
  545.             }.runTaskLater(plugin, 20);
  546.  
  547.         }
  548.  
  549.     }
  550.  
  551.     public boolean inputNameValid(String name, HashMap<String, EntityType> map) {
  552.  
  553.         boolean valid = false;
  554.  
  555.         if (map.containsKey(name.toLowerCase()))
  556.  
  557.             valid = true;
  558.  
  559.         return valid;
  560.  
  561.     }
  562.  
  563.     public String getNameFrom(String name, HashMap<String, EntityType> map) {
  564.  
  565.         if (inputNameValid(name, map)) {
  566.  
  567.             return map.get(name.toLowerCase()).toString().toLowerCase();
  568.  
  569.         }
  570.  
  571.         return null;
  572.  
  573.     }
  574.  
  575. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement