Notifying Faction members when a player is in combatI

Discussion in 'Plugin Development' started by WizKhalifa, Mar 23, 2013.

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

    WizKhalifa

    Is there a way to not have this happen

    Is it possible so it only notifies the faction members once, instead of 50? Maybe at 18 health?
    Also, to not notify the player that is being attacked, but only his online faction members?

    Here is my code so far..
    Code:
        public void onPlayerDamage(EntityDamageByEntityEvent event){
            if(event.getEntity() instanceof Player && event.getDamager() instanceof Player) {
                String notification = new String(ChatColor.GREEN + "Faction" + ChatColor.GRAY + ": " + ChatColor.RED);
                Player hurt = (Player) event.getEntity();
                for(Player fPlayer: FPlayers.i.get(hurt).getFaction().getOnlinePlayers()){
                    fPlayer.sendMessage(notification + fPlayer + ChatColor.RED +" is under attack!");
                }
            }
    Thanks in advance!
     
  2. Offline

    GodzOfMadness

    WizKhalifa you have to have fPlayer.getName() to return the player's name
     
  3. Offline

    WizKhalifa

    right, just realized that. Thanks for the proof-read!
     
  4. Offline

    GodzOfMadness

    WizKhalifa I suggest that you don't use health to determine when to stop sending messages because if the player has less than the requirement to message the people in the other faction it won't message them at all
     
  5. Offline

    WizKhalifa

    good point, right now i have it when another player deals damage to them, but seems to spam the chat. Any ideas on how to slow down the rate of messages, as well as not send the message to the player being attacked at all?
     
  6. Offline

    GodzOfMadness

    WizKhalifa Probably use a hashmap
    store the time and then compare with the current time and if the current time - the time was stored is less than what you want then don't send the message
     
  7. Offline

    WizKhalifa

    Okay, I am sort of confused:confused:. I start the hashmap when onPlayerDamage is called, and end it when? thanks again for the help
     
  8. Offline

    GodzOfMadness

    WizKhalifa The best way i concider to handle this is by making a sub class and in that sub class just make some variables like so:

    Code:
        class DataHolder{
        DataHolder(){
        }
            public long lastTime = 0L;
        }
    Then create a hashmap like so:
    Code:
    private HashMap<String, DataHolder> hash = new HashMap();
    Then in the damage event:
    Code:
    if(!hash.containsKey(name)){
                                this.hash.put(name, new DataHolder());
                            }
                            DataHolder data = (DataHolder)this.hash.get(name);
                            Long current = Long.valueOf(new Date().getTime());
                            current = Long.valueOf(current.longValue() / 1000L);
    Now that is getting the current time and the player that is in "data"

    Next you want to determine if it's time to send the next message:

    Code:
    if (current.longValue() - data.lastTime < this.time) {
                                //don't let the code after run
                                return;
                            }
    The "time" variable would be how many seconds you would want it to wait
    private long time = 2;

    WizKhalifa I also forgot to mention that if it all is clear and it is messaging the people in the faction
    set the variable "lastTime" as so:
    data.lastTime = current.longValue();

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

    WizKhalifa

    okay, not sure if i followed you exactly. This is what I did:

    Code:
    public void onPlayerDamage(EntityDamageByEntityEvent event){
        if(event.getEntity() instanceof Player && event.getDamager() instanceof Player) {
           
            String notification = new String(ChatColor.GREEN + "Faction" + ChatColor.GRAY + ": " + ChatColor.RED);
            Player hurt = (Player) event.getEntity();
           
            for(Player fPlayer: FPlayers.i.get(hurt).getFaction().getOnlinePlayers()){
     
               
                if(!hash.containsKey(name)){
                    this.hash.put(name, new DataHolder(null));
                    DataHolder data = (DataHolder)this.hashCode().get(name);
                    Long current = Long.valueOf(new Date().getTime());
                    current = Long.valueOf(current.longValue() / 1000L);
                    long time = 2;
                    if(current.longValue() - data.lastTime < this.time){
                   
                fPlayer.sendMessage(notification + fPlayer.getName() + ChatColor.RED +" is under attack!");
                    }
                }
            }
        }
    This is my sub class:
    Code:
    public class FListener implements Listener{
        class DataHoldder{
            DataHolder(){
                long lastTime = 0L;
            }
            private HashMap<String, DataHolder> hash = new HashMap();
        }
     
    }
    
     
  10. Offline

    ZeusAllMighty11

    You don't have an '@EventHandler' annotation above your method
     
  11. Offline

    WizKhalifa

    I just decided not to paste it here. Thanks though!
     
Thread Status:
Not open for further replies.

Share This Page