Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.pookeythekid.MobCannon;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Random;
- import java.util.Set;
- import org.bukkit.ChatColor;
- import org.bukkit.Location;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Creature;
- import org.bukkit.entity.EntityType;
- import org.bukkit.entity.Player;
- import org.bukkit.permissions.Permission;
- import org.bukkit.plugin.PluginManager;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.scheduler.BukkitRunnable;
- /**
- *
- * @author pookeythekid
- * @version 0.0.1
- *
- */
- public class MobCannon implements CommandExecutor {
- public static JavaPlugin plugin;
- public static HashMap<String, EntityType> usedNames = new HashMap<String, EntityType>();
- /**
- * 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.
- *
- * @param plugin - Main plugin class.
- * @param config - Config file to use.
- */
- public MobCannon(JavaPlugin plugin) {
- Map<EntityType, Set<String>> map = new HashMap<>();
- Set<String> goodNames = new HashSet<>();
- map.put(EntityType.BAT, goodNames);
- map.put(EntityType.BLAZE, goodNames);
- map.put(EntityType.CAVE_SPIDER, goodNames);
- map.put(EntityType.CHICKEN, goodNames);
- map.put(EntityType.COW, goodNames);
- map.put(EntityType.CREEPER, goodNames);
- map.put(EntityType.ENDER_DRAGON, goodNames);
- map.put(EntityType.ENDERMAN, goodNames);
- map.put(EntityType.GHAST, goodNames);
- map.put(EntityType.GIANT, goodNames);
- map.put(EntityType.HORSE, goodNames);
- map.put(EntityType.IRON_GOLEM, goodNames);
- map.put(EntityType.MAGMA_CUBE, goodNames);
- map.put(EntityType.MUSHROOM_COW, goodNames);
- map.put(EntityType.OCELOT, goodNames);
- map.put(EntityType.PIG, goodNames);
- map.put(EntityType.PIG_ZOMBIE, goodNames);
- map.put(EntityType.SHEEP, goodNames);
- map.put(EntityType.SILVERFISH, goodNames);
- map.put(EntityType.SKELETON, goodNames);
- map.put(EntityType.SLIME, goodNames);
- map.put(EntityType.SNOWMAN, goodNames);
- map.put(EntityType.SPIDER, goodNames);
- map.put(EntityType.SQUID, goodNames);
- map.put(EntityType.VILLAGER, goodNames);
- map.put(EntityType.WITCH, goodNames);
- map.put(EntityType.WITHER, goodNames);
- map.put(EntityType.WOLF, goodNames);
- map.put(EntityType.ZOMBIE, goodNames);
- new MobCannon(plugin, map, true, true);
- }
- /**
- * Constructor that has input for alternate mob names and use of boss mobs and giants. Do NOT use null for any parameters.
- *
- * @param plugin - Main plugin class.
- * @param config - Config file to use.
- * @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.
- * @param bossMobs - Defines whether the plugin allows boss mobs (Wither and Ender Dragon) or not.
- * @param giants - Defines whether the plugin allows giants (hundred-foot-tall zombies) or not.
- */
- public MobCannon(JavaPlugin plugin, Map<EntityType, Set<String>> mobAliases, boolean bossMobs, boolean giants) {
- MobCannon.plugin = plugin;
- System.out.println(usedNames.toString());
- System.out.println(mobAliases.toString());
- HashMap<String, EntityType> tempMap = new HashMap<String, EntityType>();
- for (EntityType eType : mobAliases.keySet()) {
- if (!bossMobs && (eType.equals(EntityType.ENDER_DRAGON) || eType.equals(EntityType.WITHER)))
- continue;
- if (!giants && eType.equals(EntityType.GIANT))
- continue;
- for (String name : mobAliases.get(eType)) {
- if (!tempMap.containsKey(name.toLowerCase())) {
- tempMap.put(name.toLowerCase(), eType);
- }
- }
- if (!tempMap.containsKey(eType.toString().toLowerCase()))
- tempMap.put(eType.toString().toLowerCase(), eType);
- if (!tempMap.containsKey(eType.toString().replace("_", "").toLowerCase()))
- tempMap.put(eType.toString().replace("_", "").toLowerCase(), eType);
- }
- System.out.println(tempMap.toString());
- usedNames = tempMap;
- System.out.println(usedNames.toString());
- System.out.println(tempMap.toString());
- System.out.println(usedNames.toString());
- PluginManager pm = plugin.getServer().getPluginManager();
- Permission launchCommand = new Permission("mvpgadgets.launchmob");
- Permission randomLaunch = new Permission("mvpgadgets.launchmob.random");
- pm.addPermission(launchCommand);
- pm.addPermission(randomLaunch);
- pm.getPermission("mvpgadgets.launchmob").addParent(randomLaunch, true);
- }
- public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
- if (cmd.getName().equalsIgnoreCase("launchmob")) {
- if (!sender.hasPermission("mvpgadgets.launchmob")) {
- sender.sendMessage(ChatColor.RED + "You don't have permission to launch mobs.");
- return true;
- }
- if (!(sender instanceof Player))
- sender.sendMessage("Nope. You're a console.");
- else {
- if (args.length < 1)
- launchMob("", (Player) sender, usedNames);
- else if (args.length > 0)
- launchMob(args[0], (Player) sender, usedNames);
- }
- }
- return true;
- }
- public void launchMob(String mobName, Player p, HashMap<String, EntityType> map) {
- if (!mobName.isEmpty()) {
- if (inputNameValid(mobName, map)) {
- if (!p.hasPermission("mvpgadgets.launchmob." + getNameFrom(mobName, map))) {
- p.sendMessage(ChatColor.RED + "You don't have permission to launch a(n) " + mobName);
- }
- final Creature creature;
- switch (getNameFrom(mobName, map)) {
- case "bat":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.BAT);
- creature.setTarget(null);
- break;
- case "blaze":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.BLAZE);
- creature.setTarget(null);
- break;
- case "cave_spider":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.CAVE_SPIDER);
- creature.setTarget(null);
- break;
- case "chicken":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.CHICKEN);
- creature.setTarget(null);
- break;
- case "cow":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.COW);
- creature.setTarget(null);
- break;
- case "creeper":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.CREEPER);
- creature.setTarget(null);
- break;
- case "ender_dragon":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.ENDER_DRAGON);
- creature.setTarget(null);
- break;
- case "enderman":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.ENDERMAN);
- creature.setTarget(null);
- break;
- case "ghast":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.GHAST);
- creature.setTarget(null);
- break;
- case "giant":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.GIANT);
- creature.setTarget(null);
- break;
- case "horse":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.HORSE);
- creature.setTarget(null);
- break;
- case "iron_golem":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.BAT);
- creature.setTarget(null);
- break;
- case "magma_cube":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.MAGMA_CUBE);
- creature.setTarget(null);
- break;
- case "mushroom_cow":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.MUSHROOM_COW);
- creature.setTarget(null);
- break;
- case "ocelot":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.OCELOT);
- creature.setTarget(null);
- break;
- default:
- case "pig":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.PIG);
- creature.setTarget(null);
- break;
- case "pig_zombie":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.PIG_ZOMBIE);
- creature.setTarget(null);
- break;
- case "sheep":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.SHEEP);
- creature.setTarget(null);
- break;
- case "silverfish":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.SILVERFISH);
- creature.setTarget(null);
- break;
- case "skeleton":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.SKELETON);
- creature.setTarget(null);
- break;
- case "slime":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.SLIME);
- creature.setTarget(null);
- break;
- case "snowman":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.SNOWMAN);
- creature.setTarget(null);
- break;
- case "spider":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.SPIDER);
- creature.setTarget(null);
- break;
- case "squid":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.SQUID);
- creature.setTarget(null);
- break;
- case "villager":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.VILLAGER);
- creature.setTarget(null);
- break;
- case "witch":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.WITCH);
- creature.setTarget(null);
- break;
- case "wither":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.WITHER);
- creature.setTarget(null);
- break;
- case "wolf":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.WOLF);
- creature.setTarget(null);
- break;
- case "zombie":
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), EntityType.ZOMBIE);
- creature.setTarget(null);
- break;
- }
- creature.setVelocity(p.getLocation().getDirection().multiply(3));
- new BukkitRunnable() {
- @Override
- public void run() {
- Location loc = creature.getLocation();
- creature.getWorld().createExplosion(loc.getX(), loc.getY(), loc.getZ(), 4F, false, false);
- creature.remove();
- }
- }.runTaskLater(plugin, 20);
- }
- else
- p.sendMessage(ChatColor.RED + "Mob name '" + mobName + "' is invalid.");
- }
- else {
- if (!p.hasPermission("mvpgadgets.launchmob.random")) {
- p.sendMessage(ChatColor.RED + "You must specify a creature to launch.");
- return;
- }
- ArrayList<EntityType> clist = new ArrayList<EntityType>();
- System.out.println(map.values().toString());
- System.out.println(map.toString());
- for (EntityType eType : map.values()) {
- if (!clist.contains(eType))
- clist.add(eType);
- }
- System.out.println(clist.toString());
- final Creature creature;
- creature = (Creature) p.getWorld().spawnEntity(p.getLocation(), clist.get(new Random().nextInt(clist.size())));
- creature.setTarget(null);
- creature.setVelocity(p.getLocation().getDirection().multiply(3));
- new BukkitRunnable() {
- @Override
- public void run() {
- Location loc = creature.getLocation();
- creature.getWorld().createExplosion(loc.getX(), loc.getY(), loc.getZ(), 4F, false, false);
- creature.remove();
- }
- }.runTaskLater(plugin, 20);
- }
- }
- public boolean inputNameValid(String name, HashMap<String, EntityType> map) {
- boolean valid = false;
- if (map.containsKey(name.toLowerCase()))
- valid = true;
- return valid;
- }
- public String getNameFrom(String name, HashMap<String, EntityType> map) {
- if (inputNameValid(name, map)) {
- return map.get(name.toLowerCase()).toString().toLowerCase();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement