-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimized a lot around the code, Introduced a new function Tab Custom…
…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
Showing
7 changed files
with
132 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/java/in/rs/milivojevic/KPManager/TabCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
src/main/java/in/rs/milivojevic/KPManager/utils/PingUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}]' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters