Search in sources :

Example 1 with EntityData

use of com.elmakers.mine.bukkit.api.entity.EntityData in project MagicPlugin by elBukkit.

the class PlayerController method onPlayerInteractAtEntity.

@EventHandler
public void onPlayerInteractAtEntity(PlayerInteractAtEntityEvent event) {
    Entity entity = event.getRightClicked();
    EntityData mob = controller.getMobByName(entity.getCustomName());
    if (mob == null)
        return;
    String interactSpell = mob.getInteractSpell();
    if (interactSpell == null || interactSpell.isEmpty())
        return;
    Player player = event.getPlayer();
    Mage mage = controller.getMage(player);
    event.setCancelled(true);
    ConfigurationSection config = new MemoryConfiguration();
    config.set("entity", entity.getUniqueId().toString());
    controller.cast(mage, interactSpell, config, player, player);
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) Mage(com.elmakers.mine.bukkit.magic.Mage) EntityData(com.elmakers.mine.bukkit.api.entity.EntityData) MemoryConfiguration(org.bukkit.configuration.MemoryConfiguration) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) EventHandler(org.bukkit.event.EventHandler)

Example 2 with EntityData

use of com.elmakers.mine.bukkit.api.entity.EntityData in project MagicPlugin by elBukkit.

the class MagicMobCommandExecutor method onClearMobs.

protected void onClearMobs(CommandSender sender, String[] args) {
    String mobType = args.length > 1 ? args[1] : null;
    if (mobType != null && mobType.equalsIgnoreCase("all")) {
        mobType = null;
    }
    String worldName = null;
    Integer radiusSquared = null;
    Location targetLocation = null;
    Player player = (sender instanceof Player) ? (Player) sender : null;
    BlockCommandSender commandBlock = (sender instanceof BlockCommandSender) ? (BlockCommandSender) sender : null;
    if (args.length == 3) {
        if (!(sender instanceof ConsoleCommandSender) && Bukkit.getWorld(args[2]) == null) {
            try {
                int radius = Integer.parseInt(args[2]);
                radiusSquared = radius * radius;
            } catch (Exception ignored) {
            }
        }
        if (radiusSquared == null) {
            worldName = args[2];
        } else {
            if (player != null) {
                targetLocation = player.getLocation();
            } else if (commandBlock != null) {
                targetLocation = commandBlock.getBlock().getLocation();
            } else {
                sender.sendMessage(ChatColor.RED + "Invalid world: " + args[2]);
            }
        }
    } else if (args.length > 5) {
        World world = null;
        if (args.length > 6) {
            worldName = args[6];
            world = Bukkit.getWorld(worldName);
        }
        if (world == null) {
            sender.sendMessage(ChatColor.RED + "Invalid world: " + worldName);
        }
        try {
            int radius = Integer.parseInt(args[2]);
            radiusSquared = radius * radius;
            double currentX = 0;
            double currentY = 0;
            double currentZ = 0;
            if (player != null) {
                targetLocation = player.getLocation();
            } else if (commandBlock != null) {
                targetLocation = commandBlock.getBlock().getLocation();
            }
            if (targetLocation != null) {
                currentX = targetLocation.getX();
                currentY = targetLocation.getY();
                currentZ = targetLocation.getZ();
                if (world == null) {
                    world = targetLocation.getWorld();
                    worldName = world.getName();
                }
            }
            if (world == null) {
                sender.sendMessage(ChatColor.RED + "Usage: mmob clear <type> <radius> <x> <y> <z> <world>");
                return;
            }
            targetLocation = new Location(world, ConfigurationUtils.overrideDouble(args[3], currentX), ConfigurationUtils.overrideDouble(args[4], currentY), ConfigurationUtils.overrideDouble(args[5], currentZ));
        } catch (Exception ex) {
            sender.sendMessage(ChatColor.RED + "Usage: mmob clear <type> <radius> <x> <y> <z> <world>");
            return;
        }
    }
    Collection<Mage> mages = new ArrayList<>(api.getController().getMobMages());
    int removed = 0;
    for (Mage mage : mages) {
        EntityData entityData = mage.getEntityData();
        if (entityData == null)
            continue;
        if (worldName != null && !mage.getLocation().getWorld().getName().equals(worldName))
            continue;
        if (mobType != null && !entityData.getKey().equals(mobType))
            continue;
        if (radiusSquared != null && targetLocation != null && mage.getLocation().distanceSquared(targetLocation) > radiusSquared)
            continue;
        Entity entity = mage.getEntity();
        mage.undoScheduled();
        api.getController().removeMage(mage);
        if (entity != null) {
            entity.remove();
        }
        removed++;
    }
    List<World> worlds = new ArrayList<>();
    if (worldName != null) {
        World world = Bukkit.getWorld(worldName);
        if (world == null) {
            sender.sendMessage(ChatColor.RED + "Unknown world: " + ChatColor.WHITE + worldName);
            return;
        }
        worlds.add(world);
    } else {
        worlds.addAll(Bukkit.getWorlds());
    }
    Set<String> mobNames = new HashSet<>();
    if (mobType != null) {
        EntityData mob = api.getController().getMob(mobType);
        if (mob == null) {
            sender.sendMessage(ChatColor.RED + "Unknown mob type: " + ChatColor.WHITE + mobType);
            return;
        }
        mobNames.add(mob.getName());
    } else {
        Set<String> allKeys = api.getController().getMobKeys();
        for (String key : allKeys) {
            EntityData mob = api.getController().getMob(key);
            mobNames.add(mob.getName());
        }
    }
    for (World world : worlds) {
        List<Entity> entities = world.getEntities();
        for (Entity entity : entities) {
            String customName = entity.getCustomName();
            if (entity.isValid() && customName != null && mobNames.contains(customName)) {
                if (radiusSquared != null && targetLocation != null && entity.getLocation().distanceSquared(targetLocation) > radiusSquared)
                    continue;
                entity.remove();
                removed++;
            }
        }
    }
    sender.sendMessage("Removed " + removed + " magic mobs");
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) EntityData(com.elmakers.mine.bukkit.api.entity.EntityData) World(org.bukkit.World) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Location(org.bukkit.Location) BlockCommandSender(org.bukkit.command.BlockCommandSender) HashSet(java.util.HashSet)

Example 3 with EntityData

use of com.elmakers.mine.bukkit.api.entity.EntityData in project MagicPlugin by elBukkit.

the class MagicController method spawnMob.

@Nullable
@Override
public Entity spawnMob(String key, Location location) {
    EntityData mobType = mobs.get(key);
    if (mobType != null) {
        return mobType.spawn(this, location);
    }
    EntityType entityType = com.elmakers.mine.bukkit.entity.EntityData.parseEntityType(key);
    if (entityType == null) {
        return null;
    }
    return location.getWorld().spawnEntity(location, entityType);
}
Also used : EntityType(org.bukkit.entity.EntityType) EntityData(com.elmakers.mine.bukkit.api.entity.EntityData) Nullable(javax.annotation.Nullable)

Example 4 with EntityData

use of com.elmakers.mine.bukkit.api.entity.EntityData in project MagicPlugin by elBukkit.

the class Schematic method load.

@SuppressWarnings("deprecation")
public void load(short width, short height, short length, short[] blockTypes, byte[] data, Collection<Object> tileEntityData, Collection<Object> entityData, Vector origin, Vector offset) {
    size = new Vector(width, height, length);
    center = new Vector(Math.floor(size.getBlockX() / 2), 0, Math.floor(size.getBlockZ() / 2));
    blocks = new MaterialAndData[width][height][length];
    entities = new ArrayList<>();
    // Load entities
    for (Object entity : entityData) {
        String type = NMSUtils.getMetaString(entity, "id");
        Vector position = NMSUtils.getPosition(entity, "Pos");
        if (position == null)
            continue;
        position = position.subtract(origin).subtract(center);
        if (type == null)
            continue;
        // Only doing paintings and item frames for now.
        if (type.equals("Painting")) {
            String motive = NMSUtils.getMetaString(entity, "Motive");
            motive = motive.toLowerCase();
            Art art = Art.ALBAN;
            for (Art test : Art.values()) {
                if (test.name().toLowerCase().replace("_", "").equals(motive)) {
                    art = test;
                    break;
                }
            }
            byte facing = NMSUtils.getMetaByte(entity, "Facing");
            EntityData painting = com.elmakers.mine.bukkit.entity.EntityData.loadPainting(position, art, getFacing(facing));
            entities.add(painting);
        } else if (type.equals("ItemFrame")) {
            byte facing = NMSUtils.getMetaByte(entity, "Facing");
            byte rotation = NMSUtils.getMetaByte(entity, "ItemRotation");
            Rotation rot = Rotation.NONE;
            if (rotation < Rotation.values().length) {
                rot = Rotation.values()[rotation];
            }
            ItemStack item = NMSUtils.getItem(NMSUtils.getNode(entity, "Item"));
            EntityData itemFrame = com.elmakers.mine.bukkit.entity.EntityData.loadItemFrame(position, item, getFacing(facing), rot);
            entities.add(itemFrame);
        }
    }
    // Map tile entity data
    Map<BlockVector, Object> tileEntityMap = new HashMap<>();
    for (Object tileEntity : tileEntityData) {
        try {
            Integer x = NMSUtils.getMetaInt(tileEntity, "x");
            Integer y = NMSUtils.getMetaInt(tileEntity, "y");
            Integer z = NMSUtils.getMetaInt(tileEntity, "z");
            if (x == null || y == null || z == null)
                continue;
            BlockVector location = new BlockVector(x, y, z);
            tileEntityMap.put(location, tileEntity);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    for (int y = 0; y < height; y++) {
        for (int z = 0; z < length; z++) {
            for (int x = 0; x < width; x++) {
                int index = x + (y * length + z) * width;
                Material material = null;
                try {
                    material = Material.getMaterial(blockTypes[index]);
                } catch (Exception ex) {
                    material = null;
                    ex.printStackTrace();
                }
                if (material != null) {
                    MaterialAndData block = new com.elmakers.mine.bukkit.block.MaterialAndData(material, data[index]);
                    // Check for tile entity data
                    BlockVector blockLocation = new BlockVector(x, y, z);
                    Object tileEntity = tileEntityMap.get(blockLocation);
                    if (tileEntity != null) {
                        try {
                            if (material == Material.COMMAND) {
                                String customName = NMSUtils.getMetaString(tileEntity, "CustomName");
                                if (!customName.isEmpty()) {
                                    block.setCustomName(customName);
                                }
                                block.setCommandLine(NMSUtils.getMetaString(tileEntity, "Command"));
                            } else {
                                block.setRawData(tileEntity);
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                    blocks[x][y][z] = block;
                }
            }
        }
    }
    loaded = true;
}
Also used : Art(org.bukkit.Art) HashMap(java.util.HashMap) EntityData(com.elmakers.mine.bukkit.api.entity.EntityData) Material(org.bukkit.Material) Rotation(org.bukkit.Rotation) MaterialAndData(com.elmakers.mine.bukkit.api.block.MaterialAndData) BlockVector(org.bukkit.util.BlockVector) ItemStack(org.bukkit.inventory.ItemStack) BlockVector(org.bukkit.util.BlockVector) Vector(org.bukkit.util.Vector)

Example 5 with EntityData

use of com.elmakers.mine.bukkit.api.entity.EntityData in project MagicPlugin by elBukkit.

the class BrushBatch method finish.

@Override
public void finish() {
    if (!finished) {
        MaterialBrush brush = spell.getBrush();
        if (brush != null && brush.hasEntities()) {
            // Copy over new entities
            Collection<EntityData> entities = brush.getEntities();
            // Delete any entities already in the area, add them to the undo list.
            Collection<Entity> targetEntities = brush.getTargetEntities();
            if (targetEntities != null) {
                for (Entity entity : targetEntities) {
                    if (contains(entity.getLocation())) {
                        undoList.modify(entity);
                        entity.remove();
                    }
                }
            }
            if (entities != null) {
                for (EntityData entity : entities) {
                    if (contains(entity.getLocation())) {
                        undoList.add(entity.spawn());
                    }
                }
            }
        }
        super.finish();
    }
}
Also used : Entity(org.bukkit.entity.Entity) MaterialBrush(com.elmakers.mine.bukkit.api.block.MaterialBrush) EntityData(com.elmakers.mine.bukkit.api.entity.EntityData)

Aggregations

EntityData (com.elmakers.mine.bukkit.api.entity.EntityData)6 Entity (org.bukkit.entity.Entity)3 Mage (com.elmakers.mine.bukkit.api.magic.Mage)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Player (org.bukkit.entity.Player)2 MaterialAndData (com.elmakers.mine.bukkit.api.block.MaterialAndData)1 MaterialBrush (com.elmakers.mine.bukkit.api.block.MaterialBrush)1 Mage (com.elmakers.mine.bukkit.magic.Mage)1 HashSet (java.util.HashSet)1 Nullable (javax.annotation.Nullable)1 Art (org.bukkit.Art)1 Location (org.bukkit.Location)1 Material (org.bukkit.Material)1 Rotation (org.bukkit.Rotation)1 World (org.bukkit.World)1 BlockCommandSender (org.bukkit.command.BlockCommandSender)1 ConsoleCommandSender (org.bukkit.command.ConsoleCommandSender)1 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)1 MemoryConfiguration (org.bukkit.configuration.MemoryConfiguration)1