Please help with a new plugin I'm making.

Discussion in 'Plugin Development' started by (infected), Jul 24, 2011.

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

    (infected)

    Im making a self titled plugin called Infected and was wondering what events to call to track if a player is Infected. Basically I want it to be if a player hits another player with any block bow or fist that player is infected. Nothing more than an overrated game of tag for the first version. Does anyone have ideas on where to start?
    Sorry for bothering you guys but this is really important to me.
     
  2. Offline

    Erco21

    You will have to log which players are infected, and you will need a databsae for that i think
     
  3. Offline

    bergerkiller

    Ugh database...HashSet is fine too. :)
    Code:
    public static HashSet<Player> taggedPlayers = new HashSet<Player>();
    All you do is add a player to this hashset if he becomes infected:
    Code:
    taggedPlayers.add(Player)
    To check if someone is tagged, use:
    Code:
    if (taggedPlayers.contains(Player)) {
        //stuff here
    }
    Regular remove and clear function are also in there.

    If you wish to keep a Player tagged (infected) for longer than one Server run, store the tagged player names in a text file and load it back later on. Seriously, Database? A Database is for when you have multiple values associated with multiple keys, for example, you need to know all Players that are sneaking as quick as possible. For this simple thing a Database is overkill.
     
  4. Offline

    Erco21


    Yes, but he maybe wants to store the time of infection, how long it will last, and more similar info
     
  5. Offline

    bergerkiller

    That can all be done without databases too. See this:
    Code:
    public static class InfectionInfo {
        public long timestart;
        public long timeend;
        public Player infectedby;
    }
    public HashMap<Player, InfectionInfo> infections = new HashMap<Player, InfectionInfo>();
    Need to update if a Player is infected? Run a scheduled task which will automatically un-infect a Player.
    Example of this; I update the inventories 1/10 second after a /give or /item command was sent:
    Code:
        @Override
        public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
            String msg = event.getMessage().toLowerCase();
            if (msg.startsWith("/item ")) {
                delayedUpdate(event.getPlayer());
            } else if (msg.startsWith("/give ")) {
                //parse the command
                msg = msg.substring(6);
                if (msg.contains(" ")) {
                    msg = msg.substring(0, msg.indexOf(" "));
                    List<Player> m = event.getPlayer().getServer().matchPlayer(msg);
                    if (m.size() != 0) {
                        delayedUpdate(m.get(0));
                    }
                }
            }
        }
    
        private static HashSet<Player> invupdates = new HashSet<Player>();
        /*
         * Delayed inventory update call
         */
        public void delayedUpdate(Player p) {
            invupdates.add(p);
            this.plugin.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin, new Runnable() {
                public void run() {
                    for (Player p : invupdates) {
                        HoMGeneral.updateInventory(p);
                    }
                    invupdates.clear();
                }
            }, 2L);
        }
    YAML provides more than enough features to store all this information. Plus, data only has to be loaded once, in onEnable, and saved once, in onDisable. Databases are for queries with 1000+ items, not for a HashMap of 100- items.
     
  6. Offline

    (infected)

    bergekiller you are my hero I can do DB but why? hash would work fine for this THANK YOU SO MUCH!!!!~
     
Thread Status:
Not open for further replies.

Share This Page