Solved God Mode - Stop Regeneration Of Health

Discussion in 'Plugin Development' started by NoLiver92, Dec 1, 2012.

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

    NoLiver92

    What i want to do is when a player uses /afk command they are put into god mode (i will store their status in a file) then when a player moves, or interacts with anything (including chat) afk status will be revoked and god mode disabled.

    I know how to do the commands but what do i have to set to make the player invincible and how would i have to disable it (what events)
     
  2. Offline

    Muxon

    EntityDamageByEntityEvent

    Entity evilOne = event.getDamager();
    Entity hurtedOne = event.getEntity();
    if(evilOne instanceof Player && hurtedOne instanceof Player){
    event.setDamage(0);
    }

    This ist just the beginning ;) Just look up all the DamageEvent here: klick
     
  3. Offline

    Tirelessly

    Don't use that, use EntityDamageEvent. When the player uses /afk add them to a hashset. On those events remove on. On EntityDamage if the hashset has their name set it cancelled.
     
  4. Offline

    Cammy_the_block

    Essentials has something just like this if you wanna make this for your personal use.
     
  5. Offline

    keelar

    Essentials sounds like a pretty hefty solution for such a small problem don't ya think?
     
  6. Offline

    FTWin01Gurl

    List is way better. It stores in ones, like an array.
     
  7. Offline

    Tirelessly

    I don't think you really know what you're saying, to be honest. A set here would be more appropriate as it doesn't matter the order.
     
  8. Offline

    Cammy_the_block

    Fine you can use one cmd and disable the rest. Essentials lets you disable essentials commands
     
  9. Offline

    FTWin01Gurl

    A list is better than a map because a list uses less memory, if I'm correct.
     
  10. Offline

    keelar

    Or he could write a few lines of code and do it himself. A few lines of code sounds like a better idea than Essentials.
     
  11. Offline

    NoLiver92

    I dont want essentials, if i wanted essentials i wouldnt be making my own plugin, plus i cant intergrate essentials with what i want.

    I have the file storage sorted, that isnt a problem im using a yml file, all i need is the way to make them invinsible and when the do anything like move mouse, chat, or move then their invincivility is over
     
  12. Offline

    fireblast709

    NoLiver92
    • create an ArrayList<String> (you use playernames in there, not Player objects)
    • when they do /afk you add them
    • when they move you remove them
    • if they are in the list, you ignore all damage
    Tirelessly No big difference between Set and ArrayList (except ArrayList allows you duplicates, but that is not a real big deal here is it?)
    FTWin01Gurl true but he is talking about Sets (ArrayList and Set are both subclasses of List)
    Cammy_the_block in this case writing it yourself is better, as you have less dependencies
     
  13. Offline

    NoLiver92

    fireblast709
    what i was after effecitvely is how do i set it to stop all damage?
     
  14. Offline

    fireblast709

    EntityDamageEvent. Check if the damaged entity you get from getEntity() is a Player, and if his/her name in the list. Then cancel the event
     
  15. Offline

    NoLiver92

    and i would have to clear the playername in the array in every event that i want them to come out of it? e.g. playermoveevent, playerchatevent, and so on...?
     
  16. Offline

    fireblast709

    depends on your wishes basically. But yea, if you want to make them go un-afk on chat and movement, yes
     
  17. Offline

    NoLiver92

    ok thanks for your help

    i have tried this and still have not been able to get it to work, when i test it mobs still hurt me. any help with this?

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

    fireblast709

    Use debug to check if you are actually godded
     
  19. Offline

    NoLiver92

    the code runs but nothing happens, i did that first to make sure before posting here
     
  20. Offline

    callum.thepro

    This may be a stupid comment but just to be sure as your using eventss your using @EventHandler and pm.registerevents?
     
  21. Offline

    fireblast709

    Post your code. You probably forgot to register the listener or forgot the @EventHandler

    [offtopic] callum.thepro damn you ninja :p
     
  22. Offline

    NoLiver92

    Main Class:
    Code:
    this.getCommand("afk").setExecutor(new AFKExecutor(this));
    getServer().getPluginManager().registerEvents(new EntityDamageListener(this), this);
    Listener:
    Code:
    package com.multiplugin.listeners;
     
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import com.multiplugin.MultiPlugin;
     
    public class EntityDamageListener implements Listener
    {
    private final MultiPlugin plugin;
       
        public EntityDamageListener(MultiPlugin plugin)
        {
            this.plugin = plugin;
        }
       
        @EventHandler(priority = EventPriority.NORMAL)
        public void onEntityDamage(EntityDamageByEntityEvent event)
        {
            String playername = event.getEntityType().getName();
            if(plugin.PlayerDataConfig.getBoolean(playername + ".AFK") == true)
            {
                event.setDamage(0);
            }
        }
    }
    
    Command Executor:
    Code:
    package com.multiplugin.commands;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    import com.multiplugin.MultiPlugin;
     
    public class AFKExecutor implements CommandExecutor
    {
    private final MultiPlugin plugin;
       
        public AFKExecutor(MultiPlugin plugin)
        {
            this.plugin = plugin;
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
        {
            Player player = (Player) sender;
            String playername = player.getName();
            if(args.length == 0)
            {
                plugin.PlayerDataConfig.set(playername + ".AFK", true);
                Bukkit.broadcastMessage(ChatColor.DARK_AQUA + playername + " Is AFK");
            }
            if(args.length > 0)
            {
                player.sendMessage(ChatColor.DARK_RED + "[MP] " + ChatColor.WHITE + "Usage: /afk");
            }
           
            return true;
        }
    }
    
    Everything is registered and in the plugin.yml

    PlayerDataConfig is my yml file to save sata
     
  23. Offline

    callum.thepro

    You have not defined a command You should change

    Code:
     @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
        {
            Player player = (Player) sender;
            String playername = player.getName();
            if(args.length == 0)
            {
                plugin.PlayerDataConfig.set(playername + ".AFK", true);
                Bukkit.broadcastMessage(ChatColor.DARK_AQUA + playername + " Is AFK");
            }
            if(args.length > 0)
            {
                player.sendMessage(ChatColor.DARK_RED + "[MP] " + ChatColor.WHITE + "Usage: /afk");
            }
         
            return true;
        }
    }
    To
    Code:
     @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
        {
            Player player = (Player) sender;
            String playername = player.getName();
    if(label.equilsIgnoreCase("AFK") {
            if(args.length == 0)
            {
                plugin.PlayerDataConfig.set(playername + ".AFK", true);
                Bukkit.broadcastMessage(ChatColor.DARK_AQUA + playername + " Is AFK");
            }
            if(args.length > 0)
            {
                player.sendMessage(ChatColor.DARK_RED + "[MP] " + ChatColor.WHITE + "Usage: /afk");
            }
         
    }
            return true;
        }
    }
     
  24. Offline

    NoLiver92

    there is no need to, look at the main file.

    Code:
    this.getCommand("afk").setExecutor(new AFKExecutor(this));
    This only goes to the AFKExecutor class when the command is afk, as no other command is passed to that class you do not need to include that in the class.

    ok i have it working thanks, but how do i stop the regeneration of health

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

    teunie75

    Make a listener for the EntityRegainHealthEvent.
    If they regain health you do their health - 1.
    You can make an Arraylist and if they are in that list they can't get their health back.
    I took something from my own code
    This is my main:
    Code:
        public ArrayList<String> creeper;
     
        public void onEnable(){
            this.creeper = new ArrayList<String>();
    PluginManager manager = this.getServer().getPluginManager();
    manager.registerEvents(new <classname>(this), this);
    And this for the listener:
    Code:
    public class <classname> implements Listener {
    private <mainclassname> plugin;
     
     
        public <classname>(<mainclassname> plugin){
            this.plugin = plugin;
        }
     
        @EventHandler(priority = EventPriority.NORMAL)
        public void onPlayerRespawnEvent([URL='http://jd.bukkit.org/doxygen/d9/d59/classorg_1_1bukkit_1_1event_1_1entity_1_1EntityRegainHealthEvent.html#aecd7f138b5b70eb18a32f13000e46f4e'][SIZE=14px][FONT=Roboto][U][B][COLOR=#1b89cd]EntityRegainHealthEvent[/COLOR][/B][/U][/FONT][/SIZE][/URL] event){
    Player player = event.getPlayer();
    if(plugin.creeper.contains(player.getName()){
    player.setHealth(player.getHealth - 1);
    }}}
    At the commandexecutor you type somewhere:
    Code:
    plugin.creeper.add(player.getName());
    
     
  26. Offline

    NoLiver92

    thanks for the reply but what ive done is in the entityregainhealthevent, if they are afk (ckecks in list) and if so cancelled the event. this has stopped the health regain and is in alot smaller code.
     
  27. Offline

    teunie75

    That also a possibility, that I didn't think of that :p
     
Thread Status:
Not open for further replies.

Share This Page