Skip to content

Commit

Permalink
Optimized a lot around the code, Introduced a new function Tab Custom…
Browse files Browse the repository at this point in the history
…ization it lets users have custom tab Header and Footer and Ping. added couple of checks around the code to ensure that if config.yml values are null that it doesn't proceed, this will avoid console spam.

I've used in.rs.milivojevic.KPManager.utils.PingUtil.java in order to get the ping values.

This code was taken from xDefcon's spigot-ping project.
Original code can be found at https://github.com/xDefcon/spigot-ping/blob/master/src/main/java/com/xdefcon/spigotping/utils/PingUtil.java
Original author: Luigi Martinelli (xDefcon)
License: GNU GENERAL PUBLIC LICENSE Version 3
This project uses the code under the terms of the license.
  • Loading branch information
Kame03 committed Jan 16, 2023
1 parent c554c86 commit 918e9cd
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>in.rs.milivojevic</groupId>
<artifactId>KPManager</artifactId>
<version>1.4-SNAPSHOT</version>
<version>1.5-SNAPSHOT</version>
<packaging>jar</packaging>

<name>KPManager</name>
Expand Down Expand Up @@ -78,7 +78,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.13.2-R0.1-SNAPSHOT</version>
<version>1.19.3-R0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/in/rs/milivojevic/KPManager/JoinWelcomer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.Objects;

Expand All @@ -27,9 +26,18 @@ public final class JoinWelcomer implements Listener {
public JoinWelcomer(Main plugin) {
this.plugin = plugin;
config = plugin.getConfig();
messagePlayer = config.getString("messagePlayer").replace('&', ChatColor.COLOR_CHAR);
messageEveryone = config.getString("messageEveryone").replace('&', ChatColor.COLOR_CHAR);
messageError = config.getString("messageError").replace('&', ChatColor.COLOR_CHAR);

if (config.getString("tabHeader") !=null) {
messagePlayer = config.getString("messagePlayer").replace('&', ChatColor.COLOR_CHAR);
}

if (config.getString("tabHeader") !=null) {
messageEveryone = config.getString("messageEveryone").replace('&', ChatColor.COLOR_CHAR);
}

if (config.getString("tabHeader") !=null) {
messageError = config.getString("messageError").replace('&', ChatColor.COLOR_CHAR);
}
}


Expand Down
10 changes: 5 additions & 5 deletions src/main/java/in/rs/milivojevic/KPManager/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package in.rs.milivojevic.KPManager;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scoreboard.Scoreboard;

public final class Main extends JavaPlugin implements Listener {

Expand All @@ -14,11 +14,11 @@ public void onEnable() {
this.saveDefaultConfig();
JoinWelcomer joinWelcomer = new JoinWelcomer(this);
getServer().getPluginManager().registerEvents(joinWelcomer, this);
}




Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
TabCustomizer tabCustomizer = new TabCustomizer(scoreboard,this);
getServer().getPluginManager().registerEvents(tabCustomizer, this);
}
@Override
public void onDisable() {
Bukkit.getServer().getPluginManager().disablePlugin(this);
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/in/rs/milivojevic/KPManager/TabCustomizer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package in.rs.milivojevic.KPManager;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
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.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import org.bukkit.scheduler.BukkitRunnable;
import in.rs.milivojevic.KPManager.utils.PingUtil;

public class TabCustomizer implements Listener {
private final Scoreboard scoreboard;
private FileConfiguration config;
private String tabHeader;
private String tabFooter;
private String pingInt;
private final Main plugin;
private boolean hasPingInt;
private boolean hasTabHeaderFooter;
public TabCustomizer(Scoreboard scoreboard, Main plugin) {
this.scoreboard = scoreboard;
this.plugin = plugin;
config = plugin.getConfig();

hasPingInt = config.contains("pingInt") && config.getString("pingInt") != null;

hasTabHeaderFooter = config.contains("tabHeader") && config.getString("tabHeader") != null && config.contains("tabFooter") && config.getString("tabFooter") != null;



if (config.getString("tabHeader") !=null){
tabHeader = config.getString("tabHeader").replace('&', ChatColor.COLOR_CHAR);
}

if (config.getString("tabFooter") !=null) {
tabFooter = config.getString("tabFooter").replace('&', ChatColor.COLOR_CHAR);
}

if (config.getString("pingInt") !=null) {
pingInt = config.getString("pingInt").replace('&', ChatColor.COLOR_CHAR);
}
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
String playerName = player.getName();
Team team = scoreboard.getTeam(playerName);
if (team == null) {
team = scoreboard.registerNewTeam(playerName);
}
team.addEntry(playerName);

BukkitRunnable task = new BukkitRunnable() {
@Override
public void run() {
int ping = PingUtil.getPing(player);
if (hasPingInt) {
player.setPlayerListName(playerName + ChatColor.translateAlternateColorCodes('&', pingInt.replace("{ping}", String.valueOf(ping))));
}
if (hasTabHeaderFooter) {
player.setPlayerListHeaderFooter(ChatColor.translateAlternateColorCodes('&', tabHeader.replace("{playerName}", player.getName())), ChatColor.translateAlternateColorCodes('&', tabFooter.replace("{playerName}", player.getName())));
}
}
};
task.runTaskTimer(plugin, 0, 20);
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
Team team = scoreboard.getTeam(player.getName());
if (team != null) {
team.removeEntry(player.getName());
}
}
}
27 changes: 27 additions & 0 deletions src/main/java/in/rs/milivojevic/KPManager/utils/PingUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
This code was taken from xDefcon's spigot-ping project.
Original code can be found at https://github.com/xDefcon/spigot-ping/blob/master/src/main/java/com/xdefcon/spigotping/utils/PingUtil.java
Original author: Luigi Martinelli (xDefcon)
License: GNU GENERAL PUBLIC LICENSE Version 3
This project uses the code under the terms of the license.
*/

package in.rs.milivojevic.KPManager.utils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class PingUtil {
public static int getPing(Player p) {
String v = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
if (!p.getClass().getName().equals("org.bukkit.craftbukkit." + v + ".entity.CraftPlayer")) { //compatibility with some plugins
p = Bukkit.getPlayer(p.getUniqueId()); //cast to org.bukkit.entity.Player
}
try {
int ping = p.getPing();
return ping;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
5 changes: 4 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
messagePlayer: '&aWelcome &b{playerName} &ato my server! &bEnjoy your stay!'
messageEveryone: '&a{playerName} joined from &b{country}.'
messageError: '&cWelcome, &6{playerName}, &cto the server! It seems you may have joined from localhost or there was an error with the API ip-api.com. Please check your internet connection and firewall settings.'
messageError: '&cWelcome, &6{playerName}, &cto the server! It seems you may have joined from localhost or there was an error with the API ip-api.com. Please check your internet connection and firewall settings.'
tabHeader: '&6Welcome {playerName} to our wonderful server! &6'
tabFooter: '&6Make the most of your stay, {playerName} &6'
pingInt: '&7[{ping}]'
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: KPManager
api-version: 1.19
version: 1.4
version: 1.5
main: in.rs.milivojevic.KPManager.Main
author: Kamey_
license: MIT
Expand Down

0 comments on commit 918e9cd

Please sign in to comment.