Solved Trying to make a CusotmEntity class.

Discussion in 'Spigot Plugin Development' started by Sean0402, Mar 2, 2020.

Thread Status:
Not open for further replies.
  1. Hey,

    I'm attempting to create my Object class for entities. I'm not familiar with entities really at all with mobs so this is all a bit new.

    This is what I have so far;
    Code (Java):
    public class CustomEntity {

        private String name;
        private EntityType entityType;
        private Location location;
        private Entity entity;

        public CustomEntity(String name, Entity entity, EntityType entityType, Location location) {
            this.name = name;
            this.entityType = entityType;
            this.location = location;
            this.entity = entity;
            spawnCustomEntity(location);
        }

        public String getName() {
            return this.name;
        }

        public void setCustomName(String name) {
            entity.setCustomName(Utilities.color(name));
        }

        public EntityType getEntityType() {
            return this.entityType;
        }

        public void setBaby() {
           fixed this part.
        }

        public void spawnCustomEntity(Location location) {
            location.getWorld().spawnEntity(location, getEntityType());
            setCustomName(getName());
        }
    Please point out everything I'm doing wrong. Trying to learn on this. If you recommend anything extra, that'd be much appreciated also.

    I have an issue with setting the entity name since the entity I enter is me (a player) but what else am I meant to set it to since I already declare an entity type?

    Code (Java):
    new CustomEntity("&6test", p, EntityType.ZOMBIE, p.getLocation());
    The issue is with the "p" on the second part being the player. What else am I meant to put it as?
     
    #1 Sean0402, Mar 2, 2020
    Last edited: Mar 2, 2020
  2. Thats not a utility class.
     
    • Agree Agree x 1
  3. Ok. I renamed it. Any pointers?
     
  4. The entity needs to be instanceof Ageable before you can set it's age. Also, to set their name, you use the setCustomName method provided they are instanceof Nameable. To change a player's name, I believe you need to use packets.

    (Also, that's an Object not a utility class)
     
    • Informative Informative x 1
  5. Thanks. I managed to fix the baby stuff as soon as you posted this.

    As for the custom names, I'm not looking to change the players name, I'm trying to grab the entity type I'm spawning. but it's of course printing out me (the player executing the command) since I am the entity running the command.

    I'm unsure on how to grab the entity I am spawning? Is there any way to cast EntityType to Entity?

    In short; I want to name the Zombie I am spawning and grab that zombie.
     
  6. Seems like you're creating a new Entity object in the spawnCustomEntity method you have. Set your (this.entity) instance to the new Entity you spawn, then that instance will be your spawned entity.

    This will basically change the Entity instance in your object to the one spawned.
     
    • Winner Winner x 1
  7. Thanks. Solved :)
     
Thread Status:
Not open for further replies.