1.12.2 Constructor issues. Unsure how to go about it really

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

  1. Hey all, I am wondering how would I go about calling my getEntity from my Object class.

    Entity stuff:
    Code (Java):
    private Entity entity;

    public Entity getEntity() {
            return this.entity;
        }

    public void spawnCustomEntity(Location location) {
            this.entity = location.getWorld().spawnEntity(location.clone().add(0.5, 1.0, 0.5), getEntityType());
    }
     
    It sets the entity just fine.

    Now, the constructors;(I have 2),
    Code (Java):
    public CustomEntity() {
            super();
        }

        public CustomEntity(String name, EntityType entityType, Location location, int maxEntities) {
            this.name = name;
            this.entityType = entityType;
            this.location = location;
            this.maxEntities = maxEntities;
            spawnCustomEntity(location);
        }
    How I'm trying to call getEntity(),
    Code (Java):
    @EventHandler
        public void onEntityTouch(EntityDamageByEntityEvent e) {
            CustomEntity custom = new CustomEntity();
            Bukkit.broadcastMessage("Entity: " + custom.getEntity());
            if (e.getDamager().equals(custom.getEntity())) {
                Bukkit.broadcastMessage("Entity: " + e.getDamager() + " touched: " + e.getEntity());
            }
        }
    Of course, it returns null. but I'm unsure of how to go about doing this properly without calling the other constructor with all the information as that seems unnecessary?
     
    #1 Sean0402, Mar 5, 2020
    Last edited: Mar 5, 2020
  2. ? how are you constructing the object?
    if you use the argumentless constructor, then the entity never gets created, and you just have an empty object..
     
    • Agree Agree x 1
  3. If this is your constructor:

    Code (Text):
    public CustomEntity(String name, EntityType entityType, Location location, int maxEntities) {
            this.name = name;
            this.entityType = entityType;
            this.location = location;
            this.maxEntities = maxEntities;
            spawnCustomEntity(location);
        }
    Then:
    Code (Text):
    CustomEntity custom = new CustomEntity();
    Should be something like:

    Code (Text):
    CustomEntity custom = new CustomEntity(name, type, location, maxEntities);
    Otherwise you just have an empty object.

    But it appears you already know this:
    Without giving it information it's just going to be empty/null.
     
  4. In the super constructor, could I simply call, “this.entity = getEntity()”? Or would
    It still need all the information? I feel like there is a way to call that entity again without writing out all the location, name and all the other info over again.
     
  5. I suggest taking a look at:
    https://www.javatpoint.com/super-keyword

    There might be a way you can with casting CustomEntity on Entity#getEntity (dont quote me on that), but the way you have it set up right now, you still need to give it all the info
     
    #5 FyrenGaming, Mar 5, 2020
    Last edited: Mar 5, 2020
  6. i dont think you understand how constructors work. if you've passed the arguments through the argument constructor once, you use that object. if you construct the object with the argumentless constructor, you have an entirely new, separate object with nothing in it. you cant keep creating the object to get access to the entity. thats not how objects work. you need to create it once with your arguments and reference the customentity in a field soemwhere that you can obtain access to it.