Skip to content

Commit

Permalink
HEX support
Browse files Browse the repository at this point in the history
- Added HEX support, thanks to @harry0198 and his HexiTextLib! He did 99% of the work and I just needed to make so small adjustments. #21
- Catch possible NPE with getOfflinePlayer #32
- Use Logger instead of sysout #31
- Let ServerlistMOTD shut up and stop spamming console 😆
  • Loading branch information
strumswell committed Sep 12, 2021
1 parent eb58286 commit ea84d5a
Show file tree
Hide file tree
Showing 15 changed files with 257 additions and 24 deletions.
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: ServerlistMOTD
author: Strumswell
version: X-2020-09-05
version: X-2021-09-12-BETA
description: Change your Serverlist Motd!
load: POSTWORLD
depend: [ProtocolLib]
Expand Down
20 changes: 10 additions & 10 deletions src/cloud/bolte/serverlistmotd/SpigotConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,11 @@ public static void saveSmotdConfig() {
*/
public void configWorldCheck() {
if (Bukkit.getWorld(getWeatherWorld()) == null || Bukkit.getWorld(getTimeWorld()) == null) {
System.out.println("[ServerlistMOTD] ------------------------");
Bukkit.getLogger().info("------------------------");
//Informing user of mismatch
Bukkit.getLogger().severe(
"[ServerlistMOTD] CAN'T FIND THE DEFINED WORLD FROM YOUR CONFIG!");
System.out.println("[ServerlistMOTD] Searching for available world...");
"CAN'T FIND THE DEFINED WORLD FROM YOUR CONFIG!");
Bukkit.getLogger().info("Searching for available world...");

//Search shortest world name
String worldName = "";
Expand All @@ -416,9 +416,9 @@ public void configWorldCheck() {
main.getConfig().set("Variables.TimeVariable.World", worldName);
main.getConfig().set("Variables.WeatherVariable.World", worldName);
SpigotConfig.saveSmotdConfig();
System.out.println("[ServerlistMOTD] Found '" + worldName + "‘ and saved it to config.");
System.out.println("[ServerlistMOTD] We're good now. ;-)");
System.out.println("[ServerlistMOTD] ------------------------");
Bukkit.getLogger().info("Found '" + worldName + "‘ and saved it to config.");
Bukkit.getLogger().info("We're good now. ;-)");
Bukkit.getLogger().info("------------------------");
}
}

Expand All @@ -431,24 +431,24 @@ public static void migrateConfig() {
File newFile = new File(main.getDataFolder(), "config_old.yml");

if (newFile.exists()) {
System.out.println("[ServerlistMOTD] Remove your old config.yml!");
Bukkit.getLogger().severe("Remove your old config.yml!");
Bukkit.getPluginManager().disablePlugin(main);
}

boolean fileRenamed = oldConfig.renameTo(newFile);
if (fileRenamed) {
main.saveDefaultConfig();
main.reloadConfig();
System.out.println("[ServerlistMOTD] Renamed old config and created new config!");
Bukkit.getLogger().info("Renamed old config and created new config!");
}
}
if (main.getConfig().getDouble("DoNOTtouchMe") == 10.0) {
System.out.println("[ServerlistMOTD] Adding new features to config.yml...");
Bukkit.getLogger().info("Adding new features to config.yml...");
main.getConfig().set("Slots.OutdatedClientText.Enable", false);
main.getConfig().set("Slots.OutdatedClientText.Message", "Use Minecraft 1.16 &r&7%realonline%&8/&7%realslots%");
main.getConfig().set("DoNOTtouchMe", 10.1);
saveSmotdConfig();
System.out.println("[ServerlistMOTD] Config successfully migrated to new version.");
Bukkit.getLogger().info("Config successfully migrated to new version.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import cloud.bolte.serverlistmotd.slots.OutdatedClientText;
import cloud.bolte.serverlistmotd.slots.SlotsPlusOne;
import cloud.bolte.serverlistmotd.slots.VersionText;
import org.bukkit.Bukkit;

/*
* ServerlistMOTD (c) by Strumswell, Philipp Bolte
Expand All @@ -37,7 +38,7 @@ private static void setPlugin(Main plugin) {
}

public void listenToServerlistPackets() {
System.out.println("[ServerlistMOTD] Hooking into ProtocolLib.");
Bukkit.getLogger().info("Hooking into ProtocolLib.");
ProtocolLibrary.getProtocolManager().addPacketListener(
new PacketAdapter(PacketAdapter.params(main, PacketType.Status.Server.SERVER_INFO).optionAsync()) {
@Override
Expand Down
2 changes: 2 additions & 0 deletions src/cloud/bolte/serverlistmotd/motd/BanManagerMotd.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.net.InetAddress;
import java.util.Date;

import cloud.bolte.serverlistmotd.util.HexResolver;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
Expand Down Expand Up @@ -74,6 +75,7 @@ public String formatMotd(String motd, InetAddress ip) {
.replace("%expyear%", ban.banExpDateYear(playerName));
}
formattedMotd = PapiIntegration.replaceVariables(player, formattedMotd);
formattedMotd = HexResolver.parseHexString(formattedMotd);
return formattedMotd;
}

Expand Down
2 changes: 2 additions & 0 deletions src/cloud/bolte/serverlistmotd/motd/BanMotd.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.InetAddress;

import cloud.bolte.serverlistmotd.util.HexResolver;
import org.bukkit.BanList.Type;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -67,6 +68,7 @@ public String formatMotd(String motd, InetAddress ip) {
// FULL BAN
} else formattedMotd = formattedMotd.replace("%reason%", ban.banReason(playerName));
formattedMotd = PapiIntegration.replaceVariables(player, formattedMotd);
formattedMotd = HexResolver.parseHexString(formattedMotd);
return formattedMotd;
}

Expand Down
2 changes: 2 additions & 0 deletions src/cloud/bolte/serverlistmotd/motd/ClassicMotd.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.InetAddress;

import cloud.bolte.serverlistmotd.util.HexResolver;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.server.ServerListPingEvent;
Expand Down Expand Up @@ -50,6 +51,7 @@ public String formatMotd(String motd, InetAddress ip) {
} else {
formattedMotd = PapiIntegration.replaceVariables(null, formattedMotd);
}
formattedMotd = HexResolver.parseHexString(formattedMotd);
return formattedMotd;
}

Expand Down
2 changes: 2 additions & 0 deletions src/cloud/bolte/serverlistmotd/motd/MaxBansMotd.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.InetAddress;

import cloud.bolte.serverlistmotd.util.HexResolver;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
Expand Down Expand Up @@ -69,6 +70,7 @@ public String formatMotd(String motd, InetAddress ip) {
.replace("%expyear%", ban.banExpDateYear(playerName));
}
formattedMotd = PapiIntegration.replaceVariables(player, formattedMotd);
formattedMotd = HexResolver.parseHexString(formattedMotd);
return formattedMotd;
}

Expand Down
2 changes: 2 additions & 0 deletions src/cloud/bolte/serverlistmotd/motd/RandomMotd.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import java.util.Random;

import cloud.bolte.serverlistmotd.util.HexResolver;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.server.ServerListPingEvent;
Expand Down Expand Up @@ -56,6 +57,7 @@ public String formatMotd(String motd, InetAddress ip) {
} else {
formattedMotd = PapiIntegration.replaceVariables(null, formattedMotd);
}
formattedMotd = HexResolver.parseHexString(formattedMotd);
return formattedMotd;
}

Expand Down
3 changes: 2 additions & 1 deletion src/cloud/bolte/serverlistmotd/motd/RestrictedModeMotd.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.InetAddress;

import cloud.bolte.serverlistmotd.util.HexResolver;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
Expand Down Expand Up @@ -53,7 +54,7 @@ public String formatMotd(String motd, InetAddress ip) {
} else {
formattedMotd = PapiIntegration.replaceVariables(null, formattedMotd);
}

formattedMotd = HexResolver.parseHexString(formattedMotd);
return formattedMotd;
}

Expand Down
3 changes: 2 additions & 1 deletion src/cloud/bolte/serverlistmotd/motd/WhitelistMotd.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.InetAddress;

import cloud.bolte.serverlistmotd.util.HexResolver;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
Expand Down Expand Up @@ -51,7 +52,7 @@ public String formatMotd(String motd, InetAddress ip) {
} else {
formattedMotd = PapiIntegration.replaceVariables(null, formattedMotd);
}

formattedMotd = HexResolver.parseHexString(formattedMotd);
return formattedMotd;
}

Expand Down
80 changes: 80 additions & 0 deletions src/cloud/bolte/serverlistmotd/util/Gradient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package cloud.bolte.serverlistmotd.util;

import java.awt.Color;
import java.util.List;

/**
* Author: https://github.com/harry0198/HexiTextLib MIT License
*
* NOTICE FROM harry0198:
* -------------------------
* Gradient class
* Derived from:
* https://github.com/Rosewood-Development/RoseStacker/blob/master/Plugin/src/main/java/dev/rosewood/rosestacker/utils/HexUtils.java
* https://github.com/Oribuin/ChatEmojis/blob/master/src/main/java/xyz/oribuin/chatemojis/utils/HexUtils.java
*
* Unsure who original author is.
* @author unknown
*/
public class Gradient {

private final List<Color> colors;
private final int stepSize;
private int step, stepIndex;

public Gradient(List<Color> colors, int totalColors) {
if (colors.size() < 2)
throw new IllegalArgumentException("Please provide at least 2 colors, e.g. <g:#ED4264:#FFEDBC>");

if (totalColors < 1)
throw new IllegalArgumentException("Must have at least 1 total color");

this.colors = colors;
this.stepSize = totalColors / (colors.size() - 1);
this.step = this.stepIndex = 0;
}

/**
* @return the next color in the gradient
*/
public Color next() {

Color color;
if (this.stepIndex + 1 < this.colors.size()) {
Color start = this.colors.get(this.stepIndex);
Color end = this.colors.get(this.stepIndex + 1);
float interval = (float) this.step / this.stepSize;

color = getGradientInterval(start, end, interval);
} else {
color = this.colors.get(this.colors.size() - 1);
}

this.step += 1;
if (this.step >= this.stepSize) {
this.step = 0;
this.stepIndex++;
}

return color;
}

/**
* Gets a color along a linear gradient between two colors
*
* @param start The start color
* @param end The end color
* @param interval The interval to get, between 0 and 1 inclusively
* @return A Color at the interval between the start and end colors
*/
public static Color getGradientInterval(Color start, Color end, float interval) {
if (0 > interval || interval > 1)
throw new IllegalArgumentException("Interval must be between 0 and 1 inclusively.");

int r = (int) (end.getRed() * interval + start.getRed() * (1 - interval));
int g = (int) (end.getGreen() * interval + start.getGreen() * (1 - interval));
int b = (int) (end.getBlue() * interval + start.getBlue() * (1 - interval));

return new Color(r, g, b);
}
}
Loading

0 comments on commit ea84d5a

Please sign in to comment.