Search in sources :

Example 1 with BukkitMobInitEvent

use of de.dytanic.cloudnet.bridge.event.bukkit.BukkitMobInitEvent in project CloudNet by Dytanic.

the class PacketInMobSelector method handleInput.

@Override
public void handleInput(Document data, PacketSender packetSender) {
    Map<UUID, ServerMob> mobMap = data.getObject("mobs", new TypeToken<Map<UUID, ServerMob>>() {
    }.getType());
    MobConfig mobConfig = data.getObject("mobConfig", new TypeToken<MobConfig>() {
    }.getType());
    Map<UUID, ServerMob> filteredMobs = MapWrapper.filter(mobMap, new Acceptable<ServerMob>() {

        @Override
        public boolean isAccepted(ServerMob value) {
            return value.getPosition().getGroup().equalsIgnoreCase(CloudAPI.getInstance().getGroup());
        }
    });
    if (MobSelector.getInstance() != null) {
        Bukkit.getScheduler().runTask(CloudServer.getInstance().getPlugin(), new Runnable() {

            @Override
            public void run() {
                MobSelector.getInstance().shutdown();
                MobSelector.getInstance().setMobConfig(mobConfig);
                MobSelector.getInstance().setMobs(new HashMap<>());
                MobSelector.getInstance().setMobs(MapWrapper.transform(filteredMobs, new Catcher<UUID, UUID>() {

                    @Override
                    public UUID doCatch(UUID key) {
                        return key;
                    }
                }, new Catcher<MobSelector.MobImpl, ServerMob>() {

                    @Override
                    public MobSelector.MobImpl doCatch(ServerMob key) {
                        MobSelector.getInstance().toLocation(key.getPosition()).getChunk().load();
                        Entity entity = MobSelector.getInstance().toLocation(key.getPosition()).getWorld().spawnEntity(MobSelector.getInstance().toLocation(key.getPosition()), EntityType.valueOf(key.getType()));
                        entity.setFireTicks(0);
                        Object armorStand = ReflectionUtil.armorstandCreation(MobSelector.getInstance().toLocation(key.getPosition()), entity, key);
                        if (armorStand != null) {
                            MobSelector.getInstance().updateCustom(key, armorStand);
                            Entity armor = (Entity) armorStand;
                            if (armor.getPassenger() == null && key.getItemId() != null) {
                                Item item = Bukkit.getWorld(key.getPosition().getWorld()).dropItem(armor.getLocation(), new ItemStack(key.getItemId()));
                                item.setTicksLived(Integer.MAX_VALUE);
                                item.setPickupDelay(Integer.MAX_VALUE);
                                armor.setPassenger(item);
                            }
                        }
                        if (entity instanceof Villager) {
                            ((Villager) entity).setProfession(Villager.Profession.FARMER);
                        }
                        MobSelector.getInstance().unstableEntity(entity);
                        entity.setCustomNameVisible(true);
                        entity.setCustomName(ChatColor.translateAlternateColorCodes('&', key.getDisplay()));
                        MobSelector.MobImpl mob = new MobSelector.MobImpl(key.getUniqueId(), key, entity, MobSelector.getInstance().create(mobConfig, key), new HashMap<>(), armorStand);
                        Bukkit.getPluginManager().callEvent(new BukkitMobInitEvent(mob));
                        return mob;
                    }
                }));
                Bukkit.getScheduler().runTaskAsynchronously(CloudServer.getInstance().getPlugin(), new Runnable() {

                    @Override
                    public void run() {
                        for (ServerInfo serverInfo : MobSelector.getInstance().getServers().values()) {
                            MobSelector.getInstance().handleUpdate(serverInfo);
                        }
                    }
                });
            }
        });
    } else {
        MobSelector mobSelector = new MobSelector(mobConfig);
        MobSelector.getInstance().setMobs(new HashMap<>());
        Bukkit.getScheduler().runTask(CloudServer.getInstance().getPlugin(), new Runnable() {

            @Override
            public void run() {
                MobSelector.getInstance().setMobs(MapWrapper.transform(filteredMobs, new Catcher<UUID, UUID>() {

                    @Override
                    public UUID doCatch(UUID key) {
                        return key;
                    }
                }, new Catcher<MobSelector.MobImpl, ServerMob>() {

                    @Override
                    public MobSelector.MobImpl doCatch(ServerMob key) {
                        MobSelector.getInstance().toLocation(key.getPosition()).getChunk().load();
                        Entity entity = MobSelector.getInstance().toLocation(key.getPosition()).getWorld().spawnEntity(MobSelector.getInstance().toLocation(key.getPosition()), EntityType.valueOf(key.getType()));
                        Object armorStand = ReflectionUtil.armorstandCreation(MobSelector.getInstance().toLocation(key.getPosition()), entity, key);
                        if (armorStand != null) {
                            MobSelector.getInstance().updateCustom(key, armorStand);
                            Entity armor = (Entity) armorStand;
                            if (armor.getPassenger() == null && key.getItemId() != null) {
                                Item item = Bukkit.getWorld(key.getPosition().getWorld()).dropItem(armor.getLocation(), new ItemStack(key.getItemId()));
                                item.setTicksLived(Integer.MAX_VALUE);
                                item.setPickupDelay(Integer.MAX_VALUE);
                                armor.setPassenger(item);
                            }
                        }
                        if (entity instanceof Villager) {
                            ((Villager) entity).setProfession(Villager.Profession.FARMER);
                        }
                        MobSelector.getInstance().unstableEntity(entity);
                        entity.setCustomNameVisible(true);
                        entity.setCustomName(ChatColor.translateAlternateColorCodes('&', key.getDisplay()));
                        MobSelector.MobImpl mob = new MobSelector.MobImpl(key.getUniqueId(), key, entity, MobSelector.getInstance().create(mobConfig, key), new HashMap<>(), armorStand);
                        Bukkit.getPluginManager().callEvent(new BukkitMobInitEvent(mob));
                        return mob;
                    }
                }));
            }
        });
        mobSelector.init();
    }
}
Also used : HashMap(java.util.HashMap) ServerInfo(de.dytanic.cloudnet.lib.server.info.ServerInfo) BukkitMobInitEvent(de.dytanic.cloudnet.bridge.event.bukkit.BukkitMobInitEvent) MobSelector(de.dytanic.cloudnet.bridge.internal.serverselectors.MobSelector) ServerMob(de.dytanic.cloudnet.lib.serverselectors.mob.ServerMob) TypeToken(com.google.gson.reflect.TypeToken) UUID(java.util.UUID) MobConfig(de.dytanic.cloudnet.lib.serverselectors.mob.MobConfig) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

TypeToken (com.google.gson.reflect.TypeToken)1 BukkitMobInitEvent (de.dytanic.cloudnet.bridge.event.bukkit.BukkitMobInitEvent)1 MobSelector (de.dytanic.cloudnet.bridge.internal.serverselectors.MobSelector)1 ServerInfo (de.dytanic.cloudnet.lib.server.info.ServerInfo)1 MobConfig (de.dytanic.cloudnet.lib.serverselectors.mob.MobConfig)1 ServerMob (de.dytanic.cloudnet.lib.serverselectors.mob.ServerMob)1 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1 ItemStack (org.bukkit.inventory.ItemStack)1