Solved Cast custom mob to living entity

Discussion in 'Plugin Development' started by MrDaniel, Nov 10, 2020.

  1. Offline

    MrDaniel

    Hello there gamers, I am trying to create a custom zombie entity that doesn't despawn. The way I do that, is by doing this funny line of code
    Code:
    livingentity.setRemoveWhenFarAway(false);
    The problem is, how do I get the living entity when I am attempting to spawn it. I have tried casting the custom zombie to a living entity but it throws ClassCastException
    Stacktrace (open)
    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'zombie' in plugin ApocalypseZ v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at org.bukkit.craftbukkit.v1_16_R2.CraftServer.dispatchCommand(CraftServer.java:758) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at net.minecraft.server.v1_16_R2.PlayerConnection.handleCommand(PlayerConnection.java:1697) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at net.minecraft.server.v1_16_R2.PlayerConnection.a(PlayerConnection.java:1540) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at net.minecraft.server.v1_16_R2.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at net.minecraft.server.v1_16_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at net.minecraft.server.v1_16_R2.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at net.minecraft.server.v1_16_R2.TickTask.run(SourceFile:18) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.executeTask(SourceFile:144) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at net.minecraft.server.v1_16_R2.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.executeNext(SourceFile:118) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at net.minecraft.server.v1_16_R2.MinecraftServer.ba(MinecraftServer.java:941) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at net.minecraft.server.v1_16_R2.MinecraftServer.executeNext(MinecraftServer.java:934) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.awaitTasks(SourceFile:127) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at net.minecraft.server.v1_16_R2.MinecraftServer.sleepForTick(MinecraftServer.java:918) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at net.minecraft.server.v1_16_R2.MinecraftServer.w(MinecraftServer.java:850) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at net.minecraft.server.v1_16_R2.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_271]
    Caused by: java.lang.ClassCastException: eu.ladeira.apocalypse.Infected cannot be cast to org.bukkit.entity.LivingEntity
        at eu.ladeira.apocalypse.commands.Zombie.onCommand(Zombie.java:26) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-dea4138]
        ... 18 more
    


    Command to spawn class:
    Code:
    package eu.ladeira.apocalypse.commands;
    
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.v1_16_R2.CraftWorld;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
    
    import eu.ladeira.apocalypse.Infected;
    
    public class Zombie implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                System.out.println("ERROR: This command may be only run by players");
                return false;
            }
            Player p = (Player) sender;
            Location loc = p.getLocation();
           
            Infected inf = new Infected(loc);
            LivingEntity le = (LivingEntity) inf;
            le.setRemoveWhenFarAway(false);
            ((CraftWorld)loc.getWorld()).getHandle().addEntity(inf, SpawnReason.CUSTOM);
            return false;
        }
    
    }
    
    Custom zombie class
    Code:
    package eu.ladeira.apocalypse;
    
    
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_16_R2.CraftWorld;
    
    import net.minecraft.server.v1_16_R2.AttributeProvider;
    import net.minecraft.server.v1_16_R2.EntityMonster;
    import net.minecraft.server.v1_16_R2.EntityZombie;
    import net.minecraft.server.v1_16_R2.GenericAttributes;
    
    
    public class Infected extends EntityZombie {
        public Infected(Location loc) {
            super(((CraftWorld)loc.getWorld()).getHandle());
            this.setPosition(loc.getX(), loc.getY(), loc.getZ());
        }
       
        public static AttributeProvider.Builder eS() {
            return EntityMonster.eR().a(GenericAttributes.FOLLOW_RANGE, 2D).a(GenericAttributes.MOVEMENT_SPEED, 0.4D).a(GenericAttributes.ATTACK_DAMAGE, 3.0D).a(GenericAttributes.ARMOR, 2.0D).a(GenericAttributes.SPAWN_REINFORCEMENTS);
        }
    }
    
     
  2. Offline

    KarimAKL

    @MrDaniel EntityZombie is not a LivingEntity. LivingEntity is an interface in the Bukkit API, and EntityZombie is a class in NMS.
     
  3. Offline

    MrDaniel

    How would ago about preventing it from despawning? Or getting the newly spawned mib as a LivingEntity. The Craft world method returns a boolean, not the entity spawned.

    Sent from my motorola one vision using Tapatalk
     
  4. Offline

    KarimAKL

    @MrDaniel Try CraftWorld#addEntity(net.minecraft.server.Entity entity, SpawnReason reason) or net.minecraft.server.Entity#getBukkitEntity().
    One gets the Bukkit entity object, while the other spawns the entity.
     
  5. Offline

    MrDaniel

    getting the bukkit entity using getBukkitEntity() and then casting it to a LivingEntity worked, thank you
     
    KarimAKL likes this.

Share This Page