Creating custom mob class then spawning mob

Discussion in 'Plugin Development' started by iLiveorLose, Apr 24, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    iLiveorLose

    Hey everyone, this thread kind of goes with my last about crafting recipes, it is the same plugin but a completely unrelated issue. I was wondering if it is possible to create a custom mob like a new type of Slime and spawn it with SlimeKing.class or something.

    What I have so far:
    Code:
    public class SlimeListener implements Listener {
    
        private Slime s;  
      
        public void onEnable() {
            s.setCustomNameVisible(true);
            s.setCustomName(ChatColor.GREEN.toString() + ChatColor.BOLD + "Slime King Boss");
            s.setSize(7);
            s.setMaxHealth(500);
            s.setHealth(s.getMaxHealth());
        }
    
    Code:
    @EventHandler
        public void onSlimeBossSpawn(PlayerInteractEvent e) {
            if (e.getPlayer().getInventory().getItemInHand().getType()
                    .equals(MegaSlime.slimeEgg) {
                if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                    s = e.getPlayer().getWorld().spawn(e.getPlayer().getLocation(), Slime.class);
                }
            }
        }
    
    What I want to do:
    Code:
    s = e.getPlayer().getWorld().spawn(e.getPlayer().getLocation(), KingSlime.class);
    
    Is this possible?
    Note: I'm guessing I'd have to make a KingSlime class and make it extend EntitySlime, but what do I do from there?
     
  2. Offline

    mcdorli

    Maybe test it.
     
  3. Why is the slime data in 'onEnable'? If you want to do KingSlime.class or whatever, you should make a method that is the same name of your class, instead of using onEnable. By doing that, you can use 'new (ClassName).class' instead of '(ClassName.class'. Also you shouldn't void the method, here is a format to help you understand how to use 'new (ClassName)',
    'public (ClassName)(Argument argument){
    //CODE HERE
    }'.

    P.S. It's case sensitive, like always!
     
    Last edited: Apr 24, 2016
  4. Offline

    Gonmarte

    I would say that it is.
    You should test it and then if you have a problem create a thread. Discover things by ourselfs is a lot more exciting than asking if it is possible :) (My view)
     
  5. Offline

    iLiveorLose

    @mcdorli : I get an error at the spawn code.
    Code:
    s = e.getPlayer().getWorld().spawn(e.getPlayer().getLocation(), KingSlime.class);
    
    "Bound mismatch: The generic method spawn(Location, Class<T>) of type World is not applicable for the arguments (Location, Class<KingSlime>). The inferred type KingSlime is not a valid substitute for the bounded parameter <T extends Entity>"

    Heres my KingSlime class:
    Code:
    package me.liveorlose.megaslime;
    
    import net.minecraft.server.v1_8_R3.EntitySlime;
    import net.minecraft.server.v1_8_R3.World;
    
    public class KingSlime extends EntitySlime{
    
        public KingSlime(World world) {
            super(world);
        }
       
    
    }
    
    @Gonmarte : Because I was asking what to do from there. You didn't read that part?

    @ZanctarianDevelopment : I dont exactly know what you mean. Where would I put the new KingSlime()? And there is a constructor that I had to create so do I just put the setHealth and stuff in there?
     
  6. Offline

    mcdorli

    Try to extend entity directly instead of entityslime
     
  7. Offline

    iLiveorLose

    @mcdorli : Uhhhhh.... I extended entity and there are a lot of unimplemented methods....
    Code:
        public KingSlime(World world) {
            super(world);
            // TODO Auto-generated constructor stub
        }
    
        @Override
        protected void a(NBTTagCompound arg0) {
            // TODO Auto-generated method stub
          
        }
    
        @Override
        protected void b(NBTTagCompound arg0) {
            // TODO Auto-generated method stub
          
        }
    
        @Override
        protected void h() {
            // TODO Auto-generated method stub
          
        }
    
    And it's the same error, tried cutting the spawn line out then re-pasting it, same error:
    Code:
    s = e.getPlayer().getWorld().spawn(e.getPlayer().getLocation(), KingSlime.class);
    
    "Bound mismatch: The generic method spawn(Location, Class<T>) of type World is not applicable for the arguments (Location, Class<KingSlime>). The inferred type KingSlime is not a valid substitute for the bounded parameter <T extends Entity>"
     
  8. 1. Under 'super' is where I recommend the custom data.
    2. Try adding 'new' before KingSlime.class in ur spawn statement. Like so: "new KingSlime.class".
    @iLiveorLose
     
  9. Offline

    timtower Administrator Administrator Moderator

  10. Offline

    iLiveorLose

    Then how would I do it?
     
  11. Offline

    timtower Administrator Administrator Moderator

    It takes an class as argument, not an instance, what you have is fine, at least the parameters are.
    Not in the mood now to take a good look, it is late.
     
  12. Offline

    iLiveorLose

    @ZanctarianDevelopment all of the data gets errors:
    Code:
    KingSlime.setCustomNameVisible(true);
    KingSlime.setCustomName(ChatColor.GREEN.toString() + ChatColor.BOLD
                   + "Slime King Boss");
    KingSlime.setSize(7);
    KingSlime.setMaxHealth(500);
    KingSlime.setHealth(KingSlime.getMaxHealth());
    
    Example: "Cannot make a static reference to the non-static method setCustomName(String) from the type Entity"
     
  13. Remove 'toString' and seperate ChatColor.GREEN + CharColor.BOLD with ChatColor.GREEN + "" + ChatColor.BOLD

    That will remove error ;)
     
  14. Offline

    mcdorli

    Nope, that's not it.
    you need an instance of the class to actually set it's name. Seriously, read those link we posted.
     
  15. Offline

    iLiveorLose

    Thats not the error.......

    Can anyone help? (consider this a bump)
     
  16. Offline

    Davesan

    You know, there are two type of Entity "class". One is the actual class for the server version, which is changing according to the current server version (per server jar file). The other is the org.bukkit.Entity, which is an interface. May you should try to implement that interface TOO. Like that:
    Code:
    package me.liveorlose.megaslime;
    import net.minecraft.server.v1_8_R3.EntitySlime;
    import net.minecraft.server.v1_8_R3.World;
    
    public class KingSlime extends EntitySlime implements org.bukkit.Entity {
        public KingSlime(World world) {
            super(world);
        }
    }
    Not sure if it works, but the error is clearly that your class is not inherits from the pointed Entity class/interface. The org.bukkit.World.spawn(Location, Class<T>) method expercts the T to implement the org.bukkit.Entity class. It is all, what is required for it to work. May there are some methods of the entity, which is accessed via reflection afterwards by bukkit (it is usual for bukkit devs to use such thing... especially met this when made my own event classes).

    Be warned: it is not a good practise to extend a "server-raw" class, such as the EntitySlime. These are the classes having the version in their path. On every version change, your plugin will throw error because of the missing class. However.. don't know if it is possible to make it version-independent. Maybe not. So that's it, try out to implement the interface too.

    And then it will works as some1's written it already:
    Code:
    World world = ...;
    Location location = ...;
    Entity e = world.spawn(location, KingSlime.class);
     
  17. Offline

    iLiveorLose

    I found another way to spawn without using a new custom class. I feel dumb for wasting everyone's time, but thanks for the help anyway, I will use that in case I ever need to use a custom class. Thanks again.
     
Thread Status:
Not open for further replies.

Share This Page