Almost godmode

Discussion in 'Plugin Development' started by wannezz, Mar 9, 2012.

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

    wannezz

    Hey guys, I need help...
    I made some very simple plugins before (WelcomeRulezz, EnderpearlDisable,...)
    I want to make a plugin but I don't know how to start...
    With the plugin you can't die or lose any hearts but it looks like you are taking damage if you get hit. It's like god mode. But when you get hit it looks like you take damage instead of not moving or not taking anything with regular god mode.
    The plugin should have a file of names to put players in that have the ability.

    If someone can get me started, it would be much appreciated.

    Something with EntityDamageEvent?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  2. So you want to have all the effects the damage without the damage ?
    It's simple, in the damage event just set the victim's health to 20 instead of cancelling the event.... hmm, but I think if you just add to the HP it stacks up over 20 and it could help against verry big criticals like falling.... I never did this tough.
     
  3. Offline

    wannezz

    Can it stack over 20 ?
    Didn't know that...
    So, how do I set the HP to 20?
     
  4. 20 = Full (1= 1 half heart)

    It can stack over 20, but you can't see it :p
     
  5. Offline

    wannezz

    no, I mean where do I have to put the number 20 (if I want 20)

    So I have the event:
    Code:
    onPlayerDamage(EntityDamageEvent event) {
     
    }
    
    what should I do in between those brackets to tell bukkit I want the player to have 20 HP...?

    Thanks...
     
  6. Player p = event.getPlayer

    p.setHealth(20);
     
    wannezz likes this.
  7. Offline

    wannezz

    Thank you manniL, I gave you a Like :)

    I still have a problem, in the EntityDamageEvent, I can't define Player.
    The error I get is: The method getPlayer() is undefined for the type EntityDamageEvent
    Which Event should I have to use?

    anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  8. Offline

    stelar7

    Player p = (Player) event.getEntity();
     
  9. It's getEntity(), you must check if it's instanceof Player and then cast it to Player.

    Code:
    Entity ent = event.getEntity();
    
    if(ent instanceof Player)
    {
        Player player = (Player)ent;
    
        player.setHealth(20);
    }
     
    wannezz likes this.
  10. Offline

    wannezz

    Okay, thank you :)
     
  11. Also, if you're using Eclipse press ctrl+space after a dot to print a list of available methods for that class... like: player. and ctrl+space.
    I dunno about other IDEs.
     
  12. Offline

    wannezz

    It's not working... :(
    here's my code:
    - the main class
    Code:
    package me.wannezz.SemiGod;
     
    import java.util.logging.Logger;
     
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class SemiGod extends JavaPlugin
    {
    Logger log = Logger.getLogger("Minecraft");
       
        @Override
        public void onDisable()
        {
            log.info("[SemiGod] has been disabled");
        }
     
        @Override
        public void onEnable()
        {
            log.info("[SemiGod] has been enabled");
            PluginManager manager = this.getServer().getPluginManager();
            manager.registerEvents(new DamageListener(), this);
        }
    }
    The Listener:
    Code:
    package me.wannezz.SemiGod;
     
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
     
    public class DamageListener implements Listener
    {
        @EventHandler
        public void onPlayerDamage(EntityDamageEvent event)
        {
            Entity entity = event.getEntity();
           
            if(entity instanceof Player)
            {
                Player player = (Player)entity;
               
                player.setHealth(20);
            }
                   
        }
    }
    
    This is the error I get in the console...

    Code:
    19:11:46 [SEVERE] Could not pass event EntityDamageEvent to SemiGod
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:303)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:441)
            at net.minecraft.server.EntityLiving.a(EntityLiving.java:819)
            at net.minecraft.server.EntityHuman.a(EntityHuman.java:1191)
            at net.minecraft.server.Entity.a(Entity.java:686)
            at net.minecraft.server.EntityPlayer.b(EntityPlayer.java:467)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:413)
            at net.minecraft.server.Packet10Flying.handle(SourceFile:126)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:112)
            at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:7
    8)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:549)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:447)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:490)
    Caused by: java.lang.IllegalArgumentException: Health must be between 0 and 20
            at org.bukkit.craftbukkit.entity.CraftLivingEntity.setHealth(CraftLiving
    Entity.java:55)
            at me.wannezz.SemiGod.DamageListener.onPlayerDamage(DamageListener.java:
    20)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:301)
            ... 14 more
    
    I don't know what to do... :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  13. Hmm...
    Are you sure you set it to 20 ? If you did, it could be a bukkit issue :/ are you using recommended builds ?

    EDIT: or maybe it's between exclusive, 1-19 :confused: try 19.
     
  14. Offline

    Joba

    In my plugin i use health too, and i can sat the health at 20 and at 0. I don' t know why there is an error but you could try, it works in my plugin:
    Code:
    @EventHandler
    public void onPlayerDamage(EntityDamageEvent event) {
        Player player = event.getPlayer();
        player.setHealth(20);
    }
     
  15. Offline

    wannezz

    The error is solved, but when I tested it, there is still damage when you fall, if you are drowning, you always have 9 hearts, and if you get hurt by hunger, you always have 9 and a half hearts...
    Does anyone know how to solve this?
     
  16. You can use event.setCancelled(true) ;) Then the player dont get dmg :)
     
  17. Offline

    wannezz


    But then the effects will be gone, isn't it?
    I want it have like others can see you have damage, but you actually don't get hurt...

    Anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  18. Offline

    Njol

    Use a scheduled task to set the player's health to 20 one tick after the event.
     
  19. Offline

    Milkywayz

    Lol if he's problems with this then a scheduled task would be crazy :p Ps i can't do scheduled tasks so its not really an insult hehe
     
  20. Offline

    Joba

    But when you use scheduled tasks, you can die when you fall more than 30 blocks deep. Maby there is a way to give the player an enchanted armour, that is invisible
     
  21. Offline

    Blackside921

    Perhaps he could just set the damage always to just 1. And then heal the player.
     
    wannezz likes this.
  22. Offline

    wannezz

    Thank you Blackside, this helped me a lot :)
    I gave you a like :p
     
  23. Offline

    Joba

    The easiest ideas are always the best...
     
Thread Status:
Not open for further replies.

Share This Page