Minecart that acts like skeleton

Discussion in 'Plugin Development' started by Hex_27, Mar 8, 2015.

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

    Hex_27

    I want a stationary minecart that will look at players and shoot when nearby. Is there a way to do so? I'm new to NMS and I've tried a few things that I completely don't understand, even after looking at about 3 tutorials (One on spigot, one in bukkit, one about meteors). It needs some "a" function but the minecart just doesn't have it. What else can I try?
     
  2. @Hex_27 Checking the PlayerMoveEvent for any minecarts within range. If they are in range, then change the direction of the minecart to point at the player (try using something like setYaw for the minecart location) And then have them shoot an arrow in the direction which they are facing.
     
  3. Offline

    Hex_27

    @DJSkepter Not only is that lag-causing, it is also inaccurate and it can't shoot monsters. Not to mention theres not much of a way to identify the minecarts persistently.
     
    Last edited: Mar 8, 2015
  4. Offline

    Hex_27

    I still need help with this
     
  5. Offline

    CXdur

    @Hex_27

    Have you considered using a scheduler and then using the scheduler to check if players are nearby > if they are set it to look it at > spawn an arrow above and modify its velocity.

    I'm not sure if this is the best way to go, but it would at least work.
     
  6. Offline

    Hex_27

    @CXdur I would work... until the server reloads. Then how would I effectively identify the turrets to restart the schedulers?
     
  7. Offline

    bennie3211

    why would you want to know what minecart is a turret? You wanted to let every minecart work like a turret, else you need to save the ID of the minecart, and on disable and enable save and load it.
     
  8. Offline

    Hex_27

    @bennie3211 Why I want to know which minecart is a turret? Because I DON'T want all minecarts to work like turrets. Honestly, that question was unnecessary. Saving the ID of the minecart won't work well when players make turret after turret. There would be an overwhelming file full of ids. That's why I want a custom entity.
     
  9. Offline

    bennie3211

    It was necessary because you didn't mentioned that anywhere before. But why wouldn't that work? You only save the turrets and if it they break the turret you just remove the ID from the list
     
  10. Offline

    Hex_27

    @bennie3211 this is honestly not just about turrets. I'm also trying to use this as a base to learn nms. So I would prefer the use of nms for this.
     
  11. Offline

    Hex_27

    I still need help with this
     
  12. Offline

    Hex_27

    I still need help with this
     
  13. Loop through minecart.getNearbyEntities(x, y, z);
    (x,y,z are the radius)
    But you need to make sure the nearby entity is a player or monster because for example items are too entities

    and make a Runnable which executes every 2 seconds or something. That wouldn't lag much

    add metadatas to the minecart you want to be a turret.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  14. Offline

    Hex_27

    @FisheyLP this is what I'm using now. The snowballs are REALLY glitchy, not to mention that they don't survive through reloads.
    Code:
                                for(Entity e : getNearbyEntities(loc, 5)) {
                                    if(hasNearbyEntities(loc, 5)){
                                        if(e instanceof Monster|| e instanceof Player){
                                    if(loc.getBlock().getState() instanceof BrewingStand &&
                                         Location second_location = e.getLocation();
                                         Vector vector = second_location.toVector().subtract(first_location.toVector());
                                           Snowball bullet = loc.getWorld().spawn(first_location, Snowball.class);
                                           bullets.add(bullet.getUniqueId());
                                            bullet.setVelocity(vector);
                                         
                                  
                                }else{
                                    break;
                                }
                                    }
                                }
                                }
                        }
                    }, 0L, 10L);
     
    Last edited: Mar 15, 2015
  15. Offline

    SuchSwegMuchWow

    @Hex_27 How bout invisible skeletons inside a minecart?
     
  16. 1. They destroy the minecart when shooting
    2. They don't shoot at monsters
    3. They make noises
     
  17. Offline

    Hex_27

    @FisheyLP So my main problem now is reloads, and I REALLY don't want to save at the end of each disable "Which tower belongs to who and where" and so on. Update on my progress, now I'm using Brewing Stands to prevent people doing /killall and destroying turrets,

    Also, I can't save the owners, so the turret hits anything that is unlucky enough to be nearby.

    @SuchSwegMuchWow

    4. Skeletons will obstruct a player's shooting
    6. Invisible fire in the day
    7. Wolf/iron golem / snow golem weird behaviours
    8. Doesn't rotate minecart.
    9. I can go on
     
    Last edited: Mar 15, 2015
  18. Offline

    Hex_27

    @FisheyLP The link you gave lead me to a blank page. And FYI, they DON'T survive reboots (/stop then run again).

    I still need help with this.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  19. @Hex_27
    Sorry for spoonfeed, but

    Register Entities:
    Code:
        private static Method validateEntityMethod;
       
        public HologramUtils() throws Exception {
    //Just change on here the class with u custom entity`s class, the string with the EntityType and the int with the entity id
            registerCustomEntity(NMSEntity.class, "ArmorStand", 30);
            registerCustomEntity(NMSNpc.class, "Villager", 120);
            validateEntityMethod = World.class.getDeclaredMethod("a", Entity.class);
            validateEntityMethod.setAccessible(true);
        }
       
    //Here you register your entities, if you don`t register them, them wont work.
        @SuppressWarnings("rawtypes")
        public void registerCustomEntity(Class entityClass, String name, int id) throws Exception {
            putInPrivateStaticMap(EntityTypes.class, "d", entityClass, name);
            putInPrivateStaticMap(EntityTypes.class, "f", entityClass, Integer.valueOf(id));
        }
       
    //This is just to edit something that already exists on bukkit
        @SuppressWarnings({ "rawtypes", "unchecked" })
        public static void putInPrivateStaticMap(Class<?> clazz, String fieldName, Object key, Object value) throws Exception {
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            Map map = (Map) field.get(null);
            map.put(key, value);
        }
    Custom Entity:

    Code:
    //Be sure to extend the entity u`ll like to change
    public class NMSNpc extends EntityVillager {
        public NMSNpc(World world) {
            super(world);
    //Making the minecart to attack as an skeleton. NOT SURE IF THIS WILL WORK.
            try {
                Utils.setPrivateField(EntityVillager.class, this, "a", getPrivateField("a", EntitySkeleton.class, Object)); }
                        catch (Exception e) { //This should never happen }
           
    //Deleting old behaviour
            List goalB = (List)getPrivateField("b", PathfinderGoalSelector.class, goalSelector); goalB.clear();
            List goalC = (List)getPrivateField("c", PathfinderGoalSelector.class, goalSelector); goalC.clear();
            List targetB = (List)getPrivateField("b", PathfinderGoalSelector.class, targetSelector); targetB.clear();
            List targetC = (List)getPrivateField("c", PathfinderGoalSelector.class, targetSelector); targetC.clear();
           
    //New Behaviour, this is just an example, put on here what u like
            this.goalSelector.a(1, new PathfinderGoalLookAtPlayer(this, EntityPlayer.class, 20.0F));
            this.goalSelector.a(0, new PathfinderGoalFloat(this));
        }
       
        public static Object getPrivateField(String fieldName, Class clazz, Object object) {
            Field field;
            Object o = null;
    
            try {
                field = clazz.getDeclaredField(fieldName);
    
                field.setAccessible(true);
    
                o = field.get(object);
            } catch(NoSuchFieldException e) {
                e.printStackTrace();
            } catch(IllegalAccessException e) {
                e.printStackTrace();
            }
            return o;
        }
    }
     
  20. I tried your code but instead with world.spawnArrow and it works
     
    Juancomaster1998 likes this.
  21. Offline

    Hex_27

    @Juancomaster1998 The problem is, entityminecartrideable doesn't have all the aiming things input inside it. So... how can I do this? (As in "aiming things", I mean goalSelector, a, b, c and so on. And, I still don't get it.
     
  22. @Hex_27
    Just use a goal selector to target the nearest player, and on target event, check if the entity is the entity you wanna use and if it`s, fire an arrow
     
  23. Offline

    Hex_27

Thread Status:
Not open for further replies.

Share This Page