Filter for mob target

Discussion in 'Plugin Development' started by fresser123, Apr 6, 2013.

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

    fresser123

    Hello out there again.

    Im again trying to make some turrets. Now i have got my turrets to work (Spawn despawn and the right damage)

    But. This skeleton, can i make a filter for the skeleton target? - I have 2 arrays (Actually 3, but this doesnt matter for this) "blue" and "red" - Im currently trying to make the turret attack persons from blue, and not from red.

    I have tried "onEntityTargetLivingEntity" - at this way:
    Code:
    @EventHandler
    public void onEntityTargetLivingEntity(EntityTargetLivingEntityEvent e){
    if(e.getTarget().getType() == EntityType.PLAYER){
    if(e.getEntity() == plugin.NexusRed1 || e.getEntity() == plugin.NexusRed2){
    Player p = (Player) e.getTarget();
    if(plugin.red.contains(p.getName())){
     
    e.setCancelled(true);
    }
    }
    }
    }
    
    It works... Kinda. But if theres a "red" player inside the skeleton range, the skeleton will try to target that person, then get it cancelled, and maybe continue doing this. So there sometimes is spent 10 seconds before turret is targeting the right player.

    Is it possible to make this working?
     
  2. Offline

    stirante

    fresser123
    You could write your own target selector for skeleton and add it through reflection.
     
  3. Offline

    fresser123

    Ty for trying to help me.
    Im pretty new to java, so some simple solutions or some premade code could be great :D

    Thanks you ahead.
     
  4. Offline

    stirante

    fresser123
    I wrote it in notepad so it will require some work. Every import should be from nms and i think lists should be List<Entity> but i'm not sure.
    Code:
    package com.stirante.Example;
     
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.List;
     
    public class NexusAI extends PathfinderGoalTarget {
     
        EntityLiving a;
        Class b;
        int c;
        private final IEntitySelector g;
        private DistanceComparator h;
     
        public PathfinderGoalNearestAttackableTarget(EntityLiving entityliving, Class oclass, float f, int i, boolean flag) {
            this(entityliving, oclass, f, i, flag, false);
        }
     
        public PathfinderGoalNearestAttackableTarget(EntityLiving entityliving, Class oclass, float f, int i, boolean flag, boolean flag1) {
            this(entityliving, oclass, f, i, flag, flag1, (IEntitySelector) null);
        }
     
        public PathfinderGoalNearestAttackableTarget(EntityLiving entityliving, float f, int i, boolean flag, boolean flag1, IEntitySelector ientityselector) {
            super(entityliving, f, flag, flag1);
            this.e = f;
            this.c = i;
            this.h = new DistanceComparator(this, entityliving);
            this.g = ientityselector;
            this.a(1);
        }
     
        public boolean a() {
            if (this.c > 0 && this.d.aE().nextInt(this.c) != 0) {
                return false;
            } else {
                    List list2 = this.d.world.a(this.b, this.d.boundingBox.grow((double) this.e, 4.0D, (double) this.e), this.g);
     
                    Collections.sort(list2, this.h);
                    List list = new ArrayList();
                    for (int i = 0;i<list2.size();i++){
                        // TODO: Here filter and add every attackable entity to list 'list'
                    }
                    Iterator iterator = list.iterator();
     
                    while (iterator.hasNext()) {
                        Entity entity = (Entity) iterator.next();
                        EntityLiving entityliving = (EntityLiving) entity;
     
                        if (this.a(entityliving, false)) {
                            this.a = entityliving;
                            return true;
                        }
                    }
                }
     
                return false;
            }
        }
     
        public void c() {
            this.d.setGoalTarget(this.a);
            super.c();
        }
    }
    Simplest solution would be to create new class extending EntitySkeleton but now i'll do method which will do the work.
    Code:
    public void changeAI(Skeleton skeleton) {
        try{
            EntitySkeleton nmsSkeleton = ((CraftSkeleton)skeleton).getHandle();
            Field selector = EntitySkeleton.class.getDeclaredField("targetSelector");
            selector.setAccessible(true);
            PathfinderGoalSelector goals = (PathfinderGoalSelector) selector.get(nmsSkeleton);
            Field a = PathfinderGoalSelector.class.getDeclaredField("a");
            a.setAccessible(true);
            Field b = PathfinderGoalSelector.class.getDeclaredField("b");
            b.setAccessible(true);
            a.set(goals, new ArrayList());
            b.set(goals, new ArrayList());
            goals.a(1, new PathfinderGoalHurtByTarget(nmsSkeleton, false));
            goals.a(2, new NexusAI(this, 16.0F, 0, true));
        } catch (Throwable t){ t.printStackTrace(); }
    }
    This may (or even have to :p) contain errors it should be easy to fix.
     
  5. Offline

    fresser123

    stirante
    Wao, ty.
    But as said before, im still learning to make plugins.
    Where should i paste this code? :confused:
     
  6. Offline

    stirante

    First piece of code is seperate class. Change package to yours and name if you don't like this one. There is one TODO comment which you have to 'fill'.Second is method which you have to use on skeleton in order to change his AI.
     
  7. Offline

    fresser123

    stirante
    What do i write to implement the class?
    and, how do i use that "method"?
     
  8. Offline

    stirante

    Create new class named NexusAI and paste there first code and then when you spawn your nexus skeleton use this method like changeAI(nexusSkeleton).
     
  9. Offline

    fresser123

    Nice, just one problem, too many errors i cant resolve. :(
    I know you may have many other things to do than carrying a java-newb. But i appreciate it much.
     
Thread Status:
Not open for further replies.

Share This Page