Entity NPC not working?

Discussion in 'Plugin Development' started by NepsName, Apr 24, 2015.

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

    NepsName

    Hi everyone.

    So I searched for some NPC libs because I wanted to make some simples NPCs. But either were the libraries seriously outdated/not working or they were just too much for what I wanted to do, so I tried to simply make my own NPCs, couldn't be that hard could it?

    Well, I just wanted to make an entity of an iron golem that doesn't move, doesn't despawn and that can't get hit. Just that.

    This is what I got so far:

    Code:
    static Location wardenLoc = new Location(Bukkit.getWorld("World"), -53.5, 33.0, 199.5);
       static LivingEntity warden;
            static HashMap<String, LivingEntity> NPCs = new HashMap<String, LivingEntity>();
    
        public void onEnable() {
            Bukkit.getPluginManager().registerEvents(new Events(), this);
            setupWarden();
    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    if (NPCs.get("warden").getLocation().getX() != wardenLoc.getX() || NPCs.get("warden").getLocation().getZ() != wardenLoc.getZ()) {
                        NPCs.get("warden").teleport(wardenLoc);
                    }
            }, 5, 40);
        }
    
        public static void setupWarden() {
            warden = (LivingEntity) Bukkit.getWorld("RiotMC").spawnEntity(wardenLoc, EntityType.IRON_GOLEM);
            warden.setCustomName("§6Warden");
            warden.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 999999999, 999999999, false, false));
            warden.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 999999999, 999999999, false, false));
            NPCs.put("warden", warden);
        }
    I put this code up in the server, and I restart it. When I log in it is not working. The iron golem can be pushed and killed and all that. If I do /reload without leaving the game it spawns another iron golem and that one works just like it is supposed to, it cannot be moved or hitted. As soon as my character leaves the server and re-joins, it breaks and the iron golem can now be killed and moved... I really do not understand how me leaving the server and joining back (The server is still on and is not restarted) can affect these entities...

    What did I do wrong? How can I keep them working?

    Also, how can I delete the entities and spawn new ones everytime it restarts? It spawns new ones just fine but it is not deleting them, it can't.

    /killall doesnt work. /butcher doesn't work. /kill @e doesn't work.
    I tried onworldunload and onDisable doing
    Bukkit.getWorld("Prison").getEntities().clear();
    Bukkit.getWorld("Prison").getLivingEntities().clear();
    And neither of them works. It seems like they stopped being entities for some reason...

    What can I do?
     
  2. Offline

    timtower Administrator Administrator Moderator

    Moved to plugin development.
    @NepsName You need to kill them, not remove them from the list.
     
  3. Offline

    NepsName

    Ok, I'll try that, but it doesn't fix my main issue which is the code stop working when I leave and rejoin the server... /:

    Bump

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

    Reynergodoy

    the iron golems can be killed and moved because:
    when you exit and join, these effects that are applied on players "fades away", so the effects don't affect this player, and he can now do everything that he wants ( ͡° ͜ʖ ͡°) to the iron golem

    you can try to see the difference between the player join and player exit, respawn, and death events :)

    sorry if this dont help... but i've tried to :)
     
  5. Offline

    NepsName

    @timtower That doesn't really help with the big problem, that was just the small problem. What can I do for the iron golem to keep working even after a player leaves or rejoins?

    @Reynergodoy I am not sure if I understand... I didn't apply anything to any players. All the changes I made was to the entity itself, right?
     
  6. Offline

    pie_flavor

    @NepsName Don't set the effect to 9999999, an effect over 255 is ignored and an effect over 127 is 'inverted'. Probably set it to 126, as well as jump boost 255.
    This is your problem; it's ignoring the effects.
    You ever played a map where they disable jumping? That's how, jump boost 255 because an effect over 127 is inverted. It's also how they made saturation before 1.7.
    The reason the relog happens is because player clients don't know how to handle an entity update like that, but when they relog the server just tells them that there's nothing to see here.
     
  7. Offline

    Reynergodoy

    hm.. you can try to do the
    Code:
    p.setNoDamageTicks
    but i'm not sure how to apply this to an entity thats not the player, maybe it requires it to search for an entity type called iron_golem
     
  8. Offline

    NepsName

    I tried that, it works on living entities, but it just does the same thing, works until player leaves and relogs, then that effect vanishes.
    I'm still gonna try
    @pie_flavor solution, gonna respond here with the results
     
  9. Offline

    NepsName

    Tried that, and it had no difference at all. I tried 255, 126, even 50. As soon as a player relogs they can kill the entity and move it just fine.

    Also, I use the scheduler to not allow entities to be pushed because the jump affect allows for mobs to be pushed which I can't allow. Problem is as soon as a player relogs the scheduler "loses" the entity, even when I use its UUID, its living entity, entity, no matter what, if a player relogs it all breaks...

    What now?
     
  10. Offline

    Reynergodoy

    the player HAVE to touch the entity?
    if not, use the invisible block that prevent user interacting with the entity
     
  11. Offline

    NepsName

    It is a warden for a prison server, people will be interacting with it all the time. So yeah, they will have to be able to right click it.
     
  12. Offline

    Reynergodoy

    hmmm... maybe you can add the player to an array list when he enters the server, and set that he cant hit or move the mob type iron_golem

    or you just use world guard flags to prevent they taking damage, and construct a "pedestal" to them to not move :)
     
  13. Offline

    NepsName

    That seems like it could work, the first solution.

    I know how to make a player in an array list not be able to attack iron golems... but how can I make him not be able to move iron golems? Is there any attribute I can give to a player so that they can't push mobs?
     
  14. Offline

    Reynergodoy

    hmm... idk, but you can make one pod to the warden, the players will still be able to interact with him, but not push
     
  15. Offline

    NepsName

    what do you mean by pod? What is that?
     
  16. Offline

    Reynergodoy

    like a compartment, where your warden is "stocked"
    he wont be free to "take a breath outside", but thats the easiest way
    or you can just put this warden like in a block floating, like slabs, or cobblestone
     
  17. Offline

    NepsName

    But I'll also have quest NPCs to which I would like to apply the same thing, but they would be at ground level, and I don't want players to push them... Is this really that hard? :|
     
  18. Offline

    Reynergodoy

    not really, its because i'm a newbie in java/bukkit
     
  19. Offline

    NepsName

    Ok, well, thanks anyway for you help ;)

    I will keep the question here to see if anyone can help me...

    Question: How to "apply" something to a player to make him not be able to push living entities like a villager
     
  20. Offline

    Konato_K

    @NepsName I'd go with make them not collide with entities
     
  21. Offline

    NepsName

    But how...

    I found this code example that supposely prevents colisions:

    Code:
    @Override
        public void g(double d0, double d1, double d2) {
            return;
        }
    But I have no idea where do I put this...
    Just placing it in my code gives "The method g(double, double, double) of type NPCs must override or implement a supertype method"

    Anyone know how to use this or any other ways to prevent colisions with an iron golem entity?

    @SuppaTim

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

    Konato_K

    @NepsName Considering that, I'd say that is something you need to override in an entity to make it not move in collisions.

    Try this
    Code:
    ((CraftPlayer)paramPlayer).getHandle().collidesWithEntities = false;
    Be aware that this is version dependent code.
     
  23. Offline

    NepsName

    That attribute resets on relog? By that I mean, do I have to apply it to every player on log in?
    And can't I apply it to just iron golems? Might seem weird if they can't collide with any entity at all, but thats secondary really.
     
  24. Offline

    Konato_K

    @NepsName Yes, it resets, the attribute is part of the EntityPlayer class, so no, it can't be applied to mobs, you can override mobs to change their behavior.

    This field makes players not collide with entities at all, if I recall correctly this means pushing mobs aswell.
     
  25. Offline

    NepsName

    Just tested, does work on disable pushing, but it creates other issues. It "deletes" bow fights. Since they can't collide with entities, being arrows entities, players cannot be hit by arrows.
    But the worst one that I tested so far is that players with this attribute cannot pick up items, and that is definately not something I can live with... So I'll need to find a way to make this only work torwards iron golems... How could I ever do that?

    I'll also, when I have some free time, try this myself, have some crazy ideas that might work, I'll post here if I find an answer/solution, but if anyone knows a way please tell me :)
     
  26. Offline

    Konato_K

    @NepsName I thought you were using this for a lobby-kind of thing, so I assumed arrows and picking items where not necessary there.

    Anyway, if that's the case, the only thing you can do is override the mobs to not collide with other entities instead.
     
  27. Offline

    NepsName

    How can I do that? How can I apply ".getHandle().collidesWithEntities = false;" to a LivingEntity?

    CraftIronGolem.getHandle() doesn't have "collidesWithEntities" boolean...
     
  28. Offline

    Msrules123

    Why not just set the location (from a stored area) of the entity when your entity is moved?
     
  29. Offline

    Konato_K

    @NepsName You can't, to override mobs check this tutorial, the method your have to override is probably the one you mentioned earlier, take in consideration that this is version dependent and the method to override may not be g(double, double, double) in the version you're using.
     
  30. Offline

    NepsName

    Not trying to be rude, but you should read the question before answering :/

    The problem is that that solution doesn't work, when a player relogs the code loses track of the entity, the scheduler that teleports it into a location whenever it changes starts sending errors to the console because it simply lost the entity.
    When a player relogs the iron golem simply somehow "changes" to something else, and the code "loses" it...

    Isn't there really ANY other way other then NMS? :|

    Also, how would that fix the issue of the code losing track of the entity when a player logs in?
    That is the main issue, we drifted to collision, but if when a player logs in and the code loses track of the entity, that also breaks the part where I need the interact event to detect if it is this specific entity being interacted with...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
Thread Status:
Not open for further replies.

Share This Page