Solved Make player invulnerable

Discussion in 'Plugin Development' started by Condolent, Jul 17, 2015.

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

    Condolent

    So I tried this while the specified player is listed as AFK, just so the player can't take damage at all:
    Code:
        @EventHandler
        public void onAfkHurt(EntityDamageByEntityEvent e) {
            Player p = (Player) e.getEntity();
            String UUID = p.getUniqueId().toString();
            Entity entity = e.getEntity();
           
            List<String> afk = getPlayerLog().getStringList("afk");
            getPlayerLog().set("afk", afk);
            plugin.savePlayerLog();
           
            if(entity instanceof Player) {
                if(afk.contains(UUID)) {
                    e.setCancelled(true);
                } else {
                    e.setCancelled(false);
                }
            } else {
                // Do nothing
            }
    Now when whatever mob gets hurt, it prints a huge error message in console. And I'm not even sure what's wrong here, any ideas??
     
  2. Offline

    teej107

    @Condolent I probably know what your error message is but I want you to know what it is. Do you know what the error message (stacktrace) is?
     
  3. Offline

    Condolent

    something about memory allocation if I'm not mistaken.
    Anyhow, if you want to help then please help me out. But if you don't want to then don't even bother. Seen most of your post being "you need to learn java before doing this" and that stuff and it really annoys me. Most people (like me) likes to learn by doing, that's what I've done in most cases.
    And I noticed how this kind of sounds rude, but it's not meant to be rude in ANY way so please don't misunderstand this lol
     
  4. Offline

    teej107

  5. Offline

    567legodude

    @Condolent I can actually help, you need to check if the entity is actually a player before casting it.
    if (e.getEntity() instanceof Player)
    before you can do (Player) e.getEntity()
     
  6. Offline

    Condolent

    @567legodude Still gives me the error :/ Entity entity = (Player) e.getEntity(); is what I used, hope I didnt misunderstand you haha


    @teej107
    Stacktrace (open)
    : "[23:36:05 ERROR] Could not pass event EntityDamageByEntityEvent to 123 vPrototy pe 0.0.1 org.bukkit.event.EventException at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.ja va:310) ~[lel.jar:git-Spigot-6d16e64-bf4818b] at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav a:62) ~[lel.jar:git-Spigot-6d16e64-bf4818b] at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j ava:502) [lel.jar:git-Spigot-6d16e64-bf4818b] at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j ava:487) [lel.jar:git-Spigot-6d16e64-bf4818b] at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callEvent(Craf tEventFactory.java:86) [lel.jar:git-Spigot-6d16e64-bf4818b] at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callEntityDama geEvent(CraftEventFactory.java:552) [lel.jar:git-Spigot-6d16e64-bf4818b] at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.handleEntityDa mageEvent(CraftEventFactory.java:465) [lel.jar:git-Spigot-6d16e64-bf4818b] at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.handleLivingEn tityDamageEvent(CraftEventFactory.java:584) [lel.jar:git-Spigot-6d16e64-bf4818b] at net.minecraft.server.v1_8_R3.EntityLiving.d(EntityLiving.java:1101) [ lel.jar:git-Spigot-6d16e64-bf4818b] at net.minecraft.server.v1_8_R3.EntityLiving.damageEntity(EntityLiving.j ava:742) [lel.jar:git-Spigot-6d16e64-bf4818b] at net.minecraft.server.v1_8_R3.EntityHuman.attack(EntityHuman.java:1001 ) [lel.jar:git-Spigot-6d16e64-bf4818b] at net.minecraft.server.v1_8_R3.EntityPlayer.attack(EntityPlayer.java:10 63) [lel.jar:git-Spigot-6d16e64-bf4818b] at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java :1328) [lel.jar:git-Spigot-6d16e64-bf4818b] at net.minecraft.server.v1_8_R3.PacketPlayInUseEntity.a(SourceFile:52) [ lel.jar:git-Spigot-6d16e64-bf4818b] at net.minecraft.server.v1_8_R3.PacketPlayInUseEntity.a(SourceFile:11) [ lel.jar:git-Spigot-6d16e64-bf4818b] at net.minecraft.server.v1_8_R3.PlayerConnectionUtils.run(SourceFile:1 3) [lel.jar:git-Spigot-6d16e64-bf4818b] at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [ ?:1.8.0_45] at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45] at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [lel. jar:git-Spigot-6d16e64-bf4818b] at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:7 14) [lel.jar:git-Spigot-6d16e64-bf4818b] at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3 74) [lel.jar:git-Spigot-6d16e64-bf4818b] at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6 53) [lel.jar:git-Spigot-6d16e64-bf4818b] at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java :556) [lel.jar:git-Spigot-6d16e64-bf4818b] at java.lang.Thread.run(Unknown Source) [?:1.8.0_45] Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.entity.C raftPig cannot be cast to org.bukkit.entity.Player at me.condolent.Afk.onAfkHurt(Afk.java:54) ~[?:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0 _45] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0 _45] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1 .8.0_45] at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45] at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.ja va:306) ~[lel.jar:git-Spigot-6d16e64-bf4818b] ... 23 more >"]
     
  7. Offline

    567legodude

    @Condolent You can't cast the entity to a player unless you actually know it's a player.
    Code:
    // at the beginning of the event
    Entity entity = e.getEntity();
    if (!(entity instanceof Player)) return;
    Player p = (Player) entity;
     
  8. Offline

    Condolent

    @567legodude still error D:
    Code:
    @EventHandler
        public void onAfkHurt(EntityDamageByEntityEvent e) {
            Entity entity = e.getEntity();
            if(!(entity instanceof Player)) return;
            Player p = (Player) entity;
            String UUID = p.getUniqueId().toString();
          
            List<String> afk = getPlayerLog().getStringList("afk");
            getPlayerLog().set("afk", afk);
            plugin.savePlayerLog();
          
            if(entity instanceof Player) {
                if(afk.contains(UUID)) {
                    e.setCancelled(true);
                } else {
                    e.setCancelled(false);
                }
            } else {
                // Do nothing
            }
    EDIT: Had to restart the whole server for the new version to take effect, now it works! Thanks boys

    just a quick off-topic question, how would I remove the string-line if I had getMutes().set(UUID, reason); and UUID being a string for users id and reason a message-string.
    Tried doing something like getMutes().remove(UUID, reason); but the remove thing doesn't work unless it's a list and I'm not sure if I can make a list out of this.

    I think it can be solved for a list if I can make the UUID as a int but it doesn't let me do that

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

Share This Page