Different Integer, same use

Discussion in 'Spigot Plugin Development' started by zThana, May 30, 2016.

  1. Heey,

    No clue how to describe this, but let's try my best anyways ;P

    I already got this:
    Code (Java):
        public void teleportDungeon(final Player player, String dungeon) {
            player.sendMessage(ChatColor.GRAY + "Loading dungeon...");
         
            final Location dungeonLocation = (Location) plugin.dataConfig.get("dungeons." + dungeon + ".location");
            playerTimers.put(player.getName(), plugin.rankshandler.getTeleportDelay(player));
         
            player.closeInventory();
         
            new Runnable() {
                int taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this, 0L, (playerTimers.get(player.getName()) * 20L));

                @Override
                public void run() {
                    if (playerTimers.get(player.getName()) > 0) {
                        playerTimers.put(player.getName(), playerTimers.get(player.getName()) + 1);

                        switch (playerTimers.get(player.getName())) {
                     
                        case 0:
                            Bukkit.getScheduler().cancelTask(taskID);
                            playerTimers.remove(player.getName());
                            player.teleport(dungeonLocation);
                            player.sendMessage(ChatColor.DARK_AQUA + "Teleporting!");
                        }
                    }
                }
            };
        }
    I got 2 Integers, 5 and 3.
    5 is for Player 1
    3 is for Player 2

    I made a HashMap, String and Integer (Player-name and timer)
    Now inside a runnable, I want the following to happen:

    Set player's timer to either 5 or 3, depending on what rank he is.

    Now let's say the player got the 5-second timer, it should look like this: (Every second)
    █████
    ████
    █████
    █████
    ████
    █████

    Now, when someone has a 3-second timer, it should look like this:(Every second)
    ███
    ██
    ██
    ███

    I don't really know how to tho, anyone could help, please? c:
     
  2. Get the player rank and the seconds associated with it. Take the number from the timer, add one of the green for however many they have, use subtraction to add the red ones?
     
  3. Code (Java):
    plugin.rankshandler.getTeleportDelay(player)
    So aka, create a final int which re-presents their timer and play around with that one?
    What about the map tho?
     
  4. I'd use StringBuilder to insert ChatColor.DARK_RED.toString() to the message "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588". The position to insert could be defined by the value.

    1000 POSTS \o/
    [​IMG]
     
    • Funny Funny x 2
    • Like Like x 1
  5. wat
     
  6. Let's say each char represents 10%.
    StringBuilder bar = new StringBuilder("\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588"); // An uncolored bar.
    bar.insert(10 - x, ChatColor.DARK_RED.toString()); // The more progress, the later the red color is inserted.
    player.sendMessage(ChatColor.GREEN.toString() + bar.toString()); // Send it. I'd prefer an action bar message, though.
     
  7. Oh you were serious
    Thought you were putting in random stuff, then I realised the \u2588 is some kind of color thing (MOTD gg)

    What must "x" be? The timer?
    http://hastebin.com/cixasanopi.tex

    No clue where to put it, but this seems reasonable. o.o
     
  8. \u2588 is █. It is usually considered bad practice to hardcode symbols like § or this.
    x is the percentage of the bar / 10 and not connected to this very special case. If the player's timer is 6 / 6, it should be 10, if it's 3 / 6, it should be 5 and so on.

    I didn't think through it thoroughly, yet, but it should be something like

    int x = Math.round(((double) currentValue / (double) initialValue) * 10);
     
  9. What's must currentValue and initialValue be?
     
  10. [​IMG]
    The current value is the current value and the initial value is the initial value. I assume that your timer counts at some point.
    int initialValue = playerTimers.get(player.getName());
    The initial value is static (not "static" like the keyword); it stays the same while the current value decreases e.g. with "currentValue--" each second. The initialValue defines the proportions of the bar and the currentValue how much of it is green / red.
     
  11. if (playerTimers.get(player.getName()) > 0) {
    playerTimers.put(player.getName(), playerTimers.get(player.getName()) - 1);
     
  12. But why? Modifying the Map so often is not necessary. I'd let the Map value be the same and store the changing value in a field. It's up to you how you do it, but you could make a clean and nice util out of this that works in many cases.
    Code (Text):

    public class ProgressBar extends BukkitRunnable {

        public static final String BAR = "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588";

        private Player player;
        private int seconds;
        private int secondsLeft;

        public ProgressBar(Player player, int seconds) {
            this.player = player;
            this.seconds = seconds;
            this.secondsLeft = seconds;
        }

        @Override
        public void run() {
            int x = Math.round(((double) secondsLeft / (double) seconds) * 10);
            StringBuilder bar = new StringBuilder(BAR); // An uncolored bar.
            bar.insert(10 - x, ChatColor.DARK_RED.toString()); // The more progress, the later the red color is inserted.
            player.sendMessage(ChatColor.GREEN.toString() + bar.toString()); // Send it. I'd prefer an action bar message, though.
        }

    }
     
    Code (Text):
    new ProgressBar(player, playerTimers.get(player.getName())).runTaskTimer(plugin, 0, 20);
    ...for example.
     
    • Winner Winner x 1
  13. Ehm Idk how to like modify this much yet...
    But how would I give every new ProgressBar a new taskID (int) and give the runnables a new, unique taskID and cancel the runnable via the taskID (Bukkit.getScheduler().cancelTask(taskID);) when it's done?
     
  14. If I was you, I'd get rid of this task ID crap.
    returns a BukkitTask object. You can just do
    Code (Text):
    BukkitTask barTask = new ProgressBar(player, playerTimers.get(player.getName())).runTaskTimer(plugin, 0, 20);
    and use the methods of BukkitTask. You can use barTask.getTaskId(), but I'd rather just do barTask.cancel().
     
  15. I seriously don't get it and I feel like I'm messing up my code more and more o.o
     
  16. Aaaaaah yes. Forgot that:
    Code (Text):

    public class ProgressBar extends BukkitRunnable {

        public static final String BAR = "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588";

        private Player player;
        private int seconds;
        private int secondsLeft;

        public ProgressBar(Player player, int seconds) {
            this.player = player;
            this.seconds = seconds;
            this.secondsLeft = seconds;
        }

        @Override
        public void run() {
            int x = Math.round(((double) secondsLeft / (double) seconds) * 10);
            StringBuilder bar = new StringBuilder(BAR); // An uncolored bar.
            bar.insert(10 - x, ChatColor.DARK_RED.toString()); // The more progress, the later the red color is inserted.
            player.sendMessage(ChatColor.GREEN.toString() + bar.toString()); // Send it. I'd prefer an action bar message, though.

            if (secondsLeft == 0) {
                cancel();
            }
        }

    }
     
     
  17. Speaking of forgot, you also forgot to decrement the seconds and secondsLeft variables, so if @Pr0totype2 copy/pastes (which I believe is likely), they'll end up with an infinite runnable that will never be cancelled.
     
    • Agree Agree x 1
    • Optimistic Optimistic x 1
  18. [​IMG]
     
    • Friendly Friendly x 2
  19. I fixed that on my side already ;P