Skip to content

Commit

Permalink
Add blacklist option
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre601 committed Mar 8, 2024
1 parent 024a5ed commit b51935c
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 74 deletions.
4 changes: 2 additions & 2 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ Using any older version will get your support request ignored.

### Platforms

As of writing this (29th of April, 2022) are only the following platforms officially supported:
As of writing this (29th of April 2022) are only the following platforms officially supported:

| Platform | Supported Version |
| ------------- | ------------------------------------ |
|---------------|--------------------------------------|
| BungeeCord | Any 1.18.x builds |
| Waterfall[^1] | Any 1.18.x builds |
| FlameCord[^2] | Any 1.18.x builds |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ public String getProxyVersion(){
return String.format("%s (Build #%s)", version[2], version[4]);
}

public ServerPing.PlayerInfo[] getPlayers(List<String> lines, List<Integer> serverProtocols, int userProtocol, boolean majorOnly){
return core.getPlayers(ServerPing.PlayerInfo.class, lines, serverProtocols, userProtocol, majorOnly)
public ServerPing.PlayerInfo[] getPlayers(List<String> lines, List<Integer> serverProtocols, int userProtocol, boolean majorOnly, boolean blacklist){
return core.getPlayers(ServerPing.PlayerInfo.class, lines, serverProtocols, userProtocol, majorOnly, blacklist)
.toArray(new ServerPing.PlayerInfo[0]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ public void onLogin(PreLoginEvent event){
List<String> kickMessage = plugin.getConfigHandler().getStringList(false, "Messages", "Kick");

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

int userProtocol = event.getConnection().getVersion();
if(serverProtocols.isEmpty())
return;

if(!serverProtocols.contains(userProtocol)){
if((blacklist && serverProtocols.contains(userProtocol)) || (!blacklist && !serverProtocols.contains(userProtocol))){
if(kickMessage.isEmpty())
kickMessage = Collections.singletonList("<red>This Server is running MC {version}! Please change your client version.");

event.setCancelReason(BungeeComponentSerializer.get().serialize(
plugin.getComponentParser().toComponent(kickMessage, serverProtocols, userProtocol, majorOnly)
plugin.getComponentParser().toComponent(kickMessage, serverProtocols, userProtocol, majorOnly, blacklist)
));
event.setCancelled(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,32 @@ public void onProxyPing(ProxyPingEvent event){
serverProtocols.sort(Comparator.reverseOrder());

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

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

if(!serverProtocols.contains(userProtocol)){
if((blacklist && serverProtocols.contains(userProtocol)) || (!blacklist && !serverProtocols.contains(userProtocol))){
if(!hoverMessage.isEmpty()){
ServerPing.PlayerInfo[] players = plugin.getPlayers(hoverMessage, serverProtocols, userProtocol, majorOnly);
ServerPing.PlayerInfo[] players = plugin.getPlayers(hoverMessage, serverProtocols, userProtocol, majorOnly, blacklist);
if(players != null)
ping.getPlayers().setSample(players);
}

if(!playerCount.isEmpty())
protocol.setName(plugin.getComponentParser().toString(playerCount, serverProtocols, userProtocol, majorOnly));
protocol.setName(plugin.getComponentParser().toString(playerCount, serverProtocols, userProtocol, majorOnly, blacklist));

if(!motd.isEmpty()){
TextComponent component = new TextComponent(BungeeComponentSerializer.get().serialize(
plugin.getComponentParser().toComponent(motd, serverProtocols, userProtocol, majorOnly)
plugin.getComponentParser().toComponent(motd, serverProtocols, userProtocol, majorOnly, blacklist)
));

ping.setDescriptionComponent(component);
}

ping.setFavicon(ping.getFaviconObject());

protocol.setProtocol(serverProtocols.get(0));
protocol.setProtocol(-1);

ping.setVersion(protocol);
event.setResponse(ping);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ public Map<String, Map<String, Integer>> getPieMap(){
return map;
}

public <T> List<T> getPlayers(Class<T> clazz, List<String> lines, List<Integer> serverProtocols, int userProtocol, boolean majorOnly){
public <T> List<T> getPlayers(Class<T> clazz, List<String> lines, List<Integer> serverProtocols, int userProtocol, boolean majorOnly, boolean blacklist){
try{
final List<T> players = new ArrayList<>(lines.size());
final Constructor<T> constructor = clazz.getDeclaredConstructor(String.class, UUID.class);
constructor.setAccessible(true);

for(String line : lines){
players.add(constructor.newInstance(
getComponentParser().toString(line, serverProtocols, userProtocol, majorOnly), UUID.randomUUID()
getComponentParser().toString(line, serverProtocols, userProtocol, majorOnly, blacklist), UUID.randomUUID()
));
}

Expand Down Expand Up @@ -184,7 +184,7 @@ private void enable(){
versionsSet = false;
}else{
getProxyLogger().info("Loaded the following Protocol Version(s):");
getProxyLogger().info(getProtocolVersionResolver().getVersions().getFriendlyNames(protocols, false));
getProxyLogger().info(getProtocolVersionResolver().getVersions().getFriendlyNames(protocols, false, false));

versionsSet = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ public Parser(OneVersionRemake core){
this.core = core;
}

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

public Component toComponent(String text){
return MiniMessage.miniMessage().deserialize(text);
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void handle(CmdSender sender, String[] args){
if(protocols.isEmpty()){
sender.sendMsg(NamedTextColor.RED, "None");
}else{
sender.sendMsg(NamedTextColor.GRAY, core.getProtocolVersionResolver().getVersions().getFriendlyNames(protocols, false));
sender.sendMsg(NamedTextColor.GRAY, core.getProtocolVersionResolver().getVersions().getFriendlyNames(protocols, false, false));
}

sender.sendMsg();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.google.gson.annotations.SerializedName;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -36,9 +37,18 @@ public int getFileVersion(){
return fileVersion;
}

public String getFriendlyNames(List<Integer> protocols, boolean majorOnly){
Stream<Integer> stream = protocols.stream()
.sorted(Comparator.reverseOrder());
public String getFriendlyNames(List<Integer> protocols, boolean majorOnly, boolean blacklist){
Stream<Integer> stream;

if(blacklist){
stream = Arrays.stream(protocolInfos)
.map(ProtocolInfo::getProtocol)
.filter(protocol -> !protocols.contains(protocol))
.sorted(Comparator.reverseOrder());
}else{
stream = protocols.stream()
.sorted(Comparator.reverseOrder());
}

if(majorOnly){
return stream.map(this::getMajor)
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ Protocol:
# | https://github.com/Andre601/OneVersionRemake/wiki/Config#majoronly |
# +--------------------------------------------------------------------------+
MajorOnly: false
# +--------------------------------------------------------------------------+
# | Should the list of protocols be treated as a Blacklist? |
# | If set to true, only protocol versions NOT in the list will be allowed. |
# | |
# | https://github.com/Andre601/OneVersionRemake/wiki/Config#blacklist |
# +--------------------------------------------------------------------------+
Blacklist: false

# +----------------------------------------------------------------------------+
# | Messages |
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<plugin.version>3.11.3</plugin.version>
<plugin.version>3.12.0</plugin.version>
<plugin.description>Only allow specific client versions on your Network.</plugin.description>

<maven.compiler.target>11</maven.compiler.target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ public ProxyServer getProxy(){
return proxy;
}

public ServerPing.SamplePlayer[] getPlayers(List<String> lines, List<Integer> serverProtocols, int userProtocol, boolean majorOnly){
return core.getPlayers(ServerPing.SamplePlayer.class, lines, serverProtocols, userProtocol, majorOnly)
public ServerPing.SamplePlayer[] getPlayers(List<String> lines, List<Integer> serverProtocols, int userProtocol, boolean majorOnly, boolean blacklist){
return core.getPlayers(ServerPing.SamplePlayer.class, lines, serverProtocols, userProtocol, majorOnly, blacklist)
.toArray(new ServerPing.SamplePlayer[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@ public void onPreLogin(PreLoginEvent event){
List<String> kickMessage = plugin.getConfigHandler().getStringList(false, "Messages", "Kick");

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

int userProtocol = event.getConnection().getProtocolVersion().getProtocol();
if(serverProtocols.isEmpty())
return;

if(!serverProtocols.contains(userProtocol)){
if((blacklist && serverProtocols.contains(userProtocol)) || (!blacklist && !serverProtocols.contains(userProtocol))){
if(kickMessage.isEmpty())
kickMessage = Collections.singletonList("&cThis Server is running MC {version}! Please change your client version.");

PreLoginEvent.PreLoginComponentResult result = PreLoginEvent.PreLoginComponentResult
.denied(plugin.getComponentParser().toComponent(kickMessage, serverProtocols, userProtocol, majorOnly));
.denied(plugin.getComponentParser().toComponent(kickMessage, serverProtocols, userProtocol, majorOnly, blacklist));

event.setResult(result);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,29 @@ public void onProxyPing(ProxyPingEvent event){
serverProtocols.sort(Comparator.reverseOrder());

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

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

if(!serverProtocols.contains(userProtocol)){
if((blacklist && serverProtocols.contains(userProtocol)) || (!blacklist && !serverProtocols.contains(userProtocol))){
ServerPing.Builder builder = ping.asBuilder();

if(!hoverMessage.isEmpty()){
ServerPing.SamplePlayer[] players = plugin.getPlayers(hoverMessage, serverProtocols, userProtocol, majorOnly);
ServerPing.SamplePlayer[] players = plugin.getPlayers(hoverMessage, serverProtocols, userProtocol, majorOnly, blacklist);
if(players != null)
builder.samplePlayers(players);
}

if(!playerCount.isEmpty()){
playerCount = plugin.getComponentParser().toString(playerCount, serverProtocols, userProtocol, majorOnly);
playerCount = plugin.getComponentParser().toString(playerCount, serverProtocols, userProtocol, majorOnly, blacklist);

builder.version(new ServerPing.Version(serverProtocols.get(0), playerCount));
builder.version(new ServerPing.Version(-1, playerCount));
}

if(!motd.isEmpty()){
builder.description(plugin.getComponentParser().toComponent(motd, serverProtocols, userProtocol, majorOnly));
builder.description(plugin.getComponentParser().toComponent(motd, serverProtocols, userProtocol, majorOnly, blacklist));
}

event.setPing(builder.build());
Expand Down
4 changes: 2 additions & 2 deletions versions.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"note": [
"This file is no longer updated and used by the latest OneVersionRemake version.",
"The latest version can be found at https://github.com/Andre601/andre601.github.io under",
"/site/oneversionremake/protocol_versions.json",
"The latest version can be found at https://codeberg.org/Andre601/website under",
"/docs/oneversionremake/protocol_versions.json",
"",
"This file will no longer receive updates."
],
Expand Down
16 changes: 13 additions & 3 deletions wiki/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This page tries to explain each option as detailed as possible!
- [Versions](#versions)
- [LogDenial](#logdenial)
- [MajorOnly](#majoronly)
- [Blacklist](#blacklist)
- [Messages](#messages)
- [Formatting](#formatting)
- [Basic Formatting](#basic-formatting)
Expand Down Expand Up @@ -136,6 +137,15 @@ When this option is set to true, will the `{version}` placeholder only display t
1.14.x, 1.15.x, 1.16.x
```

### Blacklist
> **Type**: `Boolean`
> **Default**:
> ```yaml
> Blacklist: false
> ```
When this option is set to true, will OneVersionRemake treat the [Protocol List](#versions) as a blacklist, meaning only players **not** having any of the listed versions may join.
## Messages
The `Messages` section allows you to define the different messages displayed when a player either joins or views a server with an unsupported version.
Expand All @@ -148,7 +158,7 @@ Depending on the config option are either only [basic](#basic-formatting) or [ad
##### Colors
| Option 1 | Option 2 |
| ----------------------------------------------- | ----------------------------------------------------------- |
|-------------------------------------------------|-------------------------------------------------------------|
| [`<aqua>`][colors] | [`<color:aqua>`][colors] |
| [`<black>`][colors] | [`<color:black>`][colors] |
| [`<blue>`][colors] | [`<color:blue>`][colors] |
Expand All @@ -169,7 +179,7 @@ Depending on the config option are either only [basic](#basic-formatting) or [ad
##### Formatting
| Name | Alias |
| ------------------------------- | --------------------- |
|---------------------------------|-----------------------|
| [`<bold>`][formatting] | [`<b>`][formatting] |
| [`<italic>`][formatting] | [`<em>`][formatting] |
| | [`<i>`][formatting] |
Expand All @@ -182,7 +192,7 @@ Depending on the config option are either only [basic](#basic-formatting) or [ad
Includes [Basic Formatting](#basic-formatting) but also some more advanced options:
| Option | Format |
| -------------- | ---------------------------------------------- |
|----------------|------------------------------------------------|
| RGB Colors | [`<#rrggbb>`][colors] |
| | [`<color:#rrggbb>`][colorsVerbose] |
| Gradients* | [`<gradient:#rrggbb:#rrggbb>`][gradient] |
Expand Down
Loading

0 comments on commit b51935c

Please sign in to comment.