Im making a plugin that adds 1 point to your score on the SIDEBAR. Basic right? Not for me please help! @sgavster @Gopaintman Code: import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scoreboard.DisplaySlot; import org.bukkit.scoreboard.Objective; import org.bukkit.scoreboard.Score; import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.ScoreboardManager; public class sbs extends JavaPlugin implements Listener { public void onPlayerJoinEvent() { ScoreboardManager sbm = Bukkit.getScoreboardManager(); Scoreboard sb = sbm.getNewScoreboard(); Objective obj = sb.registerNewObjective("123", "deathCount"); obj.setDisplaySlot(DisplaySlot.SIDEBAR); obj.setDisplayName("Kills"); Score k = obj.getScore(Bukkit.getOfflinePlayer("Kills:")); // Get a fake // offline } }
Bernabeast Man, I wish I could help you.. I'm still trying to figure this out (no one has succesfully helped me, either ) I'll continue to look around and tell you if I figure anything else, will put this on my favorites tabs thingy SkillSam Doesn't work ;( It never makes the fille.. or anything :/ EDIT by Moderator: merged posts, please use the edit button instead of double posting.
SkillSam I did that; It didn't create it, and when I kill/die from people it doesn't work :C SkillSam Alright: Code:java public FileConfiguration stats; public File sfile; public void setup(Plugin p) { sfile = new File(p.getDataFolder(), "stats.yml"); if (!sfile.exists()) { try { sfile.createNewFile(); } catch (IOException e) { Bukkit.getLogger().severe("Could not create stats.yml!"); } } stats = YamlConfiguration.loadConfiguration(sfile); } on enable: Code:java setup(plugin); onDeath: Code:java @EventHandler public void onDeath(PlayerDeathEvent e) { if(e.getEntity() instanceof Player) { Player p = e.getEntity(); if(p.getKiller() instanceof Player) { int deaths = stats.getInt("Stats." + p.getName() + ".Deaths"); stats.set("Stats." + p.getName() + ".Deaths", deaths + 1); Player k = p.getKiller(); int kills = stats.getInt("Stats." + k.getName() + ".Kills"); stats.set("Stats." + k.getName() + ".Kills", kills + 1); int kd = stats.getInt("Stats." + p.getName() + ".Kills")/stats.getInt("Stats." + p.getName() + ".Deaths"); ScoreboardManager sbm = Bukkit.getScoreboardManager(); Scoreboard sb = sbm.getNewScoreboard(); Objective ob = sb.registerNewObjective("stats", "dummy"); ob.setDisplayName(ChatColor.GOLD + "Your Stats: "); ob.setDisplaySlot(DisplaySlot.SIDEBAR); Score statkills = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_GREEN + "Kills: ")); statkills.setScore(kills); Score statdeaths = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_GREEN + "Deaths: ")); statdeaths.setScore(deaths); Score statkd = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_GREEN + "K/D ")); statkd.setScore(kd); p.setScoreboard(sb); k.setScoreboard(sb); } } }} On join: Code:java @EventHandler public void onScoreboardSet(PlayerJoinEvent e) { Player p = e.getPlayer(); int kills = QuadularPVP.getInstance().stats.getInt("Stats." + p.getName() + ".Kills"); int deaths = QuadularPVP.getInstance().stats.getInt("Stats." + p.getName() + ".Deaths"); int kd = QuadularPVP.getInstance().stats.getInt("Stats." + p.getName() + ".Kills")/QuadularPVP.getInstance().stats.getInt("Stats." + p.getName() + ".Deaths"); ScoreboardManager sbm = Bukkit.getScoreboardManager(); Scoreboard sb = sbm.getNewScoreboard(); Objective ob = sb.registerNewObjective("stats", "dummy"); ob.setDisplayName(ChatColor.GOLD + "Your Stats: "); ob.setDisplaySlot(DisplaySlot.SIDEBAR); Score statkills = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_GREEN + "Kills: ")); statkills.setScore(kills); Score statdeaths = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_GREEN + "Deaths: ")); statdeaths.setScore(deaths); Score statkd = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_GREEN + "K/D ")); statkd.setScore(kd); }} EDIT by Moderator: merged posts, please use the edit button instead of double posting.
sgavster Perhaps in your onEnable method, you can use this: Code:java public void onEnable() { setup(this);} And then I realized that e.getEntity().getKiller() returns a player, so instead you can use null checks: Code:java @EventHandlerpublic void onPlayerDeath (PlayerDeathEvent e) { if (e.getEntity().getKiller() == null) return;} If you're using the stats thing in another class, you can create a method in the original stats file location that could help in other classes. Code:java public FileConfiguration getStats() { return stats;} Other than that, everything else looks fine to me. I forgot one more thing as well. At the end of the PlayerJoinEvent, you have to set the player's scoreboard to the one you have created. Code:java p.setScoreboard(sb); EDIT: You can also just divide the integers you already declared and initialized in the PlayerJoinEvent: Code:java int kd = kills/deaths; EDIT by Moderator: merged posts, please use the edit button instead of double posting.
SkillSam Where would I put Code:java public FileConfiguration getStats() { return stats;} cause QuadularMC.getInstance().... works with arraylists and things
Well, it should rather be where you created the file, so in the class QuadularPVP is where you will create the method, and so here is what it will look like when calling the method: Code:java QuadularPVP.getInstance().getStats()...
sgavster That's right! I forgot, at the beginning of the setup method, you must check whether the data folder already exists. Code:java public void setup(Plugin p) { if (!(p.getDataFolder.exists())) p.getDataFolder.mkdir();}
SkillSam IT WORKS!!!! Thank you, so much. I have 1 question: Wont it cause a bit of 'lag' if there is a lot of players, cause it's scanning through 1 file? :O
sgavster I have absolutely no idea, as I have never ran a server before, so I'll just go along with what Plo124 said.
SkillSam Plo124 Thank you guys a lot.. Really. I have been playing with scoreboard for about 1+ months.. Thank you, alot. SkillSam Hmm, whenever I restart the server and kill someone it goes back to 0 EDIT by Moderator: merged posts, please use the edit button instead of double posting.
sgavster No problem, anytime. First time I've helped someone through someone else thread. sgavster Wow, today is just not my day, I keep forgetting everything. Okay, so you can create another method in your stats file class, which saves it every time an event is fired. Code:java public void saveStats() { try { stats.saveFile(sfile); } catch (IOException e) { Bukkit.getLogger().info("Could not save stats.yml!"); }} Example of using this: Code:java @EventHandlerpublic void onPlayerDeath (PlayerDeathEvent e) { //Code here QuadularPVP.getInstance().saveStats();} EDIT by Moderator: merged posts, please use the edit button instead of double posting.
SkillSam Hey, could you possible write everything that needs to be coded completely? I just don't understand with all these adding methods. IF you are willing so
DrTURTLE2 Oh, okay. Sure, sorry if I don't explain the methods very well. Perhaps we could talk on Skype or message me on which parts you don't understand?
Could you maybe pastebin the hole thing? :3 And also, how could you do it if you want all the players to have an individually scoreboard displaying the player's name? Oh and also SkillSam isn't that two different Scoreboards? I couldn't get that to work because it's two different, but I just made a PlayerMoveEvent which loaded the stats (updated it everytime you move) instead of the onjoin EDIT by Moderator: merged posts, please use the edit button instead of double posting.
DrTURTLE2 Doodledew I have created a class with almost everything you need. Feel free to copy it and edit it to your liking. Code:java import java.io.File;import java.io.IOException;import java.util.Random; import org.bukkit.Bukkit;import org.bukkit.ChatColor;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.Listener;import org.bukkit.event.entity.PlayerDeathEvent;import org.bukkit.event.player.PlayerJoinEvent;import org.bukkit.plugin.Plugin;import org.bukkit.scoreboard.DisplaySlot;import org.bukkit.scoreboard.Objective;import org.bukkit.scoreboard.Score;import org.bukkit.scoreboard.Scoreboard;import org.bukkit.scoreboard.ScoreboardManager; public class SettingsManager implements Listener { private SettingsManager() { } private static SettingsManager instance = new SettingsManager(); public static SettingsManager getInstance() { return instance; } File sfile; FileConfiguration stats; public void setup (Plugin p) { if (!p.getDataFolder().exists()) p.getDataFolder().mkdir(); sfile = new File (p.getDataFolder(), "stats.yml"); if (!sfile.exists()) { try { sfile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } stats = YamlConfiguration.loadConfiguration(sfile); Bukkit.getPluginManager().registerEvents(this, p); } public FileConfiguration getStats() { return stats; } public void saveStats() { try { stats.save(sfile); } catch (IOException e) { e.printStackTrace(); } } public void reloadStats() { stats = YamlConfiguration.loadConfiguration(sfile); } public void setScoreboard (Player p) { ScoreboardManager sbm = Bukkit.getScoreboardManager(); Scoreboard sb = sbm.getNewScoreboard(); Objective ob = sb.registerNewObjective("test", "dummy"); ob.setDisplayName(ChatColor.GREEN + p.getName() + "'s Stats: "); ob.setDisplaySlot(DisplaySlot.SIDEBAR); int KDKills = getStats().getInt("Stats." + p.getName() + ".Kills"); int KDDeaths = getStats().getInt("Stats." + p.getName() + ".Deaths"); int KD = 0; if (KD <= 0) { KD = KDKills; } else { KD = KDKills / KDDeaths; } Score kills = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "Kills: ")); kills.setScore(getStats().getInt("Stats." + p.getName() + ".Kills")); Score deaths = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "Deaths: ")); deaths.setScore(getStats().getInt("Stats." + p.getName() + ".Deaths")); Score killstreak = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "KillStreak: ")); killstreak.setScore(getStats().getInt("Stats." + p.getName() + ".KillStreak")); Score credits = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "Credits: ")); credits.setScore(getStats().getInt("Stats." + p.getName() + ".Credits")); Score kd = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "KD: ")); kd.setScore(KD); p.setScoreboard(sb); } @EventHandler public void PlayerJoin (PlayerJoinEvent e) { Player p = e.getPlayer(); if (getStats().getConfigurationSection("Stats." + p.getName()) == null) { getStats().set("Stats." + p.getName() + ".Kills", 0); getStats().set("Stats." + p.getName() + ".Deaths", 0); getStats().set("Stats." + p.getName() + ".KillStreak", 0); getStats().set("Stats." + p.getName() + ".Credits", 0); saveStats(); } setScoreboard(p); } @EventHandler public void PlayerDeath (PlayerDeathEvent e) { if (e.getEntity() == null && e.getEntity().getKiller() == null) return; Player p = e.getEntity(); Player k = e.getEntity().getKiller(); //Adding the Player Death Counter & Resetting KillStreak int deaths = getStats().getInt("Stats." + p.getName() + ".Deaths"); getStats().set("Stats." + p.getName() + ".Deaths", deaths + 1); getStats().set("Stats." + p.getName() + ".KillStreak", 0); //Adding the Killer's Kill Count, KillStreak, and Random Amount of Credits int kills = getStats().getInt("Stats." + k.getName() + ".Kills"); getStats().set("Stats." + k.getName() + ".Kills", kills + 1); int killstreak = getStats().getInt("Stats." + k.getName() + ".KillStreak"); getStats().set("Stats." + k.getName() + ".KillStreak", killstreak + 1); int credits = getStats().getInt("Stats." + k.getName() + ".Credits"); int percentage = new Random().nextInt(50); getStats().set("Stats." + k.getName() + ".Credits", credits + percentage); saveStats(); setScoreboard(p); setScoreboard(k); } }
YESSSS! Thank you SO much! Very much appreciated. I'm so happy! DD EDIT: It didn't work 0.0 Does it work for you? SkillSam Can I add you on Skype? :3 I have some questions regarding some code. doodle.dew EDIT by Moderator: merged posts, please use the edit button instead of double posting.
SkillSam Added And thanks! EDIT: Why didn't I see that myself? 0.0 fail xD EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Doodledew Hmm...I hope this class fixes the error. Sorry for the late reply, I had school to tend to. Code:java package me.SkillSam.Stats;import java.io.File;import java.io.IOException;import java.util.Random; import org.bukkit.Bukkit;import org.bukkit.ChatColor;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.Listener;import org.bukkit.event.entity.PlayerDeathEvent;import org.bukkit.event.player.PlayerJoinEvent;import org.bukkit.event.player.PlayerMoveEvent;import org.bukkit.plugin.Plugin;import org.bukkit.scoreboard.DisplaySlot;import org.bukkit.scoreboard.Objective;import org.bukkit.scoreboard.Score;import org.bukkit.scoreboard.Scoreboard;import org.bukkit.scoreboard.ScoreboardManager; public class SettingsManager implements Listener { private SettingsManager() { } private static SettingsManager instance = new SettingsManager(); public static SettingsManager getInstance() { return instance; } File sfile; FileConfiguration stats; public void setup (Plugin p) { if (!p.getDataFolder().exists()) p.getDataFolder().mkdir(); sfile = new File (p.getDataFolder(), "stats.yml"); if (!sfile.exists()) { try { sfile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } stats = YamlConfiguration.loadConfiguration(sfile); Bukkit.getPluginManager().registerEvents(this, p); } public FileConfiguration getStats() { return stats; } public void saveStats() { try { stats.save(sfile); } catch (IOException e) { e.printStackTrace(); } } public void reloadStats() { stats = YamlConfiguration.loadConfiguration(sfile); } public void setScoreboard (Player p) { ScoreboardManager sbm = Bukkit.getScoreboardManager(); Scoreboard sb = sbm.getNewScoreboard(); Objective ob = sb.registerNewObjective("test", "dummy"); ob.setDisplayName(ChatColor.GREEN + p.getName() + "'s Stats: "); ob.setDisplaySlot(DisplaySlot.SIDEBAR); int KDKills = getStats().getInt("Stats." + p.getName() + ".Kills"); int KDDeaths = getStats().getInt("Stats." + p.getName() + ".Deaths"); int KD = 0; if (KDDeaths == 0) { KD = KDKills; } else { KD = KDKills / KDDeaths; } Score kills = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "Kills: ")); kills.setScore(getStats().getInt("Stats." + p.getName() + ".Kills")); Score deaths = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "Deaths: ")); deaths.setScore(getStats().getInt("Stats." + p.getName() + ".Deaths")); Score killstreak = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "KillStreak: ")); killstreak.setScore(getStats().getInt("Stats." + p.getName() + ".KillStreak")); Score credits = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "Credits: ")); credits.setScore(getStats().getInt("Stats." + p.getName() + ".Credits")); Score kd = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "KD: ")); kd.setScore(KD); p.setScoreboard(sb); } @EventHandler public void PlayerJoin (PlayerJoinEvent e) { Player p = e.getPlayer(); if (getStats().getConfigurationSection("Stats." + p.getName()) == null) { getStats().set("Stats." + p.getName() + ".Kills", 0); getStats().set("Stats." + p.getName() + ".Deaths", 0); getStats().set("Stats." + p.getName() + ".KillStreak", 0); getStats().set("Stats." + p.getName() + ".Credits", 0); saveStats(); } setScoreboard(p); } @EventHandler public void PlayerMove (PlayerMoveEvent e) { Player p = e.getPlayer(); setScoreboard(p); } @EventHandler public void PlayerDeath (PlayerDeathEvent e) { Player p = e.getEntity(); //Adding the Player Death Counter & Resetting KillStreak int deaths = getStats().getInt("Stats." + p.getName() + ".Deaths"); getStats().set("Stats." + p.getName() + ".Deaths", deaths + 1); getStats().set("Stats." + p.getName() + ".KillStreak", 0); saveStats(); setScoreboard(p); if (e.getEntity().getKiller() == null) return; Player k = e.getEntity().getKiller(); //Adding the Killer's Kill Count, KillStreak, and Random Amount of Credits int kills = getStats().getInt("Stats." + k.getName() + ".Kills"); getStats().set("Stats." + k.getName() + ".Kills", kills + 1); int killstreak = getStats().getInt("Stats." + k.getName() + ".KillStreak"); getStats().set("Stats." + k.getName() + ".KillStreak", killstreak + 1); int credits = getStats().getInt("Stats." + k.getName() + ".Credits"); int percentage = new Random().nextInt(50); getStats().set("Stats." + k.getName() + ".Credits", credits + percentage); saveStats(); setScoreboard(p); setScoreboard(k); } }