Exception involving CraftAnimals.getNearbyEntities()

Discussion in 'Plugin Development' started by TheTinyMan, Apr 13, 2011.

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

    TheTinyMan

    Hi! I'm working on a plugin to cause animals to persist, form herds, and reproduce! This is the first time I've really tried to do anything in Java, and also the first time I've posted online asking for help with code, so please forgive me if I commit any major blunders!

    Unfortunately, I'm having some trouble getting herds to assimilate existing animals of the appropriate type. I'm getting an exception that seems to imply that there's a problem with getNearbyEntities(), or my call to it. Here's the log, including a little bit of extra telling me how far the code got:


    Code:
    //Server Log:
    22:02:29 [INFO] A new COW herd has been created!  Its alpha is at Location{world=CraftWorld{name=world}x=144.5y=68.0z=-56.5pitch=0.0yaw=332.86557}, an
    d it will grow in 1000 milliseconds!
    22:02:29 [SEVERE] Could not pass event CREATURE_SPAWN to Herding
    java.lang.NoSuchMethodError: org.bukkit.craftbukkit.entity.CraftAnimals.getNearbyEntities(DDD)Ljava/util/List;
            at me.thetinyman.Herding.Herds.Herd.AssimilateNearbyAnimals(Herd.java:74)
            at me.thetinyman.Herding.Herds.Herd.<init>(Herd.java:68)
            at me.thetinyman.Herding.Herds.CowHerd.<init>(CowHerd.java:24)
            at me.thetinyman.Herding.Herding.AddToNewHerd(Herding.java:207)
            at me.thetinyman.Herding.Herding.beginAssigningAnimalToHerd(Herding.java:166)
            at me.thetinyman.Herding.Herding.considerAssigningToHerd(Herding.java:122)
            at me.thetinyman.Herding.Listeners.HerdingSpawnListener.onCreatureSpawn(HerdingSpawnListener.java:29)
            at org.bukkit.plugin.java.JavaPluginLoader$44.execute(JavaPluginLoader.java:435)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:59)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:255)
            at org.bukkit.craftbukkit.event.CraftEventFactory.callCreatureSpawnEvent(CraftEventFactory.java:228)
            at net.minecraft.server.World.a(World.java:724)
            at net.minecraft.server.SpawnerCreature.a(SourceFile:148)
            at net.minecraft.server.World.h(World.java:1384)
            at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:359)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:283)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:375)
    22:02:30 [INFO] Herd is considering growing.  //I'm not sure if this was the most recent herd or an earlier one, but all herds had this same exception.
    Here is the source for the classes

    in question. I've marked lines 74 and 68 in Herd, as well as 24 in CowHerd, with "//****"s.



    Code:
    //  Herd.java
    
    package me.thetinyman.Herding.Herds;
    
    import java.util.List;
    import java.util.ArrayList;
    import java.util.Random;
    import java.util.Timer;
    import java.util.TimerTask;
    
    import me.thetinyman.Herding.HerdGrowthTask;
    import me.thetinyman.Herding.Herding;
    
    import org.bukkit.Location;
    import org.bukkit.block.BlockFace;
    import org.bukkit.craftbukkit.entity.CraftAnimals;
    import org.bukkit.entity.Animals;
    import org.bukkit.entity.CreatureType;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.plugin.Plugin;
    
    //import com.elmakers.mine.bukkit.persistence.annotation.PersistField;
    //import com.elmakers.mine.bukkit.persistence.annotation.PersistClass;;
    
    //@PersistClass(name = "GenericHerd", schema = "Herding")
    public abstract class Herd<AnimalType extends CraftAnimals>
    {
        final boolean DO_ALPHAS_FLAME = true;
        // @PersistField(id=true, auto=true)
        public int id;
        /*
         * static int assimilationDistance = 2; static int territorialDistance =
         * assimilationDistance * 2; static int maxHerdSize = 6;
         */
    
        public AnimalType _alpha;
        public ArrayList<AnimalType> _members;
        static Random random = new Random();
    
        private Herding _plugin;
        protected Timer _growthTimer;
    
        public Herd(Herding plugin, AnimalType alpha, int growthFrequency,
                int maxHerdSize, int assimilationDistance, int territorialDistance,
                int progressTowardGrowth)
        {
            _plugin = plugin;
            setAlpha(alpha);
            setHerdGrowthFrequency(growthFrequency);
            setMaxHerdSize(maxHerdSize);
            setAssimilationDistance(assimilationDistance);
            setTerritorialDistance(territorialDistance);
    
            setHerdMembers(new ArrayList<AnimalType>());
            getHerdMembers().add(alpha);
    
            _growthTimer = new Timer();
    
            int growthTime = growthFrequency - progressTowardGrowth;
            _growthTimer.schedule(new HerdGrowthTask<AnimalType>(_plugin, this),
                    growthTime);
    
            String typeString = getCreatureType().toString();
            String alphaLocation = _alpha.getLocation().toString();
            _plugin.LogMessage("A new " + typeString
                    + " herd has been created!  Its alpha is at " + alphaLocation
                    + ", and it will grow in " + growthTime + " milliseconds!");
    
            AssimilateNearbyAnimals();   //****Line 68
        }
    
        @SuppressWarnings("unchecked")
        private void AssimilateNearbyAnimals()
        {
            List<Entity> nearbyEntities = _alpha.getNearbyEntities(   //****Line 74
                    getAssimilationDistance(), getAssimilationDistance(),
                    getAssimilationDistance());
    
            for (Entity nearbyEntity : nearbyEntities)
            {
                if (nearbyEntity != _alpha)
                {
                    // assuming that this is the cheaper comparison
                    if (nearbyEntity.getLocation().toVector()
                            .distance(_alpha.getLocation().toVector()) < getAssimilationDistance())
                    {
                        if (nearbyEntity instanceof CraftAnimals)
                        {
                            if (nearbyEntity.getClass() == _alpha.getClass())
                            {
                                //make sure that if the IFs get re-ordered, this cast
                                //  stays inside "if (nearbyEntity instanceof CraftAnimals)"
                                CraftAnimals nearbyAnimal = (CraftAnimals)nearbyEntity;
                                if(!_plugin.isAnimalInAnyHerd(nearbyAnimal,getCreatureType()))
                                {
                                    _plugin.LogMessage("     A " + getCreatureType().toString() + " is being assimilated into this herd!  Its entity ID is " + nearbyAnimal.getEntityId() +", and it is at " + nearbyAnimal.getLocation().toString() + "!");
                                    AssimilateNewMember((AnimalType)nearbyAnimal);
                                }
                            }
                        }
                    }
                }
            }
    
        }
    
        public void BirthMembers()
        {
    
            if (getHerdMembers().size() >= getMaxHerdSize())
            {
    
                Location potentialLocation = _alpha.getLocation();
                LivingEntity newMember = null;
                int attemptsRemaining = 5;
                while (attemptsRemaining > 0 && newMember == null)
                {
                    newMember = _alpha.getWorld().spawnCreature(
                            GetRandomNearbyLocation(potentialLocation),
                            getCreatureType());
                    attemptsRemaining--;
                }
    
                if (newMember == null)
                {
                    _plugin.LogMessage("A new "
                            + getCreatureType().toString()
                            + " was to be born in a herd, but it could find no place among its fellows.");
                } else
                {
                    _plugin.LogMessage("A new " + getCreatureType().toString()
                            + " has been born in a herd!  Its location is "
                            + newMember.getLocation().toString()
                            + " and its alpha's location is "
                            + _alpha.getLocation().toString() + "!  It had "
                            + attemptsRemaining
                            + " attempts to find a location remaining.");
                }
            }
            CreateNextTimerTask();
        }
    
        private void CreateNextTimerTask()
        {
            _growthTimer.schedule(new HerdGrowthTask<AnimalType>(_plugin, this),
                    getHerdGrowthFrequency());
        }
    
        private Location GetRandomNearbyLocation(Location source)
        {
            int randomValue = random.nextInt(BlockFace.values().length - 1);
            BlockFace face = BlockFace.values()[randomValue];
            return source.getBlock().getRelative(face).getLocation();
        }
    
        public AnimalType getAlpha()
        {
            return _alpha;
        }
    
        public void setAlpha(AnimalType newAlpha)
        {
            if (DO_ALPHAS_FLAME && _alpha != null)
            {
                _alpha.setFireTicks(0);
            }
            _alpha = newAlpha;
    
            if (DO_ALPHAS_FLAME)
            {
                newAlpha.setFireTicks(Integer.MAX_VALUE);
            }
        }
    
        public ArrayList<AnimalType> getHerdMembers()
        {
            return _members;
        }
    
        public void setHerdMembers(ArrayList<AnimalType> newHerdMembers)
        {
            _members = newHerdMembers;
        }
    
        public abstract int getHerdGrowthFrequency();
    
        public abstract void setHerdGrowthFrequency(int newFrequency);
    
        public abstract int getAssimilationDistance();
    
        public abstract void setAssimilationDistance(int newAssimilationDistance);
    
        public abstract int getTerritorialDistance();
    
        public abstract void setTerritorialDistance(int newTerritorialDistance);
    
        public abstract int getMaxHerdSize();
    
        public abstract void setMaxHerdSize(int newMaxHerdSize);
    
        public abstract CreatureType getCreatureType();
    
        public void AssimilateNewMember(AnimalType newMember)
        {
            if (getMaxHerdSize() == getHerdMembers().size())
            {
                return;
            }
    
            getHerdMembers().add(newMember);
            newMember.setTarget(_alpha);
        }
    
    }
    


    Code:
    //CowHerd.java
    
    package me.thetinyman.Herding.Herds;
    
    import me.thetinyman.Herding.Herding;
    
    import org.bukkit.craftbukkit.entity.CraftCow;
    import org.bukkit.entity.CreatureType;
    
    public class CowHerd extends Herd<CraftCow>
    {
    
        static int defaultAssimilationDistance = 4;
        static int defaultTerritorialDistance = defaultAssimilationDistance * 2;
        static int defaultMaxHerdSize = 6;
        static int defaultGrowthFrequency = 1000;// 60 minutes // normally 60 min
    
        int assimilationDistance;
        int territorialDistance = assimilationDistance;
        int maxHerdSize ;
        int growthFrequency ;
    
        public CowHerd(Herding plugin, CraftCow alpha)
        {
            super(plugin, alpha, defaultGrowthFrequency, defaultMaxHerdSize,
                    defaultAssimilationDistance, defaultTerritorialDistance, 0);     //****Line 24
    
            assimilationDistance = defaultAssimilationDistance;
            territorialDistance = defaultTerritorialDistance;
            maxHerdSize = defaultMaxHerdSize;
            growthFrequency = defaultGrowthFrequency;
        }
    
        @Override
        public int getHerdGrowthFrequency()
        {
            return growthFrequency;
        }
    
        @Override
        public void setHerdGrowthFrequency(int newFrequency)
        {
            growthFrequency = newFrequency;
    
        }
    
        @Override
        public int getAssimilationDistance()
        {
            return assimilationDistance;
        }
    
        @Override
        public void setAssimilationDistance(int newAssimilationDistance)
        {
            assimilationDistance = newAssimilationDistance;
    
        }
    
        @Override
        public int getTerritorialDistance()
        {
            return territorialDistance;
        }
    
        @Override
        public void setTerritorialDistance(int newTerritorialDistance)
        {
            territorialDistance = newTerritorialDistance;
    
        }
    
        @Override
        public int getMaxHerdSize()
        {
            return maxHerdSize;
        }
    
        @Override
        public void setMaxHerdSize(int newMaxHerdSize)
        {
            maxHerdSize = newMaxHerdSize;
        }
    
        @Override
        public CreatureType getCreatureType()
        {
            return CreatureType.COW;
        }
    }
    

    And yeah, as I posted this I realized that I'm setting the Herd fields in both the subclass and the superclass. Oops! I'll fix that one soon ^.^


    Anyways, I'm not sure how to approach debugging that particular exception. Can anyone give me some guidance?
    Thanks for your help!
     
  2. Offline

    Edward Hand

    Ensure the version of craftbukkit/bukkit running on your test server is up to date.
     
    TheTinyMan likes this.
  3. Offline

    TheTinyMan

    *facepalm* wow, you'd think I'd have thought of that . Thanks, that worked! ^.^
     
Thread Status:
Not open for further replies.

Share This Page