Search in sources :

Example 6 with Ageable

use of org.bukkit.entity.Ageable in project Citizens2 by CitizensDev.

the class NPCCommands method age.

@Command(aliases = { "npc" }, usage = "age [age] (-l)", desc = "Set the age of a NPC", help = Messages.COMMAND_AGE_HELP, flags = "l", modifiers = { "age" }, min = 1, max = 2, permission = "citizens.npc.age")
public void age(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    if (!npc.isSpawned() || (!(npc.getEntity() instanceof Ageable) && !(npc.getEntity() instanceof Zombie)))
        throw new CommandException(Messages.MOBTYPE_CANNOT_BE_AGED, npc.getName());
    Age trait = npc.getTrait(Age.class);
    boolean toggleLock = args.hasFlag('l');
    if (toggleLock) {
        Messaging.sendTr(sender, trait.toggle() ? Messages.AGE_LOCKED : Messages.AGE_UNLOCKED);
    }
    if (args.argsLength() <= 1) {
        if (!toggleLock)
            trait.describe(sender);
        return;
    }
    int age = 0;
    try {
        age = args.getInteger(1);
        if (age > 0) {
            throw new CommandException(Messages.INVALID_AGE);
        }
        Messaging.sendTr(sender, Messages.AGE_SET_NORMAL, npc.getName(), age);
    } catch (NumberFormatException ex) {
        if (args.getString(1).equalsIgnoreCase("baby")) {
            age = -24000;
            Messaging.sendTr(sender, Messages.AGE_SET_BABY, npc.getName());
        } else if (args.getString(1).equalsIgnoreCase("adult")) {
            age = 0;
            Messaging.sendTr(sender, Messages.AGE_SET_ADULT, npc.getName());
        } else
            throw new CommandException(Messages.INVALID_AGE);
    }
    trait.setAge(age);
}
Also used : Zombie(org.bukkit.entity.Zombie) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) Ageable(org.bukkit.entity.Ageable) Age(net.citizensnpcs.trait.Age) Command(net.citizensnpcs.api.command.Command)

Example 7 with Ageable

use of org.bukkit.entity.Ageable in project CommandHelper by EngineHub.

the class BukkitConvertor method BukkitGetCorrectEntity.

// /**
// * We don't want to allow scripts to clear other plugin's tasks
// * on accident, so only ids registered through our interface
// * can also be cancelled.
// */
// private static final Set<Integer> validIDs = new TreeSet<Integer>();
// 
// @Override
// public synchronized int SetFutureRunnable(DaemonManager dm, long ms, Runnable r) {
// int id = Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(CommandHelperPlugin.self, r, Static.msToTicks(ms));
// validIDs.add(id);
// return id;
// }
// 
// @Override
// public synchronized int SetFutureRepeater(DaemonManager dm, long ms, long initialDelay, Runnable r){
// int id = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(CommandHelperPlugin.self, r, Static.msToTicks(initialDelay), Static.msToTicks(ms));
// validIDs.add(id);
// return id;
// }
// 
// @Override
// public synchronized void ClearAllRunnables() {
// //Doing cancelTasks apparently does not work, so let's just manually cancel each task, which does appear to work.
// //Anyways, it's better that way anyhow, because we actually remove IDs from validIDs that way.
// //((BukkitMCServer)Static.getServer()).__Server().getScheduler().cancelTasks(CommandHelperPlugin.self);
// Set<Integer> ids = new TreeSet<Integer>(validIDs);
// for(int id : ids){
// try{
// //If this doesn't work, it shouldn't kill everything.
// ClearFutureRunnable(id);
// } catch(Exception e){
// Logger.getLogger(BukkitConvertor.class.getName()).log(null, Level.SEVERE, e);
// }
// }
// }
// 
// @Override
// public void ClearFutureRunnable(int id) {
// if(validIDs.contains(id)){
// Bukkit.getServer().getScheduler().cancelTask(id);
// validIDs.remove(id);
// }
// }
public static MCEntity BukkitGetCorrectEntity(Entity be) {
    if (be == null) {
        return null;
    }
    BukkitMCEntityType type = BukkitMCEntityType.valueOfConcrete(be.getType());
    if (type.getWrapperClass() != null) {
        return ReflectionUtils.newInstance(type.getWrapperClass(), new Class[] { Entity.class }, new Object[] { be });
    }
    if (be instanceof Hanging) {
        type.setWrapperClass(BukkitMCHanging.class);
        return new BukkitMCHanging(be);
    }
    if (be instanceof Minecart) {
        // Must come before Vehicle
        type.setWrapperClass(BukkitMCMinecart.class);
        return new BukkitMCMinecart(be);
    }
    if (be instanceof Projectile) {
        type.setWrapperClass(BukkitMCProjectile.class);
        return new BukkitMCProjectile(be);
    }
    if (be instanceof Tameable) {
        // Must come before Ageable
        type.setWrapperClass(BukkitMCTameable.class);
        return new BukkitMCTameable(be);
    }
    if (be instanceof Ageable) {
        // Must come before LivingEntity
        type.setWrapperClass(BukkitMCAgeable.class);
        return new BukkitMCAgeable(be);
    }
    if (be instanceof HumanEntity) {
        // Must come before LivingEntity
        type.setWrapperClass(BukkitMCHumanEntity.class);
        return new BukkitMCHumanEntity(be);
    }
    if (be instanceof ComplexEntityPart) {
        type.setWrapperClass(BukkitMCComplexEntityPart.class);
        return new BukkitMCComplexEntityPart(be);
    }
    if (be instanceof ComplexLivingEntity) {
        // Must come before LivingEntity
        type.setWrapperClass(BukkitMCComplexLivingEntity.class);
        return new BukkitMCComplexLivingEntity(be);
    }
    if (be instanceof LivingEntity) {
        type.setWrapperClass(BukkitMCLivingEntity.class);
        return new BukkitMCLivingEntity(be);
    }
    if (be instanceof Vehicle) {
        type.setWrapperClass(BukkitMCVehicle.class);
        return new BukkitMCVehicle(be);
    }
    // Handle generically if we can't find a more specific type
    type.setWrapperClass(BukkitMCEntity.class);
    return new BukkitMCEntity(be);
}
Also used : BukkitMCTameable(com.laytonsmith.abstraction.bukkit.entities.BukkitMCTameable) BukkitMCHumanEntity(com.laytonsmith.abstraction.bukkit.entities.BukkitMCHumanEntity) ComplexEntityPart(org.bukkit.entity.ComplexEntityPart) BukkitMCComplexEntityPart(com.laytonsmith.abstraction.bukkit.entities.BukkitMCComplexEntityPart) BukkitMCTameable(com.laytonsmith.abstraction.bukkit.entities.BukkitMCTameable) Tameable(org.bukkit.entity.Tameable) BukkitMCAgeable(com.laytonsmith.abstraction.bukkit.entities.BukkitMCAgeable) BukkitMCEntityType(com.laytonsmith.abstraction.enums.bukkit.BukkitMCEntityType) BukkitMCCommandMinecart(com.laytonsmith.abstraction.bukkit.entities.BukkitMCCommandMinecart) CommandMinecart(org.bukkit.entity.minecart.CommandMinecart) Minecart(org.bukkit.entity.Minecart) BukkitMCMinecart(com.laytonsmith.abstraction.bukkit.entities.BukkitMCMinecart) BukkitMCComplexLivingEntity(com.laytonsmith.abstraction.bukkit.entities.BukkitMCComplexLivingEntity) ComplexLivingEntity(org.bukkit.entity.ComplexLivingEntity) BukkitMCHanging(com.laytonsmith.abstraction.bukkit.entities.BukkitMCHanging) Ageable(org.bukkit.entity.Ageable) BukkitMCAgeable(com.laytonsmith.abstraction.bukkit.entities.BukkitMCAgeable) BukkitMCLivingEntity(com.laytonsmith.abstraction.bukkit.entities.BukkitMCLivingEntity) BukkitMCProjectile(com.laytonsmith.abstraction.bukkit.entities.BukkitMCProjectile) Projectile(org.bukkit.entity.Projectile) BukkitMCProjectile(com.laytonsmith.abstraction.bukkit.entities.BukkitMCProjectile) BukkitMCComplexLivingEntity(com.laytonsmith.abstraction.bukkit.entities.BukkitMCComplexLivingEntity) BukkitMCLivingEntity(com.laytonsmith.abstraction.bukkit.entities.BukkitMCLivingEntity) LivingEntity(org.bukkit.entity.LivingEntity) ComplexLivingEntity(org.bukkit.entity.ComplexLivingEntity) Vehicle(org.bukkit.entity.Vehicle) BukkitMCVehicle(com.laytonsmith.abstraction.bukkit.entities.BukkitMCVehicle) BukkitMCEntity(com.laytonsmith.abstraction.bukkit.entities.BukkitMCEntity) BukkitMCMinecart(com.laytonsmith.abstraction.bukkit.entities.BukkitMCMinecart) Hanging(org.bukkit.entity.Hanging) BukkitMCHanging(com.laytonsmith.abstraction.bukkit.entities.BukkitMCHanging) BukkitMCHumanEntity(com.laytonsmith.abstraction.bukkit.entities.BukkitMCHumanEntity) HumanEntity(org.bukkit.entity.HumanEntity) BukkitMCComplexLivingEntity(com.laytonsmith.abstraction.bukkit.entities.BukkitMCComplexLivingEntity) BukkitMCComplexEntityPart(com.laytonsmith.abstraction.bukkit.entities.BukkitMCComplexEntityPart) BukkitMCVehicle(com.laytonsmith.abstraction.bukkit.entities.BukkitMCVehicle)

Example 8 with Ageable

use of org.bukkit.entity.Ageable in project MagicPlugin by elBukkit.

the class MagicCommandExecutor method onMagicList.

protected boolean onMagicList(CommandSender sender, String subCommand, String[] args) {
    String usage = "Usage: magic list <wands|map|automata|tasks|schematics|entities|blocks>";
    String listCommand = "";
    if (args.length > 1) {
        listCommand = args[1];
        if (!api.hasPermission(sender, "Magic.commands.magic." + subCommand + "." + listCommand)) {
            sendNoPermission(sender);
            return false;
        }
    } else {
        sender.sendMessage(ChatColor.GRAY + "For more specific information, add 'tasks', 'wands', 'maps', 'schematics', 'entities', 'blocks' or 'automata' parameter.");
        MageController apiController = api.getController();
        if (apiController != null && apiController instanceof MagicController) {
            MagicController controller = (MagicController) apiController;
            long timeout = controller.getPhysicsTimeout();
            if (timeout > 0) {
                long seconds = (timeout - System.currentTimeMillis()) / 1000;
                sender.sendMessage(ChatColor.GREEN + "Physics handler active for another " + ChatColor.DARK_GREEN + seconds + ChatColor.GREEN + " seconds");
            } else {
                sender.sendMessage(ChatColor.GRAY + "Physics handler inactive");
            }
        }
        Collection<Mage> mages = controller.getMages();
        sender.sendMessage(ChatColor.AQUA + "Modified blocks (" + ChatColor.LIGHT_PURPLE + UndoList.getRegistry().getModified().size() + ChatColor.AQUA + ")");
        sender.sendMessage(ChatColor.AQUA + "Watching blocks (" + ChatColor.LIGHT_PURPLE + UndoList.getRegistry().getWatching().size() + ChatColor.AQUA + ")");
        sender.sendMessage(ChatColor.AQUA + "Registered breaking (" + ChatColor.LIGHT_PURPLE + UndoList.getRegistry().getBreaking().size() + ChatColor.AQUA + ")");
        sender.sendMessage(ChatColor.AQUA + "Registered breakable (" + ChatColor.LIGHT_PURPLE + UndoList.getRegistry().getBreakable().size() + ChatColor.AQUA + ")");
        sender.sendMessage(ChatColor.AQUA + "Registered reflective (" + ChatColor.LIGHT_PURPLE + UndoList.getRegistry().getReflective().size() + ChatColor.AQUA + ")");
        sender.sendMessage(ChatColor.LIGHT_PURPLE + "Active mages: " + ChatColor.LIGHT_PURPLE + mages.size());
        Collection<com.elmakers.mine.bukkit.api.block.UndoList> pendingUndo = api.getPendingUndo();
        sender.sendMessage(ChatColor.AQUA + "Pending undo (" + ChatColor.LIGHT_PURPLE + pendingUndo.size() + ChatColor.AQUA + "): ");
        long now = System.currentTimeMillis();
        for (com.elmakers.mine.bukkit.api.block.UndoList undo : pendingUndo) {
            long remainingTime = (undo.getScheduledTime() - now) / 1000;
            sender.sendMessage(ChatColor.AQUA + undo.getName() + ChatColor.GRAY + " will undo in " + ChatColor.WHITE + "" + remainingTime + "" + ChatColor.GRAY + " seconds");
        }
        Collection<Mage> pending = api.getMagesWithPendingBatches();
        sender.sendMessage(ChatColor.AQUA + "Pending casts (" + ChatColor.LIGHT_PURPLE + pending.size() + ChatColor.AQUA + "): ");
        for (Mage mage : pending) {
            int totalSize = 0;
            int totalRemaining = 0;
            Collection<Batch> pendingBatches = mage.getPendingBatches();
            String names = "";
            if (pendingBatches.size() > 0) {
                for (Batch batch : pendingBatches) {
                    names = names + batch.getName() + " ";
                    totalSize += batch.size();
                    totalRemaining += batch.remaining();
                }
            }
            sender.sendMessage(ChatColor.AQUA + mage.getName() + ChatColor.GRAY + " has " + ChatColor.WHITE + "" + pendingBatches.size() + "" + ChatColor.GRAY + " pending (" + ChatColor.WHITE + "" + totalRemaining + "/" + totalSize + "" + ChatColor.GRAY + ") (" + names + ")");
        }
        return true;
    }
    if (listCommand.equalsIgnoreCase("schematics")) {
        List<String> schematics = new ArrayList<>();
        try {
            Plugin plugin = (Plugin) api;
            MagicController controller = (MagicController) api.getController();
            // Find built-in schematics
            CodeSource src = MagicAPI.class.getProtectionDomain().getCodeSource();
            if (src != null) {
                URL jar = src.getLocation();
                try (InputStream is = jar.openStream();
                    ZipInputStream zip = new ZipInputStream(is)) {
                    while (true) {
                        ZipEntry e = zip.getNextEntry();
                        if (e == null)
                            break;
                        String name = e.getName();
                        if (name.startsWith("schematics/")) {
                            schematics.add(name.replace("schematics/", ""));
                        }
                    }
                }
            }
            // Check extra path first
            File configFolder = plugin.getDataFolder();
            File magicSchematicFolder = new File(configFolder, "schematics");
            if (magicSchematicFolder.exists()) {
                for (File nextFile : magicSchematicFolder.listFiles()) {
                    schematics.add(nextFile.getName());
                }
            }
            String extraSchematicFilePath = controller.getExtraSchematicFilePath();
            if (extraSchematicFilePath != null && extraSchematicFilePath.length() > 0) {
                File schematicFolder = new File(configFolder, "../" + extraSchematicFilePath);
                if (schematicFolder.exists() && !schematicFolder.equals(magicSchematicFolder)) {
                    for (File nextFile : schematicFolder.listFiles()) {
                        schematics.add(nextFile.getName());
                    }
                }
            }
        } catch (Exception ex) {
            sender.sendMessage("Error loading schematics: " + ex.getMessage());
            ex.printStackTrace();
            ;
        }
        sender.sendMessage(ChatColor.DARK_AQUA + "Found " + ChatColor.LIGHT_PURPLE + schematics.size() + ChatColor.DARK_AQUA + " schematics");
        Collections.sort(schematics);
        for (String schematic : schematics) {
            if (schematic.indexOf(".schematic") > 0) {
                sender.sendMessage(ChatColor.AQUA + schematic.replace(".schematic", ""));
            }
        }
        return true;
    }
    if (listCommand.equalsIgnoreCase("tasks")) {
        List<BukkitTask> tasks = Bukkit.getScheduler().getPendingTasks();
        HashMap<String, Integer> pluginCounts = new HashMap<>();
        HashMap<String, HashMap<String, Integer>> taskCounts = new HashMap<>();
        for (BukkitTask task : tasks) {
            String pluginName = task.getOwner().getName();
            HashMap<String, Integer> pluginTaskCounts = taskCounts.get(pluginName);
            if (pluginTaskCounts == null) {
                pluginTaskCounts = new HashMap<>();
                taskCounts.put(pluginName, pluginTaskCounts);
            }
            String className = "(Unknown)";
            Runnable taskRunnable = CompatibilityUtils.getTaskRunnable(task);
            if (taskRunnable != null) {
                Class<? extends Runnable> taskClass = taskRunnable.getClass();
                className = taskClass.getName();
            }
            Integer count = pluginTaskCounts.get(className);
            if (count == null)
                count = 0;
            count++;
            pluginTaskCounts.put(className, count);
            Integer totalCount = pluginCounts.get(pluginName);
            if (totalCount == null)
                totalCount = 0;
            totalCount++;
            pluginCounts.put(pluginName, totalCount);
        }
        sender.sendMessage(ChatColor.LIGHT_PURPLE + "Active tasks: " + tasks.size());
        for (Entry<String, HashMap<String, Integer>> pluginEntry : taskCounts.entrySet()) {
            String pluginName = pluginEntry.getKey();
            sender.sendMessage(" " + ChatColor.DARK_PURPLE + pluginName + ": " + ChatColor.LIGHT_PURPLE + pluginCounts.get(pluginName));
            for (Entry<String, Integer> taskEntry : pluginEntry.getValue().entrySet()) {
                sender.sendMessage("  " + ChatColor.DARK_PURPLE + taskEntry.getKey() + ": " + ChatColor.LIGHT_PURPLE + taskEntry.getValue());
            }
        }
        return true;
    }
    if (listCommand.equalsIgnoreCase("wands")) {
        String owner = "";
        if (args.length > 2) {
            owner = args[2];
        }
        Collection<LostWand> lostWands = api.getLostWands();
        int shown = 0;
        for (LostWand lostWand : lostWands) {
            Location location = lostWand.getLocation();
            if (location == null)
                continue;
            if (owner.length() > 0 && !owner.equalsIgnoreCase(lostWand.getOwner())) {
                continue;
            }
            shown++;
            sender.sendMessage(ChatColor.AQUA + lostWand.getName() + ChatColor.WHITE + " (" + lostWand.getOwner() + ") @ " + ChatColor.BLUE + location.getWorld().getName() + " " + location.getBlockX() + " " + location.getBlockY() + " " + location.getBlockZ());
        }
        sender.sendMessage(shown + " lost wands found" + (owner.length() > 0 ? " for " + owner : ""));
        return true;
    }
    if (listCommand.equalsIgnoreCase("automata")) {
        Collection<Mage> automata = api.getAutomata();
        for (Mage automaton : automata) {
            Location location = automaton.getLocation();
            String worldName = location.getWorld().getName();
            boolean isOnline = false;
            World world = Bukkit.getWorld(worldName);
            if (worldName != null) {
                isOnline = world.isChunkLoaded(location.getBlockX() >> 4, location.getBlockZ() >> 4);
            }
            ChatColor nameColor = isOnline ? ChatColor.AQUA : ChatColor.GRAY;
            sender.sendMessage(nameColor + automaton.getName() + ChatColor.WHITE + " @ " + ChatColor.BLUE + worldName + " " + location.getBlockX() + " " + location.getBlockY() + " " + location.getBlockZ());
        }
        sender.sendMessage(automata.size() + " automata active");
        return true;
    }
    if (listCommand.equalsIgnoreCase("maps")) {
        String keyword = "";
        for (int i = 2; i < args.length; i++) {
            if (i != 2)
                keyword = keyword + " ";
            keyword = keyword + args[i];
        }
        onMapList(sender, keyword);
        return true;
    }
    if (listCommand.equalsIgnoreCase("blocks")) {
        for (BlockData blockData : UndoList.getRegistry().getModified().values()) {
            BlockVector blockLocation = blockData.getLocation();
            Block block = blockData.getBlock();
            sender.sendMessage(ChatColor.BLUE + "Block at " + ChatColor.GRAY + blockLocation.getBlockX() + ChatColor.DARK_GRAY + "," + ChatColor.GRAY + blockLocation.getBlockY() + ChatColor.DARK_GRAY + "," + ChatColor.GRAY + blockLocation.getBlockZ() + ChatColor.BLUE + " stored as " + ChatColor.AQUA + blockData.getMaterial() + ChatColor.BLUE + " is currently " + ChatColor.AQUA + block.getType() + ChatColor.BLUE + " from " + ChatColor.GOLD + blockData.getUndoList().getName());
        }
        return true;
    }
    if (listCommand.equalsIgnoreCase("mages")) {
        for (Mage mage : api.getController().getMages()) {
            Entity mageEntity = mage.getEntity();
            Location location = mage.getLocation();
            ChatColor mageColor = ChatColor.YELLOW;
            if (mage instanceof com.elmakers.mine.bukkit.magic.Mage && ((com.elmakers.mine.bukkit.magic.Mage) mage).isForget()) {
                mageColor = ChatColor.RED;
            } else if (mage.isAutomaton()) {
                mageColor = ChatColor.GOLD;
            }
            String mageType = mageEntity == null ? "Non-Entity" : mageEntity.getType().name();
            String message = ChatColor.AQUA + "Mage " + mageColor + mage.getId() + ChatColor.GRAY + " (" + mage.getName() + ")" + ChatColor.AQUA + " of type " + ChatColor.DARK_AQUA + mageType + ChatColor.AQUA;
            if (location != null) {
                String worldName = location.getWorld() != null ? location.getWorld().getName() : "(Unknown world)";
                message = message + " is at " + ChatColor.BLUE + worldName + " " + ChatColor.DARK_PURPLE + " " + location.getBlockX() + " " + location.getBlockY() + " " + location.getBlockZ();
            }
            sender.sendMessage(message);
        }
        return true;
    }
    if (listCommand.equalsIgnoreCase("entities")) {
        World world = Bukkit.getWorlds().get(0);
        NumberFormat formatter = new DecimalFormat("#0.0");
        List<EntityType> types = Arrays.asList(EntityType.values());
        Collections.sort(types, new Comparator<EntityType>() {

            @Override
            public int compare(EntityType o1, EntityType o2) {
                return o1.name().compareTo(o2.name());
            }
        });
        Collection<? extends Player> players = Bukkit.getServer().getOnlinePlayers();
        for (Player player : players) {
            showEntityInfo(sender, player, EntityType.PLAYER.name() + ChatColor.GRAY + " (" + player.getName() + " [" + (player.isSneaking() ? "sneaking" : "standing") + "])", formatter);
            break;
        }
        final Class<?> worldClass = NMSUtils.getBukkitClass("net.minecraft.server.World");
        for (EntityType entityType : types) {
            if (entityType.isSpawnable()) {
                Entity testEntity = null;
                String errorMessage = null;
                String entityName = "Entity" + entityType.getEntityClass().getSimpleName();
                // Still better than actually adding all of these entities to the world!
                if (entityName.equals("EntityGiant")) {
                    entityName = "EntityGiantZombie";
                } else if (entityName.equals("EntityLeashHitch")) {
                    entityName = "EntityLeash";
                } else if (entityName.equals("EntityStorageMinecart")) {
                    entityName = "EntityMinecartChest";
                } else if (entityName.equals("EntitySpawnerMinecart")) {
                    entityName = "EntityMinecartMobSpawner";
                } else if (entityName.equals("EntityCommandMinecart")) {
                    entityName = "EntityMinecartCommandBlock";
                } else if (entityName.equals("EntityPoweredMinecart")) {
                    entityName = "EntityMinecartFurnace";
                } else if (entityName.equals("EntityExplosiveMinecart")) {
                    entityName = "EntityMinecartTNT";
                } else if (entityName.contains("Minecart")) {
                    entityName = entityType.getEntityClass().getSimpleName();
                    entityName = entityName.replace("Minecart", "");
                    entityName = "EntityMinecart" + entityName;
                }
                try {
                    Class<?> entityClass = NMSUtils.getBukkitClass("net.minecraft.server." + entityName);
                    if (entityClass != null) {
                        Constructor<? extends Object> constructor = entityClass.getConstructor(worldClass);
                        Object nmsWorld = NMSUtils.getHandle(world);
                        Object nmsEntity = constructor.newInstance(nmsWorld);
                        testEntity = NMSUtils.getBukkitEntity(nmsEntity);
                        if (testEntity == null) {
                            errorMessage = "Failed to get Bukkit entity for class " + entityName;
                        }
                    } else {
                        errorMessage = "Could not load class " + entityName;
                    }
                } catch (Exception ex) {
                    testEntity = null;
                    errorMessage = ex.getClass().getSimpleName() + " [" + entityName + "]";
                    String message = ex.getMessage();
                    if (message != null && !message.isEmpty()) {
                        errorMessage += ": " + message;
                    }
                }
                if (testEntity == null) {
                    sender.sendMessage(ChatColor.BLACK + entityType.name() + ": " + ChatColor.RED + "Spawning error " + ChatColor.DARK_RED + "(" + errorMessage + ")");
                    continue;
                }
                String label = entityType.name();
                Ageable ageable = (testEntity instanceof Ageable) ? (Ageable) testEntity : null;
                Zombie zombie = (testEntity instanceof Zombie) ? (Zombie) testEntity : null;
                Skeleton skeleton = (testEntity instanceof Skeleton) ? (Skeleton) testEntity : null;
                Slime slime = (testEntity instanceof Slime) ? (Slime) testEntity : null;
                if (ageable != null) {
                    label = label + ChatColor.GRAY + " (Adult)";
                    ageable.setAdult();
                } else if (zombie != null) {
                    label = label + ChatColor.GRAY + " (Adult)";
                    zombie.setBaby(false);
                } else if (skeleton != null) {
                    label = label + ChatColor.GRAY + " (NORMAL)";
                    skeleton.setSkeletonType(Skeleton.SkeletonType.NORMAL);
                } else if (slime != null) {
                    label = label + ChatColor.GRAY + " (Size 1)";
                    slime.setSize(1);
                }
                showEntityInfo(sender, testEntity, label, formatter);
                if (ageable != null) {
                    label = entityType.name() + ChatColor.GRAY + " (Baby)";
                    ageable.setBaby();
                    showEntityInfo(sender, testEntity, label, formatter);
                } else if (zombie != null) {
                    label = entityType.name() + ChatColor.GRAY + " (Baby)";
                    zombie.setBaby(true);
                    showEntityInfo(sender, testEntity, label, formatter);
                } else if (skeleton != null) {
                    label = entityType.name() + ChatColor.GRAY + " (WITHER)";
                    skeleton.setSkeletonType(Skeleton.SkeletonType.WITHER);
                    showEntityInfo(sender, testEntity, label, formatter);
                } else if (slime != null) {
                    label = entityType.name() + ChatColor.GRAY + " (Size 2)";
                    slime.setSize(2);
                    showEntityInfo(sender, testEntity, label, formatter);
                    label = entityType.name() + ChatColor.GRAY + " (Size 4)";
                    slime.setSize(4);
                    showEntityInfo(sender, testEntity, label, formatter);
                    label = entityType.name() + ChatColor.GRAY + " (Size 8)";
                    slime.setSize(8);
                    showEntityInfo(sender, testEntity, label, formatter);
                    label = entityType.name() + ChatColor.GRAY + " (Size 16)";
                    slime.setSize(16);
                    showEntityInfo(sender, testEntity, label, formatter);
                }
            }
        }
        return true;
    }
    sender.sendMessage(usage);
    return true;
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) World(org.bukkit.World) MageController(com.elmakers.mine.bukkit.api.magic.MageController) LostWand(com.elmakers.mine.bukkit.api.wand.LostWand) Player(org.bukkit.entity.Player) Zombie(org.bukkit.entity.Zombie) CodeSource(java.security.CodeSource) EntityType(org.bukkit.entity.EntityType) ZipInputStream(java.util.zip.ZipInputStream) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Block(org.bukkit.block.Block) File(java.io.File) ChatColor(org.bukkit.ChatColor) ZipEntry(java.util.zip.ZipEntry) DecimalFormat(java.text.DecimalFormat) Ageable(org.bukkit.entity.Ageable) URL(java.net.URL) UndoList(com.elmakers.mine.bukkit.block.UndoList) Batch(com.elmakers.mine.bukkit.api.batch.Batch) MagicController(com.elmakers.mine.bukkit.magic.MagicController) Skeleton(org.bukkit.entity.Skeleton) BlockData(com.elmakers.mine.bukkit.api.block.BlockData) MagicAPI(com.elmakers.mine.bukkit.api.magic.MagicAPI) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) BukkitTask(org.bukkit.scheduler.BukkitTask) Slime(org.bukkit.entity.Slime) WandCleanupRunnable(com.elmakers.mine.bukkit.wand.WandCleanupRunnable) BlockVector(org.bukkit.util.BlockVector) Plugin(org.bukkit.plugin.Plugin) Location(org.bukkit.Location) NumberFormat(java.text.NumberFormat)

Example 9 with Ageable

use of org.bukkit.entity.Ageable in project MagicPlugin by elBukkit.

the class ShrinkSpell method onCast.

@Override
public SpellResult onCast(ConfigurationSection parameters) {
    String giveName = parameters.getString("name");
    if (giveName != null) {
        String itemName = giveName + "'s Head";
        Player player = Bukkit.getPlayer(giveName);
        if (player != null) {
            dropPlayerHead(getLocation(), player, itemName);
        } else {
            dropPlayerHead(getLocation(), giveName, itemName);
        }
        return SpellResult.CAST;
    }
    Target target = getTarget();
    if (target.hasEntity()) {
        Entity targetEntity = target.getEntity();
        if (controller.isElemental(targetEntity)) {
            double elementalSize = controller.getElementalScale(targetEntity);
            if (elementalSize < 0.1) {
                int elementalDamage = parameters.getInt("elemental_damage", DEFAULT_ENTITY_DAMAGE);
                controller.damageElemental(targetEntity, elementalDamage, 0, mage.getCommandSender());
            } else {
                elementalSize /= 2;
                controller.setElementalScale(targetEntity, elementalSize);
            }
            return SpellResult.CAST;
        }
        if (!(targetEntity instanceof LivingEntity))
            return SpellResult.NO_TARGET;
        // Register for undo in advance to catch entity death.
        registerForUndo();
        int damage = parameters.getInt("entity_damage", DEFAULT_ENTITY_DAMAGE);
        LivingEntity li = (LivingEntity) targetEntity;
        boolean alreadyDead = li.isDead() || li.getHealth() <= 0;
        String ownerName = null;
        String itemName = null;
        byte data = 3;
        if (li instanceof Player) {
            damage = parameters.getInt("player_damage", DEFAULT_PLAYER_DAMAGE);
            ownerName = ((Player) li).getName();
        } else {
            itemName = li.getType().getName() + " Head";
            switch(li.getType()) {
                case CREEPER:
                    data = 4;
                    break;
                case ZOMBIE:
                    data = 2;
                    break;
                case SKELETON:
                    Skeleton skeleton = (Skeleton) li;
                    data = (byte) (skeleton.getSkeletonType() == SkeletonType.NORMAL ? 0 : 1);
                    break;
                default:
                    ownerName = getMobSkin(li.getType());
            }
        }
        if (itemName == null && ownerName != null) {
            itemName = ownerName + "'s Head";
        }
        Location targetLocation = targetEntity.getLocation();
        if (li instanceof Player) {
            CompatibilityUtils.magicDamage(li, damage, mage.getEntity());
            if (li.isDead() && !alreadyDead) {
                dropPlayerHead(targetEntity.getLocation(), (Player) li, itemName);
            }
        } else if (li.getType() == EntityType.GIANT) {
            UndoList spawnedList = com.elmakers.mine.bukkit.block.UndoList.getUndoList(li);
            registerModified(li);
            li.remove();
            Entity zombie = targetLocation.getWorld().spawnEntity(targetLocation, EntityType.ZOMBIE);
            if (zombie instanceof Zombie) {
                ((Zombie) zombie).setBaby(false);
            }
            registerForUndo(zombie);
            if (spawnedList != null) {
                spawnedList.add(zombie);
            }
        } else if (li instanceof Ageable && ((Ageable) li).isAdult() && !(li instanceof Player)) {
            registerModified(li);
            ((Ageable) li).setBaby();
        } else if (li instanceof Zombie && !((Zombie) li).isBaby()) {
            registerModified(li);
            ((Zombie) li).setBaby(true);
        } else if (li instanceof PigZombie && !((PigZombie) li).isBaby()) {
            registerModified(li);
            ((PigZombie) li).setBaby(true);
        } else if (li instanceof Slime && ((Slime) li).getSize() > 1) {
            registerModified(li);
            Slime slime = (Slime) li;
            slime.setSize(slime.getSize() - 1);
        } else {
            CompatibilityUtils.magicDamage(li, damage, mage.getEntity());
            if ((ownerName != null || data != 3) && (li.isDead() || li.getHealth() == 0) && !alreadyDead) {
                dropHead(targetEntity.getLocation(), ownerName, itemName, data);
            }
        }
    } else {
        Block targetBlock = target.getBlock();
        if (targetBlock == null) {
            return SpellResult.NO_TARGET;
        }
        String blockSkin = getBlockSkin(targetBlock.getType());
        if (blockSkin == null)
            return SpellResult.NO_TARGET;
        if (!hasBreakPermission(targetBlock)) {
            return SpellResult.INSUFFICIENT_PERMISSION;
        }
        if (mage.isIndestructible(targetBlock)) {
            return SpellResult.NO_TARGET;
        }
        registerForUndo(targetBlock);
        registerForUndo();
        dropHead(targetBlock.getLocation(), blockSkin, targetBlock.getType().name(), (byte) 3);
        targetBlock.setType(Material.AIR);
    }
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) Zombie(org.bukkit.entity.Zombie) PigZombie(org.bukkit.entity.PigZombie) Ageable(org.bukkit.entity.Ageable) Slime(org.bukkit.entity.Slime) LivingEntity(org.bukkit.entity.LivingEntity) Target(com.elmakers.mine.bukkit.utility.Target) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) PigZombie(org.bukkit.entity.PigZombie) Block(org.bukkit.block.Block) Skeleton(org.bukkit.entity.Skeleton) Location(org.bukkit.Location)

Example 10 with Ageable

use of org.bukkit.entity.Ageable in project MagicPlugin by elBukkit.

the class ShrinkEntityAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Entity targetEntity = context.getTargetEntity();
    MageController controller = context.getController();
    if (controller.isElemental(targetEntity)) {
        double elementalSize = controller.getElementalScale(targetEntity);
        if (elementalSize < 0.1) {
            return super.perform(context);
        }
        elementalSize /= 2;
        controller.setElementalScale(targetEntity, elementalSize);
        return SpellResult.CAST;
    }
    if (!(targetEntity instanceof LivingEntity))
        return SpellResult.NO_TARGET;
    LivingEntity li = (LivingEntity) targetEntity;
    boolean alreadyDead = li.isDead() || li.getHealth() <= 0;
    String ownerName = null;
    String itemName = null;
    byte data = 3;
    if (li instanceof Player) {
        ownerName = ((Player) li).getName();
    } else {
        itemName = DeprecatedUtils.getName(li.getType()) + " Head";
        switch(li.getType()) {
            case CREEPER:
                data = 4;
                break;
            case ZOMBIE:
                data = 2;
                break;
            case SKELETON:
                Skeleton skeleton = (Skeleton) li;
                data = (byte) (skeleton.getSkeletonType() == SkeletonType.NORMAL ? 0 : 1);
                break;
            default:
                ownerName = controller.getMobSkin(li.getType());
        }
    }
    if (itemName == null && ownerName != null) {
        itemName = ownerName + "'s Head";
    }
    Location targetLocation = targetEntity.getLocation();
    if (li instanceof Player) {
        super.perform(context);
        if (li.isDead() && !alreadyDead) {
            dropPlayerHead(targetEntity.getLocation(), (Player) li, itemName);
        }
    } else if (li.getType() == EntityType.GIANT) {
        UndoList spawnedList = com.elmakers.mine.bukkit.block.UndoList.getUndoList(li);
        context.registerModified(li);
        li.remove();
        Entity zombie = targetLocation.getWorld().spawnEntity(targetLocation, EntityType.ZOMBIE);
        if (zombie instanceof Zombie) {
            ((Zombie) zombie).setBaby(false);
        }
        context.registerForUndo(zombie);
        if (spawnedList != null) {
            spawnedList.add(zombie);
        }
    } else if (li instanceof Ageable && ((Ageable) li).isAdult() && !(li instanceof Player)) {
        context.registerModified(li);
        ((Ageable) li).setBaby();
    } else if (li instanceof Zombie && !((Zombie) li).isBaby()) {
        context.registerModified(li);
        ((Zombie) li).setBaby(true);
    } else if (li instanceof PigZombie && !((PigZombie) li).isBaby()) {
        context.registerModified(li);
        ((PigZombie) li).setBaby(true);
    } else if (li instanceof Slime && ((Slime) li).getSize() > 1) {
        context.registerModified(li);
        Slime slime = (Slime) li;
        slime.setSize(slime.getSize() - 1);
    } else if (li instanceof Skeleton && skeletons && ((Skeleton) li).getSkeletonType() == SkeletonType.WITHER) {
        context.registerModified(li);
        Skeleton skeleton = (Skeleton) li;
        skeleton.setSkeletonType(SkeletonType.NORMAL);
    } else {
        super.perform(context);
        if ((ownerName != null || data != 3) && (li.isDead() || li.getHealth() == 0) && !alreadyDead) {
            dropHead(targetEntity.getLocation(), ownerName, itemName, data);
        }
    }
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) Zombie(org.bukkit.entity.Zombie) PigZombie(org.bukkit.entity.PigZombie) Ageable(org.bukkit.entity.Ageable) Slime(org.bukkit.entity.Slime) LivingEntity(org.bukkit.entity.LivingEntity) MageController(com.elmakers.mine.bukkit.api.magic.MageController) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) PigZombie(org.bukkit.entity.PigZombie) Skeleton(org.bukkit.entity.Skeleton) Location(org.bukkit.Location)

Aggregations

Ageable (org.bukkit.entity.Ageable)14 LivingEntity (org.bukkit.entity.LivingEntity)10 Player (org.bukkit.entity.Player)8 Zombie (org.bukkit.entity.Zombie)8 Location (org.bukkit.Location)7 Entity (org.bukkit.entity.Entity)6 Slime (org.bukkit.entity.Slime)5 UndoList (com.elmakers.mine.bukkit.api.block.UndoList)4 PigZombie (org.bukkit.entity.PigZombie)4 Skeleton (org.bukkit.entity.Skeleton)4 MageController (com.elmakers.mine.bukkit.api.magic.MageController)3 Target (com.elmakers.mine.bukkit.utility.Target)3 Block (org.bukkit.block.Block)3 EntityType (org.bukkit.entity.EntityType)3 Tameable (org.bukkit.entity.Tameable)3 Mage (com.elmakers.mine.bukkit.api.magic.Mage)2 Age (net.citizensnpcs.trait.Age)2 Projectile (org.bukkit.entity.Projectile)2 Batch (com.elmakers.mine.bukkit.api.batch.Batch)1 BlockData (com.elmakers.mine.bukkit.api.block.BlockData)1