Skip to content

Instantly share code, notes, and snippets.

@heinrichquirit
Created February 12, 2014 13:39
Show Gist options
  • Save heinrichquirit/8955723 to your computer and use it in GitHub Desktop.
Save heinrichquirit/8955723 to your computer and use it in GitHub Desktop.
package main.java.net.bigbadcraft.census;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class Census extends JavaPlugin implements Listener {
private final ChatColor T = ChatColor.DARK_AQUA;
private final String CENSUS_VIEW = "census.view";
private final String CENSUS_ADMIN = "census.clearcount";
private File jFile;
private FileConfiguration jConf;
public void onEnable() {
jFile = new File(getDataFolder(), "joincount.yml");
if (!jFile.exists()) {
try {
jFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
jConf = YamlConfiguration.loadConfiguration(jFile);
reload();
getServer().getPluginManager().registerEvents(this, this);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onJoin(PlayerJoinEvent e) {
if (!e.getPlayer().hasPlayedBefore()) {
reload();
jConf.set("unique-joins", jConf.getInt("unique-joins") == 0 ? 1 : jConf.getInt("unique-joins") + 1);
jConf.set("total-joins", jConf.getInt("total-joins") == 0 ? 1 : jConf.getInt("total-joins") + 1);
save();
return;
}
reload();
jConf.set("total-joins", jConf.getInt("total-joins") == 0 ? 1 : jConf.getInt("total-joins") + 1);
save();
}
public boolean onCommand(CommandSender cs, Command cmd, String lbl, String[] strings) {
if (!(cs instanceof Player)) {
cs.sendMessage("Use this command in-game.");
return true;
}
Player player = (Player) cs;
if (cmd.getName().equalsIgnoreCase("census")) {
if (player.hasPermission(CENSUS_VIEW) || player.hasPermission(CENSUS_ADMIN)) {
if (strings.length == 0) {
player.sendMessage(player.hasPermission(CENSUS_ADMIN) ? T
+ "/census totaljoins [reset] or /census uniquejoins [reset]" : T
+ "/census totaljoins or /census uniquejoins");
}
else if (strings.length == 1) {
if (strings[0].equalsIgnoreCase("totaljoins")) {
player.sendMessage(T + "Total Joins Ever: " + jConf.getInt("total-joins"));
}
else if (strings[0].equalsIgnoreCase("uniquejoins")) {
player.sendMessage(T + "Unique Joins Ever: " + jConf.getInt("unique-joins"));
}
}
else if (strings.length == 2) {
if (strings[0].equalsIgnoreCase("totaljoins")) {
if (strings[1].equalsIgnoreCase("reset")) {
reload();
jConf.set("total-joins", 0);
save();
player.sendMessage(T + "Successfully resetted total joins count.");
}
}
else if (strings[0].equalsIgnoreCase("uniquejoins")) {
if (strings[1].equalsIgnoreCase("reset")) {
reload();
jConf.set("unique-joins", 0);
save();
player.sendMessage(T + "Successfully resetted unique joins count.");
}
}
}
}
}
return true;
}
private void reload() {
if (jConf == null) {
jFile = new File(getDataFolder(), "joincount.yml");
}
jConf = YamlConfiguration.loadConfiguration(jFile);
// Look for defaults in the jar
InputStream defConfigStream = this.getResource("joincount.yml");
if (defConfigStream != null) {
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
jConf.setDefaults(defConfig);
}
}
private void save() {
if (jConf == null || jFile == null) {
return;
}
try {
jConf.save(jFile);
} catch (IOException ex) {
getLogger().log(Level.SEVERE, "Could not save config to " + jFile, ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment