diff --git a/src/main/java/de/fanta/challenges/Challenges.java b/src/main/java/de/fanta/challenges/Challenges.java index 96e15bd..4e7729d 100644 --- a/src/main/java/de/fanta/challenges/Challenges.java +++ b/src/main/java/de/fanta/challenges/Challenges.java @@ -23,6 +23,7 @@ import de.fanta.challenges.scoreboard.ScoreBoardMananger; import de.fanta.challenges.scoreboard.ScoreManager; import de.fanta.challenges.utils.ChatUtil; +import de.fanta.challenges.utils.ColorUtils; import de.fanta.challenges.utils.Config; import de.fanta.challenges.utils.Statistics; import de.fanta.challenges.utils.VanishUtils; @@ -114,6 +115,8 @@ private ProtocolManager protocolManager; + private ColorUtils colorUtils; + public static Challenges getPlugin() { return plugin; } @@ -144,6 +147,7 @@ protocolVersion = Bukkit.getUnsafe().getProtocolVersion(); + colorUtils = new ColorUtils(this); this.timer = new Timer(this); this.rndDrops = new RandomDropsChallenge(); @@ -696,4 +700,8 @@ public ProtocolManager getProtocolManager() { return protocolManager; } + + public ColorUtils getColorUtils() { + return colorUtils; + } } diff --git a/src/main/java/de/fanta/challenges/Timer.java b/src/main/java/de/fanta/challenges/Timer.java index 27f552e..9dedbd4 100644 --- a/src/main/java/de/fanta/challenges/Timer.java +++ b/src/main/java/de/fanta/challenges/Timer.java @@ -22,6 +22,9 @@ private TimerMode mode; private long countingSinceTimestamp; + private static final int[] baseColorsTimer = new int[]{0xFF6668, 0xFFB566, 0xFFED66, 0x66FF75, 0x66B8FF, 0xE666FF}; + private static final int[] baseColorsReserveTimer = new int[]{0x7FD4E0, 0x947DFF, 0xFF88F1, 0xFF7D7D}; + public Timer(Challenges plugin) { this.plugin = plugin; this.mode = TimerMode.UP; @@ -57,14 +60,14 @@ } else { time -= (System.currentTimeMillis() - countingSinceTimestamp); countingSinceTimestamp = System.currentTimeMillis(); - sendTimerActionBar(ChatUtil.BLUE); + sendTimerActionBar(true); } } else { time += (System.currentTimeMillis() - countingSinceTimestamp); countingSinceTimestamp = System.currentTimeMillis(); - sendTimerActionBar(ChatUtil.GREEN); + sendTimerActionBar(false); } - }, 20L, 20L); + }, 1L, 1L); if (!Config.getBoolean("firsttimerstart")) { Config.setValue("firsttimerstart", true, false); } @@ -104,16 +107,12 @@ }, 20L, 20L); } - private void sendTimerActionBar(ChatColor color) { + private void sendTimerActionBar(Boolean reverse) { for (Player p : Bukkit.getOnlinePlayers()) { if (!plugin.getVanish().isVanish(p)) { if (Config.getBoolean("showtimer")) { - if (Config.getBoolean("editsettings")) { - p.sendActionBar(formatTime(color)); - } else { - p.sendActionBar(ChatUtil.GREEN + "SpeedRun " + formatTime(color)); - } - + //p.sendActionBar(formatTime(color)); + p.sendActionBar(formateRainbowTime(!Config.getBoolean("editsettings"))); } } @@ -132,6 +131,14 @@ return formattime; } + public String formateRainbowTime() { + return formateRainbowTime(false); + } + + public String formateRainbowTime(boolean speedRun) { + return plugin.getColorUtils().addChatColorToString((speedRun ? "SpeedRun " : "") + formatTime(), mode == TimerMode.UP ? baseColorsTimer : baseColorsReserveTimer, 1.5); + } + public void reverseTimer() { if (mode == TimerMode.DOWN) { mode = TimerMode.UP; @@ -177,7 +184,11 @@ Config.setValue("editsettings", true); } - private enum TimerMode { + public TimerMode getMode() { + return mode; + } + + public enum TimerMode { UP, DOWN } diff --git a/src/main/java/de/fanta/challenges/utils/ColorUtils.java b/src/main/java/de/fanta/challenges/utils/ColorUtils.java new file mode 100644 index 0000000..7c26310 --- /dev/null +++ b/src/main/java/de/fanta/challenges/utils/ColorUtils.java @@ -0,0 +1,195 @@ +package de.fanta.challenges.utils; + +import de.fanta.challenges.Challenges; +import de.fanta.challenges.Timer; +import de.fanta.challenges.schedular.CancellableTask; +import net.md_5.bungee.api.ChatColor; + +import java.awt.*; + +public class ColorUtils { + + private final Challenges plugin; + private long time; + private CancellableTask cancellableTask; + + public ColorUtils(Challenges plugin) { + this.plugin = plugin; + restartTask(1); + } + + public static ChatColor getColorGradient(int[] baseColors, long time, double extraTime, double speed) { + int v = Math.floorMod((int) (time * speed + extraTime * 21), baseColors.length * 100); + int step = v / 100; + double ratio = (v % 100) * 0.01; + int c0 = baseColors[step]; + int c1 = baseColors[(step + 1) % baseColors.length]; + + int r1 = (c0 >> 16) & 0xff; + int g1 = (c0 >> 8) & 0xff; + int b1 = c0 & 0xff; + + int r2 = (c1 >> 16) & 0xff; + int g2 = (c1 >> 8) & 0xff; + int b2 = c1 & 0xff; + + double cmax1 = Math.max(r1, g1); + if (b1 > cmax1) { + cmax1 = b1; + } + double cmin1 = Math.min(r1, g1); + if (b1 < cmin1) { + cmin1 = b1; + } + + double brightness1 = (cmax1) / 255.0; + double saturation1; + if (cmax1 != 0) { + saturation1 = ((cmax1 - cmin1)) / (cmax1); + } else { + saturation1 = 0; + } + double hue1; + if (saturation1 == 0) { + hue1 = 0; + } else { + double redc = ((cmax1 - r1)) / ((cmax1 - cmin1)); + double greenc = ((cmax1 - g1)) / ((cmax1 - cmin1)); + double bluec = ((cmax1 - b1)) / ((cmax1 - cmin1)); + if (r1 == cmax1) { + hue1 = bluec - greenc; + } else if (g1 == cmax1) { + hue1 = 2.0 + redc - bluec; + } else { + hue1 = 4.0 + greenc - redc; + } + hue1 = hue1 / 6.0; + if (hue1 < 0) { + hue1 = hue1 + 1.0; + } + } + + double cmax2 = Math.max(r2, g2); + if (b2 > cmax2) { + cmax2 = b2; + } + double cmin2 = Math.min(r2, g2); + if (b2 < cmin2) { + cmin2 = b2; + } + + double brightness2 = (cmax2) / 255.0; + double saturation2; + if (cmax2 != 0) { + saturation2 = ((cmax2 - cmin2)) / (cmax2); + } else { + saturation2 = 0; + } + double hue2; + if (saturation2 == 0) { + hue2 = 0; + } else { + double redc = ((cmax2 - r2)) / ((cmax2 - cmin2)); + double greenc = ((cmax2 - g2)) / ((cmax2 - cmin2)); + double bluec = ((cmax2 - b2)) / ((cmax2 - cmin2)); + if (r2 == cmax2) { + hue2 = bluec - greenc; + } else if (g2 == cmax2) { + hue2 = 2.0 + redc - bluec; + } else { + hue2 = 4.0 + greenc - redc; + } + hue2 = hue2 / 6.0; + if (hue2 < 0) { + hue2 = hue2 + 1.0; + } + } + + double ratio2 = 1.0 - ratio; + double brightness = brightness1 * ratio2 + brightness2 * ratio; + double saturation = saturation1 * ratio2 + saturation2 * ratio; + if (hue2 - hue1 > 0.5) { + hue1 += 1; + } else if (hue1 - hue2 > 0.5) { + hue2 += 1; + } + double hue = hue1 * ratio2 + hue2 * ratio; + if (hue > 1.0) { + hue -= 1.0; + } + + double r = 0.0; + double g = 0.0; + double b = 0.0; + if (saturation == 0) { + r = g = b = (int) (brightness * 255.0f + 0.5f); + } else { + double h = (hue - Math.floor(hue)) * 6.0f; + double f = h - Math.floor(h); + double p = brightness * (1.0f - saturation); + double q = brightness * (1.0f - saturation * f); + double t = brightness * (1.0f - (saturation * (1.0f - f))); + switch ((int) h) { + case 0 -> { + r = (int) (brightness * 255.0f + 0.5f); + g = (int) (t * 255.0f + 0.5f); + b = (int) (p * 255.0f + 0.5f); + } + case 1 -> { + r = (int) (q * 255.0f + 0.5f); + g = (int) (brightness * 255.0f + 0.5f); + b = (int) (p * 255.0f + 0.5f); + } + case 2 -> { + r = (int) (p * 255.0f + 0.5f); + g = (int) (brightness * 255.0f + 0.5f); + b = (int) (t * 255.0f + 0.5f); + } + case 3 -> { + r = (int) (p * 255.0f + 0.5f); + g = (int) (q * 255.0f + 0.5f); + b = (int) (brightness * 255.0f + 0.5f); + } + case 4 -> { + r = (int) (t * 255.0f + 0.5f); + g = (int) (p * 255.0f + 0.5f); + b = (int) (brightness * 255.0f + 0.5f); + } + case 5 -> { + r = (int) (brightness * 255.0f + 0.5f); + g = (int) (p * 255.0f + 0.5f); + b = (int) (q * 255.0f + 0.5f); + } + } + } + int rFinal = (int) r; + int gFinal = (int) g; + int bFinal = (int) b; + + int c = (rFinal << 16) | (gFinal << 8) | (bFinal); + + return ChatColor.of(new Color(c)); + } + + public void restartTask(long l) { + if (this.cancellableTask != null) { + cancellableTask.cancel(); + } + + cancellableTask = plugin.getScheduler().runGlobalAtFixedRate(() -> time++, 0L, l); + } + + public long getTime() { + return this.time; + } + + public String addChatColorToString(String input, int[] baseColors, double speed) { + StringBuilder output = new StringBuilder(); + double extraTime = 0.0; + for (char c : input.toCharArray()) { + extraTime = plugin.getTimer().getMode() == Timer.TimerMode.UP ? extraTime - 0.40 : extraTime + 0.40; + output.append(getColorGradient(baseColors, getTime(), extraTime, speed)).append(ChatColor.BOLD).append(c); + } + return output.toString(); + } +}