Search in sources :

Example 1 with MyPetMinecraftEntity

use of de.Keyle.MyPet.api.entity.MyPetMinecraftEntity in project MyPet by xXKeyleXx.

the class MyPet method createEntity.

public SpawnFlags createEntity() {
    lastUsed = System.currentTimeMillis();
    if (status != PetState.Here && getOwner().isOnline()) {
        Player owner = getOwner().getPlayer();
        if (owner.isDead()) {
            status = PetState.Despawned;
            return SpawnFlags.OwnerDead;
        }
        if (owner.getGameMode().name().equals("SPECTATOR")) {
            return SpawnFlags.Spectator;
        }
        if (respawnTime <= 0) {
            Location loc = petOwner.getPlayer().getLocation();
            if (!WorldGroup.getGroupByWorld(loc.getWorld().getName()).getName().equals(getWorldGroup())) {
                return SpawnFlags.WrongWorldGroup;
            }
            if (owner.isFlying()) {
                boolean groundFound = false;
                for (int i = 10; i >= 0; i--) {
                    Block b = loc.getBlock();
                    if (b.getRelative(BlockFace.DOWN).getType().isSolid()) {
                        groundFound = true;
                        break;
                    }
                    loc = loc.subtract(0, 1, 0);
                }
                if (!groundFound) {
                    return SpawnFlags.Flying;
                }
            }
            MyPetCallEvent event = new MyPetCallEvent(this);
            Bukkit.getServer().getPluginManager().callEvent(event);
            if (event.isCancelled()) {
                return SpawnFlags.NotAllowed;
            }
            MyPetMinecraftEntity minecraftEntity = MyPetApi.getEntityRegistry().createMinecraftEntity(this, loc.getWorld());
            if (minecraftEntity == null) {
                status = PetState.Despawned;
                return SpawnFlags.Canceled;
            }
            bukkitEntity = minecraftEntity.getBukkitEntity();
            if (MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.9") >= 0) {
                Random r = new Random(petOwner.getInternalUUID().toString().hashCode());
                String random = RandomStringUtils.random(10, 0, 0, true, true, null, r);
                Team t;
                if (owner.getScoreboard().getTeam("MyPet-" + random) != null) {
                    t = owner.getScoreboard().getTeam("MyPet-" + random);
                } else {
                    t = owner.getScoreboard().registerNewTeam("MyPet-" + random);
                    t.setOption(Team.Option.COLLISION_RULE, Team.OptionStatus.NEVER);
                }
                for (String entry : t.getEntries()) {
                    t.removeEntry(entry);
                }
                t.addEntry(minecraftEntity.getUniqueID().toString());
            }
            if (getYSpawnOffset() > 0) {
                loc = loc.add(0, getYSpawnOffset(), 0);
            }
            loc.setPitch(0);
            loc.setYaw(0);
            Location origin = loc.clone();
            boolean positionFound = false;
            loc.subtract(1, 0, 1);
            for (double x = 0; x <= 2; x += 0.5) {
                for (double z = 0; z <= 2; z += 0.5) {
                    if (x != 1 && z != 1) {
                        minecraftEntity.setLocation(loc);
                        if (MyPetApi.getPlatformHelper().canSpawn(loc, minecraftEntity)) {
                            Block b = loc.getBlock();
                            if (b.getRelative(BlockFace.DOWN).getType().isSolid()) {
                                positionFound = true;
                                break;
                            }
                        }
                    }
                    loc.add(0, 0, 0.5);
                }
                if (positionFound) {
                    break;
                }
                loc.subtract(0, 0, 2);
                loc.add(0.5, 0, 0);
            }
            if (!positionFound) {
                minecraftEntity.setLocation(origin);
                if (!MyPetApi.getPlatformHelper().canSpawn(origin, minecraftEntity)) {
                    status = PetState.Despawned;
                    return SpawnFlags.NoSpace;
                }
            }
            if (MyPetApi.getEntityRegistry().spawnMinecraftEntity(minecraftEntity, loc.getWorld())) {
                bukkitEntity.setMetadata("MyPet", new FixedMetadataValue(MyPetApi.getPlugin(), this));
                status = PetState.Here;
                if (worldGroup == null || worldGroup.equals("")) {
                    setWorldGroup(WorldGroup.getGroupByWorld(loc.getWorld().getName()).getName());
                }
                autoAssignSkilltree();
                wantsToRespawn = false;
                return SpawnFlags.Success;
            }
            return SpawnFlags.Canceled;
        }
    }
    if (status == PetState.Dead) {
        return SpawnFlags.Dead;
    } else {
        return SpawnFlags.AlreadyHere;
    }
}
Also used : Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) Block(org.bukkit.block.Block) MyPetMinecraftEntity(de.Keyle.MyPet.api.entity.MyPetMinecraftEntity) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) Team(org.bukkit.scoreboard.Team) MyPetCallEvent(de.Keyle.MyPet.api.event.MyPetCallEvent) Location(org.bukkit.Location)

Aggregations

MyPetMinecraftEntity (de.Keyle.MyPet.api.entity.MyPetMinecraftEntity)1 MyPetCallEvent (de.Keyle.MyPet.api.event.MyPetCallEvent)1 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)1 Location (org.bukkit.Location)1 Block (org.bukkit.block.Block)1 Player (org.bukkit.entity.Player)1 FixedMetadataValue (org.bukkit.metadata.FixedMetadataValue)1 Team (org.bukkit.scoreboard.Team)1