# Minecraft Scoreboard



## Bob_Gmbh (2. Aug 2018)

Ich habe ein Essentialssystem für Minecraft als plugin gecodet daher ich noch relativ neu in der Java sprache bin hätte ich eine frage bezüglich des Scoreboards. Ich habe nähmlch ein coinssystem programmiert und möchte das diese Coins im Scoreboard angeziegt werden.



Code für Scoreboard :


```
package de.bob.listener;



import java.io.File;

import java.util.Collection;

import java.util.HashMap;

import java.util.UUID;



import org.apache.commons.io.output.ThresholdingOutputStream;

import org.bukkit.Bukkit;

import org.bukkit.OfflinePlayer;

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.player.PlayerJoinEvent;

import org.bukkit.event.player.PlayerQuitEvent;

import org.bukkit.scheduler.BukkitScheduler;

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;

import org.bukkit.scoreboard.Team;



import com.avaje.ebeaninternal.server.subclass.GetterSetterMethods;



import de.bob.commands.COMMAND_Coins;



import de.bob.main.Main;

import io.netty.channel.local.LocalAddress;



public class ScoreboardJoinListener

 implements Listener

{

 private static HashMap<String, String[]> savedData = new HashMap();



 public static void reload()

 {

   for (Player all : Bukkit.getOnlinePlayers() ) {

     createBoard(all);

   }

 }



 public static void createBoard(Player p)

 {

   Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();

   board.registerNewTeam("00000Inhaber");

   board.registerNewTeam("00001Admin");

   board.registerNewTeam("00002SrMod");

   board.registerNewTeam("00003Mod");

   board.registerNewTeam("00004SrDev");

   board.registerNewTeam("00005Dev");

   board.registerNewTeam("00006SrBuilder");

   board.registerNewTeam("00007Builder");

   board.registerNewTeam("00008Sup");

   board.registerNewTeam("00009Freund");

   board.registerNewTeam("99999Spieler");



   board.getTeam("00000Inhaber").setPrefix("§4Inhaber §7» ");

   board.getTeam("00001Admin").setPrefix("§4Admin §7» ");

   board.getTeam("00002SrMod").setPrefix("§cSrMod §7» ");

   board.getTeam("00003Mod").setPrefix("§cMod §7» ");

   board.getTeam("00004SrDev").setPrefix("§3SrDev §7» ");

   board.getTeam("00005Dev").setPrefix("§3Dev §7» ");

   board.getTeam("00006SrBuilder").setPrefix("§6SrBuilder §7» ");

   board.getTeam("00007Builder").setPrefix("§6Builder §7» ");

   board.getTeam("00008Sup").setPrefix("§9Sup §7» ");

   board.getTeam("00009Freund").setPrefix("§eFreund §7» ");

   board.getTeam("99999Spieler").setPrefix("§7Spieler §7» ");



   p.setScoreboard(board);

   for (Player all : Bukkit.getOnlinePlayers()) {

     setTeam(p, all);

   }

   createSidebar(p);

 }



 public static void setTeam(Player p, Player toUpdate)

 {

   String team = getTeam(toUpdate);



   Team scoreboardTeam = p.getScoreboard().getTeam(team);

   scoreboardTeam.addPlayer(toUpdate);

 }



 private static String getTeam(Player p)

 {

   if (p.hasPermission("system.Inhaber")) {

     return "00000Inhaber";

   }

   if (p.hasPermission("system.Admin")) {

     return "00001Admin";

   }

   if (p.hasPermission("system.SrModerator")) {

     return "00002SrMod";

   }

   if (p.hasPermission("system.Moderator")) {

     return "00003Mod";

   }

   if (p.hasPermission("system.SrDeveloper")) {

     return "00004SrDev";

   }

   if (p.hasPermission("system.Developer")) {

     return "00005Dev";

   }

   if (p.hasPermission("system.SrBuilder")) {

     return "00006SrBuilder";

   }

   if (p.hasPermission("system.Builder")) {

     return "00007Builder";

   }

   if (p.hasPermission("system.Supporter")) {

     return "00008Sup";

   }

   if (p.hasPermission("system.Freund")) {

      return "00009Freund";

   }

   return "99999Spieler";

 }



 public static void updateOnline()

 {

   int online = Bukkit.getOnlinePlayers().size();

   for (Player all : Bukkit.getOnlinePlayers()) {

     updateOnlineCount(all, online);

   }

 }



 private static void updateOnlineCount(Player p, int online)

 {

   Scoreboard board = p.getScoreboard();

   String lastValue = ((String[])savedData.get(p.getUniqueId().toString()))[0];

   if (lastValue != null) {

     board.resetScores(lastValue);

   }

   Objective obj = board.getObjective(DisplaySlot.SIDEBAR);

   String newScore = "§7" + online + "§8/§7" + Bukkit.getMaxPlayers();

   obj.getScore(newScore).setScore(6);



   String[] value = (String[])savedData.get(p.getUniqueId().toString());

   value[0] = newScore;

   savedData.put(p.getUniqueId().toString(), value);

 }

 public static void createSidebar(Player p)

 {

   Scoreboard board = p.getScoreboard();

   Objective obj = board.registerNewObjective("abcd", "abcd");



   obj.setDisplaySlot(DisplaySlot.SIDEBAR);

   obj.setDisplayName("§7§ §6Spectralgames.net §7");



   obj.getScore("§a ").setScore(11);

   obj.getScore("§cRang:").setScore(10);

   if (p.hasPermission("system.Inhaber")) {

     obj.getScore("§4Inhaber").setScore(9);

   } else if (p.hasPermission("system.Admin")) {

     obj.getScore("§4Admin").setScore(9);

   } else if (p.hasPermission("system.SrModerator")) {

     obj.getScore("§cSrMod").setScore(9);

   } else if (p.hasPermission("system.Moderator")) {

     obj.getScore("§cMod").setScore(9);

   } else if (p.hasPermission("system.SrDeveloper")) {

     obj.getScore("§3SrDev").setScore(9);

   } else if (p.hasPermission("system.Developer")) {

     obj.getScore("§3Dev").setScore(9);

   } else if (p.hasPermission("system.SrBuilder")) {

     obj.getScore("§6SrBuilder").setScore(9);

   } else if (p.hasPermission("system.Builder")) {

     obj.getScore("§6Builder").setScore(9);

   } else if (p.hasPermission("system.Supporter")) {

     obj.getScore("§9Sup").setScore(9);

   } else if (p.hasPermission("system.freund")){

     obj.getScore("§eFreund").setScore(9);

   } else {

      obj.getScore("§7Spieler").setScore(9);

   }

   String playerCount = "§7" + Bukkit.getOnlinePlayers().size() + "§8/§7" + Bukkit.getMaxPlayers();

   obj.getScore("§b ").setScore(8);

   obj.getScore("§aOnline: ").setScore(7);

   obj.getScore(playerCount).setScore(6);

   obj.getScore("§c ").setScore(5);

   obj.getScore("§eCoins:").setScore(4);

   obj.getScore("§a").setScore(3);

   obj.getScore("").setScore(2);

   obj.getScore("§3TeamSpeak:").setScore(1);

   obj.getScore("§7Spectralgames.net").setScore(0);



   savedData.put(p.getUniqueId().toString(), new String[] {

     playerCount });

 }



 @EventHandler

 public void handlePlayerJoin(PlayerJoinEvent e)

 {

   Player p = e.getPlayer();



   createBoard(p);

   for (Player all : Bukkit.getOnlinePlayers()) {

     setTeam(all, p);

   }

   updateOnline();

 }



 @EventHandler

 public void handlePlayerleave(PlayerQuitEvent e)

 {

   Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new Runnable()

   {

     public void run() {}

   }, 10L);

 }

}
```


```
package de.bob.commands;



import java.io.File;

import java.io.IOException;



import org.bukkit.Bukkit;

import org.bukkit.Sound;

import org.bukkit.command.Command;

import org.bukkit.command.CommandExecutor;

import org.bukkit.command.CommandSender;

import org.bukkit.configuration.file.FileConfiguration;

import org.bukkit.configuration.file.YamlConfiguration;

import org.bukkit.entity.Player;



import de.bob.main.Main;



public class COMMAND_Coins

 implements CommandExecutor

{

 public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)

 {

   Player p = (Player)sender;

   if (args.length == 0)

   {

     p.sendMessage("§8[§2Coins§8]§7 Du besitzt: §6" + getMoney(p.getName()) + " Coin(s)");

   }

   else if (args.length == 3)

   {

     if (args[0].equalsIgnoreCase("add"))

     {

       if (p.hasPermission("eco.add"))

       {

         String target = args[1];

         Integer amount = Integer.valueOf(args[2]);

         addMoney(target, amount.intValue());

         p.sendMessage("§8[§2Coins§8]§7 Dem Spieler §6" + target + " §7wurden §6" + amount + " §7Coin(s) hinzugef§gt!");

         p.playSound(p.getLocation(), Sound.LEVEL_UP, 1.0F, 1.0F);

       }

     }

     else if (args[0].equalsIgnoreCase("remove"))

     {

       if (p.hasPermission("eco.remove"))

       {

         String target = args[1];

         Integer amount = Integer.valueOf(args[2]);

         removeMoney(target, amount.intValue());

         p.sendMessage("§8[§2Coins§8]§7 Dem Spieler §6" + target + " §7wurden §6" + amount + " §7Coin(s) entfernt!");

         p.playSound(p.getLocation(), Sound.LEVEL_UP, 1.0F, 1.0F);

       }

     }

     else if (args[0].equalsIgnoreCase("set"))

     {

       if (p.hasPermission("eco.set"))

       {

         String target = args[1];

         Integer amount = Integer.valueOf(args[2]);

         setMoney(target, amount.intValue());

         p.sendMessage("§8[§2Coins§8]§7 Die Coins vom Spieler §6" + target + " §7wurden auf §6" + amount + " §7Coin(s) gesetzt!");

         p.playSound(p.getLocation(), Sound.LEVEL_UP, 1.0F, 1.0F);

       }

     }

     else if (args[0].equalsIgnoreCase("pay"))

     {

       String target = args[1];

       Integer amount = Integer.valueOf(args[2]);

       if(amount < 0) {

          p.sendMessage("§8[§2Coins§8]§7 §cDie zahl ist negativ!");

       }

       else if (hassEnoughMoney(p.getName(), amount)) {

          removeMoney(p.getName(), amount);

          p.sendMessage("§8[§2Coins§8]§7 Du hast dem Spieler §6" + target + " " + amount + " §7Coin(s) überwiesen!");

          addMoney(target, amount.intValue());

          Player z = Bukkit.getPlayer(target);

          z.sendMessage("§8[§2Coins§8]§7 Du hast vom Spieler §6" + p.getName() + " " + amount + " §7Coin(s) Bekommen!");

       } else {

          p.sendMessage("§8[§2Coins§8]§7 Du hast nicht Genügent Coin(s)!");

       }

     }

   }

   else

   {

     p.playSound(p.getLocation(), Sound.NOTE_BASS, 1.0F, 1.0F);

     p.sendMessage("§8[§2Coins§8]§7------------ Coins-Help -----------");

     p.sendMessage("§8[§2Coins§8]§7 Benutze /coins add [Name] [Coins]");

     p.sendMessage("§8[§2Coins§8]§7 Benutze /coins remove [Name] [Coins]");

     p.sendMessage("§8[§2Coins§8]§7 Benutze /coins set [Name] [Coins]");

     p.sendMessage("§8[§2Coins§8]§7 Benutze /coins pay [Name] [Coins]");

     p.sendMessage("§8[§2Coins§8]§7------------ Coins-Help -----------");

   }

   return true;

 }



 public Integer getMoney(String name)

 {

   File file = new File("plugins/CityBuild/Coins", "coins.yml");

   FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);



   int coins = cfg.getInt(name + ".coins");

   return Integer.valueOf(coins);

 }



 public void addMoney(String name, int amount)

 {

   File file = new File("plugins/CityBuild/Coins", "coins.yml");

   FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);



   int coins = cfg.getInt(name + ".coins");

   coins += amount;

   cfg.set(name + ".coins", Integer.valueOf(coins));

   try

   {

     cfg.save(file);

   }

   catch (IOException e)

   {

     e.printStackTrace();

   }

 }



 public void removeMoney(String name, int amount)

 {

   File file = new File("plugins/CityBuild/Coins", "coins.yml");

   FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);



   int coins = cfg.getInt(name + ".coins");

   coins -= amount;

   cfg.set(name + ".coins", Integer.valueOf(coins));

   try

   {

     cfg.save(file);

   }

   catch (IOException e)

   {

     e.printStackTrace();

   }

 }



 public void setMoney(String name, int amount)

 {

   File file = new File("plugins/CityBuild/Coins", "coins.yml");

   FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);



   cfg.set(name + ".coins", Integer.valueOf(amount));

   try

   {

     cfg.save(file);

   }

   catch (IOException e)

   {

     e.printStackTrace();

   }

 }



 public boolean hassEnoughMoney(String name, int amount)

 {

   File file = new File("plugins/CityBuild/Coins", "coins.yml");

   FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);



   int coins = cfg.getInt(name + ".coins");

   if (coins >= amount) {

     return true;

   }

   return false;

 }



 public void pay(String name, int amount)

 {

   File file = new File("plugins/CityBuild/Coins", "coins.yml");

   FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);



   int coins = cfg.getInt(name + ".coins");

   coins -= amount;

 }

}
```



Das währe alles.



Ich bedanke mich im Vorraus

L.G. Bob_Gmbh


----------



## Robat (2. Aug 2018)

und was ist jetzt deine Frage?


----------



## Bob_Gmbh (2. Aug 2018)

Wie ich aus der coins.yml die coins auslesen kann und diese im scoreboard  anzeigen lässt


----------



## VfL_Freak (2. Aug 2018)

Moin,
und nutze bitte die Code-Tags, so bekommt man ja Augenkrebs 
VG Klaus


----------

