Custom Entities

Discussion in 'Spigot Plugin Development' started by Heraldry, Jul 30, 2018.

  1. So previously when working with a custom "entity" I basically made an object which held information about the entity and had an awkward work around in which I used an NBT tag as a UUID to get the information about the horse from a Hashmap.
    As I'm currently recoding my plugin I thought other than awkwardly replicating this I would attempt to create a custom entity that would be able to work by itself rather than storing information about the entity in a list.
    So I've looked at a few tutorials (Haven't went into making the entity) but I'm having an issue with the theory of it.
    What I'm trying to do is essentially create a CustomHorse but this wouldn't just fall under the Horse entity but also other types such as the Donkey, Mule, SkeletonHorse, ZombieHorse etc.
    I was wondering if it'd be possible to create this custom entity which would be able to have the entity types of above.
    Any help would be greatly appreciated and if I haven't explained this too well I'm more than happy to try and explain it a little better.
     
  2. BananaPuncher714

    Supporter

  3. I know but EntityHorse is only that one entity, I wanted to create an overall entity which could have different in-game types such as the Skeleton Horse, Horse and Zombie Horse etc.
     
  4. I think I might be misunderstanding, but with BananaPuncher's suggestion I believe now all you'd have to do is create the entity classes for each entity type you want to spawn

    edited since was wrong/misleading
     
    #4 FakeSaint, Jul 30, 2018
    Last edited: Jul 31, 2018
    • Agree Agree x 1
  5. Yeah but I don’t want 5 separate custom entities, I was hoping there’d be a way that would be able to have one general custom entity just different types. I don’t really want a bunch of repeating code etc
     
  6. I think when you spawn the entity, you have the option to set the type of the horse? I am not so sure on 1.13
     
  7. Ah ok, I'm not exactly sure how the nms entities are structured. To my knowledge it USED to be that different horse types were part of the same base entity class. I believe now they are separate entities that inherit from the horse base class, but to create custom versions of each you will need to create custom classes for each. In the past you'd have been able to just create the custom horse, then set the variant of that custom horse that spawns, but I don't think so anymore.

    Now since they are separate entities I think you have to create custom versions of each "variant". Even though the nms Skeleton Horse and nms Zombie horse inherit from the same base class I don't see a way to accomplish what you want without creating custom classes for each variant, since each "variant type" is actually its own entity class now (I think).

    However! To avoid "repeating code" you could instead create a custom interface for your Custom Skeleton/Zombie Horse ect classes to implement. Then wrap your custom attribute/behavior implementations into a static helper class and use Java's default methods (Java 8) within your interface in order to reuse as much code as possible. I haven't tried something like this myself in Spigot, but I think that's the way to go.

    tldr I think the best way to do this is with an interface + potentially a helper class with implementation. SkeletonHorses, NormalHorses, ZombieHorses ect are different entities, not different types of the same entity anymore
     
    #7 FakeSaint, Jul 31, 2018
    Last edited: Jul 31, 2018
  8. well as I am lazy as hell when creating custom entities what I would do is create a public enum EntityType in my plugins main class

    Code (Text):
        public enum CustomEntityType {
            HORSE1, HORSE2, ETCHORSE
        }
    Then add that enum as an entity trait or field to my custom entity
    Code (Text):
    public class MyCustomEntityClassSample {
        private CustomEntityType type = CustomEntityType.HORSE1;

        protected CustomEntityType getType() {
            return type;
        }

        protected void setType(CustomEntityType type) {
            this.type = type;
        }

    }
     
  9. I did some more research, EntityHorse, EntityHorseSkeleton, and EntityHorseZombie inherit from EntityHorseAbstract in nms. I don't know if it would be possible to create a custom EntityHorseAbstract Class that custom EntityHorseClasses ect could inherit from. The issue I see is that any Custom Horse Class will extend one of the nms classes, which extend the default EntityHorseAbstract class.

    Would be cool, but I don't think its possible/practical. I think Interfaces are the way to go
     
    #9 FakeSaint, Jul 31, 2018
    Last edited: Jul 31, 2018