1.12.2 Need help with Scoreboard plugin!

Discussion in 'Spigot Plugin Development' started by LiamHBest, Mar 31, 2020.

  1. Hey! Im a new developer that is trying to code a scoreboard plugin. I have run in to some problems that i cant fix. When someone joins a specific world i want the "Lobby: " score on the scoreboard to change depending on which world they joined. But then when a player is switching world I also want the "Lobby: " score to change depending on which world they switched to! I have tried to do this in every way but I cant figure out how! Both the scoreboard just tries to override each other! I will send my code here!
    Im a new developer so please dont blame me for my bad code and only using one class!
    Thanks! :)

    Code (Text):
    package me.liamhbest.scoreboardTest;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.HandlerList;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerChangedWorldEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.Plugin;
    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;

    import me.clip.placeholderapi.PlaceholderAPI;

    public class Test extends JavaPlugin implements Listener {

        static Plugin plugin;
        public void onEnable() {
            plugin = this;
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
        @EventHandler
        public void PlayerJoin(PlayerJoinEvent e) {
            final Player p = e.getPlayer();
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            final Scoreboard board = manager.getNewScoreboard();
            Objective objective = board.registerNewObjective("test", "dummy");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "ALFACRAFT" + ChatColor.GRAY + " " + ChatColor.ITALIC + "");
       
            Score score = objective.getScore(ChatColor.GREEN + " ");
            score.setScore(12);
       
            Score score1 = objective.getScore(ChatColor.AQUA + "Rank: " + ChatColor.WHITE + "PLAYERS RANK");
            score1.setScore(11);
       
            Score score2 = objective.getScore(ChatColor.AQUA + "Level: " + ChatColor.WHITE + "PLAYERS LEVEL");
            score2.setScore(10);
       
            Score score3 = objective.getScore(ChatColor.AQUA + "Gems: " + ChatColor.WHITE + "PLAYERS GEMS");
            score3.setScore(9);
       
            Score score4 = objective.getScore(ChatColor.WHITE + "  ");
            score4.setScore(8);
       
            Score score5 = objective.getScore(ChatColor.AQUA + "Mystery Boxes: " + PlaceholderAPI.setPlaceholders(p, ChatColor.WHITE + "%gadgetsmenu_mystery_boxes%"));
            score5.setScore(7);
       
            Score score6 = objective.getScore(ChatColor.AQUA + "Mystery Dust: " + PlaceholderAPI.setPlaceholders(p, ChatColor.WHITE + "%gadgetsmenu_mystery_dust%"));
            score6.setScore(6);
            Score score7 = objective.getScore(ChatColor.AQUA + "");
            score7.setScore(5);
       
            Score score8 = objective.getScore(ChatColor.AQUA + "Players Online: " + ChatColor.WHITE + (Bukkit.getOnlinePlayers().size()));
            score8.setScore(4);
       
            if (p.getWorld().getName().equalsIgnoreCase("world")) {
                Score scoreworld = objective.getScore(ChatColor.AQUA + "Lobby: " + ChatColor.WHITE + "#1");
                scoreworld.setScore(3);
            }
       
       
            if (p.getWorld().getName().equalsIgnoreCase("hub1_main")) {
                Score scorehub1 = objective.getScore(ChatColor.AQUA + "Lobby: " + ChatColor.WHITE + "#2");
                scorehub1.setScore(3);
            }
       
            Score score10 = objective.getScore(ChatColor.AQUA + " ");
            score10.setScore(2);
       
            Score score11 = objective.getScore(ChatColor.YELLOW + "www.alfacraft.buycraft.net");
            score11.setScore(1);
       
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Test.plugin, new Runnable() {
                public void run() {

       
            p.setScoreboard(board);
               
                }
            },0, 10 * 2); {
           
            }
        }
       
        @EventHandler
        public void onWorldSwitch(PlayerChangedWorldEvent event) {
            final Player p = event.getPlayer();
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            final Scoreboard board1 = manager.getNewScoreboard();
            Objective objective = board1.registerNewObjective("test", "dummy");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "ALFACRAFT" + ChatColor.GRAY + " " + ChatColor.ITALIC + "");
       
            Score score = objective.getScore(ChatColor.GREEN + " ");
            score.setScore(12);
       
            Score score1 = objective.getScore(ChatColor.AQUA + "Rank: " + ChatColor.WHITE + "PLAYERS RANK");
            score1.setScore(11);
         
            Score score2 = objective.getScore(ChatColor.AQUA + "Level: " + ChatColor.WHITE + "PLAYERS LEVEL");
            score2.setScore(10);
       
            Score score3 = objective.getScore(ChatColor.AQUA + "Gems: " + ChatColor.WHITE + "PLAYERS GEMS");
            score3.setScore(9);
       
            Score score4 = objective.getScore(ChatColor.WHITE + "  ");
            score4.setScore(8);
       
            Score score5 = objective.getScore(ChatColor.AQUA + "Mystery Boxes: " + PlaceholderAPI.setPlaceholders(p, ChatColor.WHITE + "%gadgetsmenu_mystery_boxes%"));
            score5.setScore(7);
       
            Score score6 = objective.getScore(ChatColor.AQUA + "Mystery Dust: " + PlaceholderAPI.setPlaceholders(p, ChatColor.WHITE + "%gadgetsmenu_mystery_dust%"));
            score6.setScore(6);
            Score score7 = objective.getScore(ChatColor.AQUA + "");
            score7.setScore(5);
       
            Score score8 = objective.getScore(ChatColor.AQUA + "Players Online: " + ChatColor.WHITE + (Bukkit.getOnlinePlayers().size()));
            score8.setScore(4);
       
            if (p.getWorld().getName().equalsIgnoreCase("world")) {
                Score scoreworldA = objective.getScore(ChatColor.AQUA + "Lobby: " + ChatColor.WHITE + "#1");
                scoreworldA.setScore(3);
            }
       
            if (p.getWorld().getName().equalsIgnoreCase("hub1_main")) {
                Score scorehub1A = objective.getScore(ChatColor.AQUA + "Lobby: " + ChatColor.WHITE + "#2");
                scorehub1A.setScore(3);
            }
       
            Score score10 = objective.getScore(ChatColor.AQUA + " ");
            score10.setScore(2);
       
            Score score11 = objective.getScore(ChatColor.YELLOW + "www.alfacraft.buycraft.net");
            score11.setScore(1);
       
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Test.plugin, new Runnable() {
                public void run() {

       
            p.setScoreboard(board1);
               
               
                }
            },0, 10 * 2); {
           
            }

       
        }

    }
     
     
  2. You are sending two scoreboards to the player. One when he joins and one whenever he changes world so of course this is gonna overriding. You should probably use some packets, but if you wanna stick to the spigot api on the world change event you'll need to get this player scoreboard, delete the lobby line and re-set it. For this, you'll probably want to use HashMap to store the player name and the scoreboard associated with that player. Also, don't forget to remove the player from the hashmap whenever he leaves the server.
     
  3. Okay thanks for the answer! As I said im new to coding so i think I will have to figure out another way to do it because your way sounds like I really hard way to do it but a way that will work!
    Is there any way to delete the first scoreboard when the player switch world, meaning when the player joins the server the Join Event handels the scoreboard and when the Player Switches world it deletes the Player Join scoreboard and from there making the Switch World scoreboard in-charge. And then when the player leaves the system will automatically reset as the scoreboard will be set again because the player joins and automatically creats a loop?
    Or is that way just impossible or as difficult as your method??
     
  4. This way sounds more difficult, less efficient and will make the scoreboard flash.
     
  5. Okay! Im sorry if I ask a lot of question but i dont really get any of these things, either your way or my way. I might just delete the feature as I have no idea of have to use HashMaps, delete a score line, reset it and then do all the coding to that.
    But if I in some way manage to do this, then how will I make the first scoreboard not to override the second scoreboard just by reset a score line, the first scoreboard will still be there even if I delete the line, am i right?
     
  6. Yes, the scoreboard will still be there, but the line will be removed / replaced with another.
    You can also use any kind of scoreboard API to make this easier for yourself: https://github.com/MinusKube/Netherboard
     
  7. Yea thanks! I will try to use the API as best as I can! Can I delete or reset scores with the help of that API??
     
  8. Really thanks! This has made it a hell lot of easier and I have saved a lot of time! Just to ask you when i can is there any good Wiki or tutorial how to add this to maven! I use Eclipse IDE
    Im new to develop so I dont have any ideas how maven work!
     
  9. https://www.vogella.com/tutorials/EclipseMaven/article.html

    This is the best one I’ve found. Go directly to step 3 since eclipse now has maven integrated with it. If you need more help just let me know
     
  10. Thanks! I created a maven project but i dont know where to paste the maven stuff that connect Netherboard to my plugin? I will paste the maven code here, can you please tell me where to paste the Netherboard maven thing??

    Code (Text):
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>scoreboardTest2</groupId>
      <artifactId>scoreboardTest2</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <build>
        <sourceDirectory>src</sourceDirectory>
        <resources>
          <resource>
            <directory>src</directory>
            <excludes>
              <exclude>**/*.java</exclude>
            </excludes>
          </resource>
        </resources>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
     
     
  11. Right before the </project> at the end, add this


    Code (Text):
    <repositories>
        <!-- This adds the Spigot Maven repository to the build -->
        <repository>
            <id>spigot-repo</id>
        <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
        </repository>
    </repositories>

    <dependencies>
        <!--This adds the Spigot API artifact to the build -->
        <dependency>
               <groupId>org.spigotmc</groupId>
               <artifactId>spigot-api</artifactId>
               <version>1.15.2-R0.1-SNAPSHOT</version>
               <scope>provided</scope>
        </dependency>

    <dependency>
      <groupId>fr.minuskube</groupId>
      <artifactId>netherboard-bukkit</artifactId>
      <version>1.1.3</version>
    </dependency>
    </dependencies>
     
  12. Thanks! I hope this is right now! My maven code looks like this now so everything shoud work right??
    Code (Text):
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
     
      <groupId>scoreboardTest2</groupId>
      <artifactId>scoreboardTest2</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <build>
        <sourceDirectory>src</sourceDirectory>
        <resources>
          <resource>
            <directory>src</directory>
            <excludes>
              <exclude>**/*.java</exclude>
            </excludes>
          </resource>
        </resources>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    <repositories>
        <!-- This adds the Spigot Maven repository to the build -->
        <repository>
            <id>spigot-repo</id>
        <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
        </repository>
    </repositories>

    <dependencies>
        <!--This adds the Spigot API artifact to the build -->
        <dependency>
               <groupId>org.spigotmc</groupId>
               <artifactId>spigot-api</artifactId>
               <version>1.15.2-R0.1-SNAPSHOT</version>
               <scope>provided</scope>
        </dependency>

    <dependency>
      <groupId>fr.minuskube</groupId>
      <artifactId>netherboard-bukkit</artifactId>
      <version>1.1.3</version>
    </dependency>
    </dependencies>
     
  13. No you had to put it before the </project> not after it
     
  14. THANKS! It finally works! But i cant export the plugin becuase it says "Jar does not contain plugin.yml". But im sorry do interupt you but i just wanted to check and ask you dont need a jar file to use this API because if not I have something wrong with the plugin.yml

    My plugin.yml
    Code (Text):
    main: me.liamhbest.scoreboardTest.Main
    name: testScoreboard
    author: LiamH
    version: 1.0
    description: Liams Plugin
     
  15. In your maven project, there should be a folder called “resources”. That’s where you wanna put your config.yml and plugin.yml - those are gonna be included inside your jar when you’ll compile the code with maven
     
  16. Like this??
    Code (Text):
        </resources>
        name: testScoreboard
    version: 1.0.0
    main: me.liamhbest.scoreboardTest.Test1
    author: LiamHBest
    description: a test!
    depend: [SmartInvs]
     
  17. Sorry for misguiding you, I do not use Eclipse.
    You will need to create the folder yourself.
    https://prnt.sc/rr3m5d
    Name it resources - this is where you'll put your plugin.yml file and config.yml
     
  18. Thanks you again for everything!! I made the folder put a copy of my plugin.yml and config.yml there and sooo! But one question, are you sure that thr API work because now my console say that my PlayerJoin event is not working. But i did the exact same PlayerJoin event as I always do and that usually works, but when i did it with the API it does not work??
     
  19. What is the console saying exactly?