Skip to content

Updating variables on events

games647 edited this page Mar 29, 2015 · 3 revisions

For more information: BukkitWiki

Some variables only updates on specific action or events. Because of performance reasons we don't want to ask the replacer for new values. Therefore it should notify the ReplaceManager if that event happened. Like here:

package yourdomain.yourplugin;

import com.github.games647.scoreboardstats.ScoreboardStats;
import com.github.games647.scoreboardstats.variables.ReplaceEvent;
import com.github.games647.scoreboardstats.variables.ReplaceManager;
import com.github.games647.scoreboardstats.variables.VariableReplacer;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class YourPlugin extends JavaPlugin 
        implements VariableReplacer, Listener {

    private ReplaceManager replaceManager;

    @Override
    public void onEnable() {
        ScoreboardStats scoreboardStats = JavaPlugin
                .getPlugin(ScoreboardStats.class);
        replaceManager = scoreboardStats.getReplaceManager();
        
        replaceManager.register(this, this, "variable", "worldChanged");
   }

    public void onReplace(Player player, String var, ReplaceEvent replaceEvent) {
        //Set to this value for all variables for this replacer
        replaceEvent.setScore(321);
    }

    @EventHandler(ignoreCancelled = true)
    public void onPlayerJoin(PlayerJoinEvent joinEvent) {
        //updates global as there is no player specified
        replaceManager.updateScore("variable", joinEvent.getPlayer().getLevel());
    }

    @EventHandler(ignoreCancelled = true)
    public void onWorldChange(PlayerChangedWorldEvent worldChangeEvent) {
        Player player = worldChangeEvent.getPlayer();
        replaceManager.updateScore(player, "worldChanged", 1);
    }
}