Skip to content

Commit

Permalink
Version 3.1.0
Browse files Browse the repository at this point in the history
Add MajorOnly config option
  • Loading branch information
Andre601 committed Dec 13, 2020
1 parent 519e327 commit 913e2f2
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void execute(CommandSender commandSender, String[] args){
if(serverProtocols.isEmpty()){
sendMsg(commandSender, NamedTextColor.RED, "None");
}else{
sendMsg(commandSender, NamedTextColor.GRAY, ProtocolVersion.getFriendlyNames(serverProtocols));
sendMsg(commandSender, NamedTextColor.GRAY, ProtocolVersion.getFriendlyNames(serverProtocols, false));
}

sendMsg(commandSender);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void onLogin(PreLoginEvent event){
List<Integer> serverProtocols = plugin.getConfigHandler().getIntList("Protocol", "Versions");
List<String> kickMessage = plugin.getConfigHandler().getStringList("Messages", "Kick");

boolean majorOnly = plugin.getConfigHandler().getBoolean(false, "Protocol", "MajorOnly");
int userProtocol = event.getConnection().getVersion();
if(serverProtocols.isEmpty())
return;
Expand All @@ -52,7 +53,9 @@ public void onLogin(PreLoginEvent event){
if(kickMessage.isEmpty())
kickMessage = Collections.singletonList("&cThis Server is running MC {version}! Please change your client version.");

event.setCancelReason(BungeeComponentSerializer.get().serialize(Parser.toTextComponent(kickMessage, serverProtocols, userProtocol)));
event.setCancelReason(BungeeComponentSerializer.get().serialize(
Parser.toTextComponent(kickMessage, serverProtocols, userProtocol, majorOnly)
));
event.setCancelled(true);

if(plugin.getConfigHandler().getBoolean(true, "Protocol", "LogDenial")){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,25 @@ public void onProxyPing(ProxyPingEvent event){
return;

serverProtocols.sort(Comparator.reverseOrder());

boolean majorOnly = plugin.getConfigHandler().getBoolean(false, "Protocol", "MajorOnly");

String playerCount = plugin.getConfigHandler().getString("", "Messages", "PlayerCount");
List<String> motd = plugin.getConfigHandler().getStringList("Messages", "Motd");

if(!serverProtocols.contains(userProtocol)){
if(!hoverMessage.isEmpty())
ping.getPlayers().setSample(getSamplePlayers(hoverMessage, serverProtocols, userProtocol));
ping.getPlayers().setSample(getSamplePlayers(hoverMessage, serverProtocols, userProtocol, majorOnly));

if(!playerCount.isEmpty())
protocol.setName(Parser.toString(playerCount, serverProtocols, userProtocol));
protocol.setName(Parser.toString(playerCount, serverProtocols, userProtocol, majorOnly));

if(!motd.isEmpty()){
if(motd.size() > 2)
motd = motd.subList(0, 1);

TextComponent component = new TextComponent(BungeeComponentSerializer.get().serialize(
Parser.toTextComponent(motd, serverProtocols, userProtocol)
Parser.toTextComponent(motd, serverProtocols, userProtocol, majorOnly)
));

ping.setDescriptionComponent(component);
Expand All @@ -86,10 +88,10 @@ public void onProxyPing(ProxyPingEvent event){
}
}

private ServerPing.PlayerInfo[] getSamplePlayers(List<String> lines, List<Integer> serverProtocols, int userProtocol){
private ServerPing.PlayerInfo[] getSamplePlayers(List<String> lines, List<Integer> serverProtocols, int userProtocol, boolean majorOnly){
ServerPing.PlayerInfo[] samplePlayers = new ServerPing.PlayerInfo[lines.size()];
for(int i = 0; i < samplePlayers.length; i++){
samplePlayers[i] = new ServerPing.PlayerInfo(Parser.toString(lines.get(i), serverProtocols, userProtocol), UUID.randomUUID());
samplePlayers[i] = new ServerPing.PlayerInfo(Parser.toString(lines.get(i), serverProtocols, userProtocol, majorOnly), UUID.randomUUID());
}

return samplePlayers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,7 @@ private void start(){

ProxyLogger logger = pluginCore.getProxyLogger();

logger.info("");
logger.info(" ____ _ ______");
logger.info(" / __ \\ | / / __ \\");
logger.info(" / / / / | / / /_/ /");
logger.info("/ /_/ /| |/ / _, _/");
logger.info("\\____/ |___/_/ |_|");
logger.info("");
logger.info("OneVersionRemake v" + (getVersion() == null ? "UNKNOWN" : getVersion()));
logger.info("Platform: " + pluginCore.getProxyPlatform().getName());
logger.info("");
printBanner(logger);

if(configHandler.loadConfig()){
logger.info("Loaded config.yml!");
Expand All @@ -85,20 +76,10 @@ private void start(){

List<Integer> protocols = configHandler.getIntList("Protocol", "Versions");
if(protocols.isEmpty()){
logger.warn("================================================================================");
logger.warn("WARNING!");
logger.warn("The config option 'Versions' doesn't contain any protocol numbers!");
logger.warn("Please edit the config to include valid protocol numbers or OneVersionRemake");
logger.warn("won't work as expected.");
logger.warn("");
logger.warn("You may find a list of supported protocol versions here:");
logger.warn("https://github.com/Andre601/OneVersionRemake/wiki/Supported-Protocols");
logger.warn("");
logger.warn("OneVersionRemake won't handle joining Players to prevent any possible issues.");
logger.warn("================================================================================");
printWarning(logger);
}else{
logger.info("Loaded the following Protocol Version(s):");
logger.info(ProtocolVersion.getFriendlyNames(protocols));
logger.info(ProtocolVersion.getFriendlyNames(protocols, false));
}

logger.info("Loading command /ovr...");
Expand All @@ -112,6 +93,33 @@ private void start(){
logger.info("OneVersionRemake is ready!");
}

private void printBanner(ProxyLogger logger){
logger.info("");
logger.info(" ____ _ ______");
logger.info(" / __ \\ | / / __ \\");
logger.info(" / / / / | / / /_/ /");
logger.info("/ /_/ /| |/ / _, _/");
logger.info("\\____/ |___/_/ |_|");
logger.info("");
logger.info("OneVersionRemake v" + (getVersion() == null ? "UNKNOWN" : getVersion()));
logger.info("Platform: " + pluginCore.getProxyPlatform().getName());
logger.info("");
}

private void printWarning(ProxyLogger logger){
logger.warn("================================================================================");
logger.warn("WARNING!");
logger.warn("The config option 'Versions' doesn't contain any protocol numbers!");
logger.warn("Please edit the config to include valid protocol numbers or OneVersionRemake");
logger.warn("won't work as expected.");
logger.warn("");
logger.warn("You may find a list of supported protocol versions here:");
logger.warn("https://github.com/Andre601/OneVersionRemake/wiki/Supported-Protocols");
logger.warn("");
logger.warn("OneVersionRemake won't handle joining Players to prevent any possible issues.");
logger.warn("================================================================================");
}

private void loadVersion(){
try(InputStream is = getClass().getResourceAsStream("/core.properties")){
Properties properties = new Properties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@

public class Parser{

public static Component toTextComponent(List<String> lines, List<Integer> serverProtocols, int userProtocol){
return MiniMessage.get().parse(parse(String.join("\n", lines), serverProtocols, userProtocol));
public static Component toTextComponent(List<String> lines, List<Integer> serverProtocols, int userProtocol, boolean majorOnly){
return MiniMessage.get().parse(parse(String.join("\n", lines), serverProtocols, userProtocol, majorOnly));
}

public static String toString(String text, List<Integer> serverProtocols, int userProtocol){
Component component = MiniMessage.get().parse(parse(text, serverProtocols, userProtocol));
public static String toString(String text, List<Integer> serverProtocols, int userProtocol, boolean majorOnly){
Component component = MiniMessage.get().parse(parse(text, serverProtocols, userProtocol, majorOnly));
return LegacyComponentSerializer.legacySection().serialize(component);
}

private static String parse(String text, List<Integer> serverProtocols, int userProtocol){
return text.replace("{version}", ProtocolVersion.getFriendlyNames(serverProtocols))
private static String parse(String text, List<Integer> serverProtocols, int userProtocol, boolean majorOnly){
return text.replace("{version}", ProtocolVersion.getFriendlyNames(serverProtocols, majorOnly))
.replace("{userVersion}", ProtocolVersion.getFriendlyName(userProtocol));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,45 @@
import java.util.stream.Collectors;

public enum ProtocolVersion{
MC_1_16_4(754, "1.16.4"),
MC_1_16_3(753, "1.16.3"),
MC_1_16_2(751, "1.16.2"),
MC_1_16_1(736, "1.16.1"),
MC_1_16 (735, "1.16"),
MC_1_15_2(578, "1.15.2"),
MC_1_15_1(575, "1.15.1"),
MC_1_15 (573, "1.15"),
MC_1_14_4(498, "1.14.4"),
MC_1_14_3(490, "1.14.3"),
MC_1_14_2(485, "1.14.2"),
MC_1_14_1(480, "1.14.1"),
MC_1_14 (477, "1.14"),
MC_1_13_2(404, "1.13.2"),
MC_1_13_1(401, "1.13.1"),
MC_1_13 (393, "1.13"),
MC_1_12_2(340, "1.12.2"),
MC_1_12_1(338, "1.12.1"),
MC_1_12 (335, "1.12"),
MC_1_11_2(316, "1.11.2"),
MC_1_11 (315, "1.11"),
MC_1_10_2(210, "1.10.2"),
MC_1_9_4 (110, "1.9.4"),
MC_1_9_2 (109, "1.9.2"),
MC_1_9_1 (108, "1.9.1"),
MC_1_9 (107, "1.9"),
MC_1_8_9 (47, "1.8.9"),
MC_1_16_4(754, "1.16.4", "1.16.x"),
MC_1_16_3(753, "1.16.3", "1.16.x"),
MC_1_16_2(751, "1.16.2", "1.16.x"),
MC_1_16_1(736, "1.16.1", "1.16.x"),
MC_1_16 (735, "1.16", "1.16.x"),
MC_1_15_2(578, "1.15.2", "1.15.x"),
MC_1_15_1(575, "1.15.1", "1.15.x"),
MC_1_15 (573, "1.15", "1.15.x"),
MC_1_14_4(498, "1.14.4", "1.14.x"),
MC_1_14_3(490, "1.14.3", "1.14.x"),
MC_1_14_2(485, "1.14.2", "1.14.x"),
MC_1_14_1(480, "1.14.1", "1.14.x"),
MC_1_14 (477, "1.14", "1.14.x"),
MC_1_13_2(404, "1.13.2", "1.13.x"),
MC_1_13_1(401, "1.13.1", "1.13.x"),
MC_1_13 (393, "1.13", "1.13.x"),
MC_1_12_2(340, "1.12.2", "1.12.x"),
MC_1_12_1(338, "1.12.1", "1.12.x"),
MC_1_12 (335, "1.12", "1.12.x"),
MC_1_11_2(316, "1.11.2", "1.11.x"),
MC_1_11 (315, "1.11", "1.11.x"),
MC_1_10_2(210, "1.10.2", "1.10.2"),
MC_1_9_4 (110, "1.9.4", "1.9.x"),
MC_1_9_2 (109, "1.9.2", "1.9.x"),
MC_1_9_1 (108, "1.9.1", "1.9.x"),
MC_1_9 (107, "1.9", "1.9.x"),
MC_1_8_9 (47, "1.8.9", "1.8.9"),

// In case none of the above versions match
UNKNOWN (0, "?");
UNKNOWN (0, "?", "?");

private final int protocol;
private final String friendlyName;
private final String major;

ProtocolVersion(int protocol, String friendlyName){
ProtocolVersion(int protocol, String friendlyName, String major){
this.protocol = protocol;
this.friendlyName = friendlyName;
this.major = major;
}

public static String getFriendlyName(int protocol){
Expand All @@ -71,18 +73,39 @@ public static String getFriendlyName(int protocol){
return UNKNOWN.getFriendlyName();
}

public static String getFriendlyNames(List<Integer> protocols){
public static String getFriendlyNames(List<Integer> protocols, boolean majorOnly){
if(majorOnly){
return protocols.stream()
.sorted(Comparator.reverseOrder())
.map(ProtocolVersion::getMajor)
.distinct()
.collect(Collectors.joining(", "));
}

return protocols.stream()
.sorted(Comparator.reverseOrder())
.map(ProtocolVersion::getFriendlyName)
.collect(Collectors.joining(", "));
}

private static String getMajor(int protocol){
for(ProtocolVersion version : values()){
if(version.getProtocol() == protocol)
return version.getMajor();
}

return UNKNOWN.getMajor();
}

private int getProtocol(){
return protocol;
}

private String getFriendlyName(){
return friendlyName;
}

private String getMajor(){
return major;
}
}
7 changes: 7 additions & 0 deletions core/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ Protocol:
# Should OneVersionRemake log any denied Login attempts in the console?
#
LogDenial: true
#
# Should OneVersionRemake only show major MC versions (1.16.x, 1.15.x, ...)
# for the {version} placeholder?
# This is useful for when you have a lot of Versions defined but don't want
# the placeholder from becoming too long.
#
MajorOnly: false

#
# +---------------------------------------------------------------------+ #
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<plugin.version>3.0.1</plugin.version>
<plugin.version>3.1.0</plugin.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void execute(Invocation invocation){
if(serverProtocols.isEmpty()){
sendMsg(commandSource, NamedTextColor.RED, "None");
}else{
sendMsg(commandSource, NamedTextColor.GRAY, ProtocolVersion.getFriendlyNames(serverProtocols));
sendMsg(commandSource, NamedTextColor.GRAY, ProtocolVersion.getFriendlyNames(serverProtocols, false));
}

sendMsg(commandSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public VelocityLoginListener(VelocityCore plugin){
public void onPreLogin(PreLoginEvent event){
List<Integer> serverProtocols = plugin.getConfigHandler().getIntList("Protocol", "Versions");
List<String> kickMessage = plugin.getConfigHandler().getStringList("Messages", "Kick");


boolean majorOnly = plugin.getConfigHandler().getBoolean(false, "Protocol", "MajorOnly");
int userProtocol = event.getConnection().getProtocolVersion().getProtocol();
if(serverProtocols.isEmpty())
return;
Expand All @@ -51,7 +52,7 @@ public void onPreLogin(PreLoginEvent event){
kickMessage = Collections.singletonList("&cThis Server is running MC {version}! Please change your client version.");

PreLoginEvent.PreLoginComponentResult result = PreLoginEvent.PreLoginComponentResult
.denied(Parser.toTextComponent(kickMessage, serverProtocols, userProtocol));
.denied(Parser.toTextComponent(kickMessage, serverProtocols, userProtocol, majorOnly));

event.setResult(result);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public void onProxyPing(ProxyPingEvent event){
return;

serverProtocols.sort(Comparator.reverseOrder());

boolean majorOnly = plugin.getConfigHandler().getBoolean(false, "Protocol", "MajorOnly");

String playerCount = plugin.getConfigHandler().getString("", "Messages", "PlayerCount");
List<String> motd = plugin.getConfigHandler().getStringList("Messages", "Motd");
Expand All @@ -62,10 +64,10 @@ public void onProxyPing(ProxyPingEvent event){
ServerPing.Builder builder = ServerPing.builder();

if(!hoverMessage.isEmpty())
builder.samplePlayers(getSamplePlayers(hoverMessage, serverProtocols, userProtocol));
builder.samplePlayers(getSamplePlayers(hoverMessage, serverProtocols, userProtocol, majorOnly));

if(!playerCount.isEmpty()){
playerCount = Parser.toString(playerCount, serverProtocols, userProtocol);
playerCount = Parser.toString(playerCount, serverProtocols, userProtocol, majorOnly);

builder.version(new ServerPing.Version(serverProtocols.get(0), playerCount));
}
Expand All @@ -74,7 +76,7 @@ public void onProxyPing(ProxyPingEvent event){
if(motd.size() > 2)
motd = motd.subList(0, 1);

builder.description(Parser.toTextComponent(motd, serverProtocols, userProtocol));
builder.description(Parser.toTextComponent(motd, serverProtocols, userProtocol, majorOnly));
}else{
builder.description(ping.getDescriptionComponent());
}
Expand All @@ -83,10 +85,10 @@ public void onProxyPing(ProxyPingEvent event){
}
}

private ServerPing.SamplePlayer[] getSamplePlayers(List<String> lines, List<Integer> serverProtocols, int userProtocol){
private ServerPing.SamplePlayer[] getSamplePlayers(List<String> lines, List<Integer> serverProtocols, int userProtocol, boolean majorOnly){
ServerPing.SamplePlayer[] samplePlayers = new ServerPing.SamplePlayer[lines.size()];
for(int i = 0; i < samplePlayers.length; i++){
samplePlayers[i] = new ServerPing.SamplePlayer(Parser.toString(lines.get(i), serverProtocols, userProtocol), UUID.randomUUID());
samplePlayers[i] = new ServerPing.SamplePlayer(Parser.toString(lines.get(i), serverProtocols, userProtocol, majorOnly), UUID.randomUUID());
}

return samplePlayers;
Expand Down

0 comments on commit 913e2f2

Please sign in to comment.