WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
This repository was archived by the owner on Apr 9, 2024. It is now read-only.

Commit 99509b5

Browse files
authored
Merge pull request #16 from Andre601/feature/improve-fakeplayers-creation
Improve handling of fake players
2 parents 5cb1245 + 4539fe4 commit 99509b5

File tree

19 files changed

+381
-271
lines changed

19 files changed

+381
-271
lines changed

bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/BungeeCordCore.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,23 @@
3030
import ch.andre601.advancedserverlist.bungeecord.events.PingEvent;
3131
import ch.andre601.advancedserverlist.bungeecord.logging.BungeeLogger;
3232
import ch.andre601.advancedserverlist.core.AdvancedServerList;
33-
import ch.andre601.advancedserverlist.core.interfaces.PluginCore;
33+
import ch.andre601.advancedserverlist.core.interfaces.core.ProxyCore;
3434
import ch.andre601.advancedserverlist.core.interfaces.PluginLogger;
35+
import ch.andre601.advancedserverlist.core.parsing.ComponentParser;
3536
import ch.andre601.advancedserverlist.core.profiles.favicon.FaviconHandler;
37+
import ch.andre601.advancedserverlist.core.profiles.replacer.placeholders.Placeholders;
3638
import net.md_5.bungee.api.Favicon;
39+
import net.md_5.bungee.api.ServerPing;
3740
import net.md_5.bungee.api.plugin.Plugin;
3841
import org.bstats.bungeecord.Metrics;
3942
import org.bstats.charts.SimplePie;
4043

4144
import java.nio.file.Path;
45+
import java.util.ArrayList;
46+
import java.util.List;
47+
import java.util.UUID;
4248

43-
public class BungeeCordCore extends Plugin implements PluginCore<Favicon>{
49+
public class BungeeCordCore extends Plugin implements ProxyCore<Favicon, ServerPing.PlayerInfo>{
4450

4551
private AdvancedServerList core;
4652
private FaviconHandler<Favicon> faviconHandler = null;
@@ -86,7 +92,7 @@ public AdvancedServerList getCore(){
8692
}
8793

8894
@Override
89-
public Path getPath(){
95+
public Path getFolderPath(){
9096
return getDataFolder().toPath();
9197
}
9298

@@ -112,4 +118,20 @@ public String getPlatformName(){
112118
public String getPlatformVersion(){
113119
return getProxy().getVersion();
114120
}
121+
122+
@Override
123+
public List<ServerPing.PlayerInfo> createPlayers(List<String> lines, Placeholders... placeholders){
124+
List<ServerPing.PlayerInfo> players = new ArrayList<>(lines.size());
125+
126+
for(String line : lines){
127+
String parsed = ComponentParser.text(line)
128+
.replacements(placeholders[0])
129+
.replacements(placeholders[1])
130+
.toString();
131+
132+
players.add(new ServerPing.PlayerInfo(parsed, UUID.randomUUID()));
133+
}
134+
135+
return players;
136+
}
115137
}

bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/events/PingEvent.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import ch.andre601.advancedserverlist.bungeecord.BungeeCordCore;
2929
import ch.andre601.advancedserverlist.bungeecord.BungeePlayer;
30-
import ch.andre601.advancedserverlist.core.AdvancedServerList;
3130
import ch.andre601.advancedserverlist.core.parsing.ComponentParser;
3231
import ch.andre601.advancedserverlist.core.profiles.ProfileManager;
3332
import ch.andre601.advancedserverlist.core.profiles.ServerListProfile;
@@ -80,8 +79,8 @@ public void onProxyPing(ProxyPingEvent event){
8079
if(profile == null)
8180
return;
8281

83-
if(profile.getXMore() >= 0){
84-
max = online + profile.getXMore();
82+
if(profile.isExtraPlayersEnabled()){
83+
max = online + profile.getExtraPlayers();
8584
ping.getPlayers().setMax(max);
8685
}
8786

@@ -100,15 +99,9 @@ public void onProxyPing(ProxyPingEvent event){
10099

101100
if(profile.shouldHidePlayers()){
102101
ping.setPlayers(null);
103-
104-
ping.setFavicon(ping.getFaviconObject());
105-
ping.setVersion(protocol);
106-
107-
event.setResponse(ping);
108-
return;
109102
}
110103

111-
if(!profile.getPlayerCount().isEmpty()){
104+
if(!profile.getPlayerCount().isEmpty() && !profile.shouldHidePlayers()){
112105
protocol.setName(ComponentParser.text(profile.getPlayerCount())
113106
.replacements(playerPlaceholders)
114107
.replacements(serverPlaceholders)
@@ -117,13 +110,8 @@ public void onProxyPing(ProxyPingEvent event){
117110
protocol.setProtocol(-1);
118111
}
119112

120-
if(!profile.getPlayers().isEmpty()){
121-
String players = ComponentParser.list(profile.getPlayers())
122-
.replacements(playerPlaceholders)
123-
.replacements(serverPlaceholders)
124-
.toString();
125-
126-
ServerPing.PlayerInfo[] playerInfos = AdvancedServerList.getPlayers(ServerPing.PlayerInfo.class, players)
113+
if(!profile.getPlayers().isEmpty() && !profile.shouldHidePlayers()){
114+
ServerPing.PlayerInfo[] playerInfos = plugin.createPlayers(profile.getPlayers(), playerPlaceholders, serverPlaceholders)
127115
.toArray(new ServerPing.PlayerInfo[0]);
128116

129117
if(playerInfos.length > 0)
@@ -137,6 +125,7 @@ public void onProxyPing(ProxyPingEvent event){
137125
try{
138126
return Favicon.create(image);
139127
}catch(Exception ex){
128+
plugin.getPluginLogger().warn("Unable to create Favicon. %s", ex.getMessage());
140129
return null;
141130
}
142131
});

core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java

Lines changed: 47 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,24 @@
2727

2828
import ch.andre601.advancedserverlist.core.commands.CommandHandler;
2929
import ch.andre601.advancedserverlist.core.file.FileHandler;
30-
import ch.andre601.advancedserverlist.core.interfaces.PluginCore;
31-
import ch.andre601.advancedserverlist.core.interfaces.PluginLogger;
30+
import ch.andre601.advancedserverlist.core.interfaces.core.PluginCore;
3231
import ch.andre601.advancedserverlist.core.profiles.players.PlayerHandler;
3332

3433
import java.io.IOException;
3534
import java.io.InputStream;
36-
import java.lang.reflect.Constructor;
37-
import java.lang.reflect.InvocationTargetException;
3835
import java.nio.file.Path;
3936
import java.util.*;
4037

4138
public class AdvancedServerList{
4239

43-
private final PluginCore plugin;
40+
private final PluginCore<?> plugin;
4441
private final FileHandler fileHandler;
4542
private final CommandHandler commandHandler;
4643
private final PlayerHandler playerHandler;
4744

4845
private String version;
4946

50-
public AdvancedServerList(PluginCore plugin){
47+
public AdvancedServerList(PluginCore<?> plugin){
5148
this.plugin = plugin;
5249
this.fileHandler = new FileHandler(this);
5350
this.commandHandler = new CommandHandler(this);
@@ -56,41 +53,8 @@ public AdvancedServerList(PluginCore plugin){
5653
load();
5754
}
5855

59-
public static <T> List<T> getPlayers(Class<T> clazz, String text){
60-
try{
61-
String[] lines = text.split("\n");
62-
63-
final List<T> players = new ArrayList<>(lines.length);
64-
final Constructor<T> constructor = clazz.getDeclaredConstructor(String.class, UUID.class);
65-
66-
constructor.setAccessible(true);
67-
68-
for(String line : lines){
69-
players.add(constructor.newInstance(line, UUID.randomUUID()));
70-
}
71-
72-
return players;
73-
}catch(NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ex){
74-
return Collections.emptyList();
75-
}
76-
}
77-
78-
public void disable(){
79-
getPluginLogger().info("Saving cache.data file...");
80-
getPlayerHandler().save();
81-
getPluginLogger().info("AdvancedServerList disabled!");
82-
}
83-
84-
public void clearFaviconCache(){
85-
plugin.clearFaviconCache();
86-
}
87-
88-
public PluginLogger getPluginLogger(){
89-
return plugin.getPluginLogger();
90-
}
91-
92-
public Path getPath(){
93-
return plugin.getPath();
56+
public PluginCore<?> getPlugin(){
57+
return plugin;
9458
}
9559

9660
public FileHandler getFileHandler(){
@@ -109,58 +73,69 @@ public String getVersion(){
10973
return version;
11074
}
11175

76+
public void disable(){
77+
getPlugin().getPluginLogger().info("Saving cache.data file...");
78+
getPlayerHandler().save();
79+
getPlugin().getPluginLogger().info("AdvancedServerList disabled!");
80+
}
81+
82+
public void clearFaviconCache(){
83+
plugin.clearFaviconCache();
84+
}
85+
11286
private void load(){
11387
printBanner();
11488
resolveVersion();
115-
116-
getPluginLogger().info("Starting AdvancedServerList v%s...", version);
117-
118-
getPluginLogger().info("Platform: " + plugin.getPlatformName() + " " + plugin.getPlatformVersion());
89+
90+
getPlugin().getPluginLogger().info("Starting AdvancedServerList v%s...", version);
91+
92+
getPlugin().getPluginLogger().info("Platform: " + plugin.getPlatformName() + " " + plugin.getPlatformVersion());
11993

12094
if(getFileHandler().loadConfig()){
121-
getPluginLogger().info("Successfully loaded config.yml!");
95+
getPlugin().getPluginLogger().info("Successfully loaded config.yml!");
12296
}else{
123-
getPluginLogger().warn("Unable to load config.yml! Check previous lines for errors.");
97+
getPlugin().getPluginLogger().warn("Unable to load config.yml! Check previous lines for errors.");
12498
return;
12599
}
126100

127101
if(getFileHandler().loadProfiles()){
128-
getPluginLogger().info("Successfully loaded " + getFileHandler().getProfiles().size() + " profiles!");
102+
getPlugin().getPluginLogger().info("Successfully loaded " + getFileHandler().getProfiles().size() + " profiles!");
129103
}else{
130-
getPluginLogger().warn("Unable to load profiles! Check previous lines for errors.");
104+
getPlugin().getPluginLogger().warn("Unable to load profiles! Check previous lines for errors.");
131105
return;
132106
}
133107

134-
if(!getPath().resolve("favicons").toFile().exists() && getPath().resolve("favicons").toFile().mkdirs())
135-
getPluginLogger().info("Successfully created favicons folder.");
136-
137-
getPluginLogger().info("Loading Commands...");
108+
Path folder = getPlugin().getFolderPath().resolve("favicons");
109+
if(!folder.toFile().exists() && folder.toFile().mkdirs())
110+
getPlugin().getPluginLogger().info("Successfully created favicons folder.");
111+
112+
getPlugin().getPluginLogger().info("Loading Commands...");
138113
plugin.loadCommands();
139-
getPluginLogger().info("Commands loaded!");
140-
141-
getPluginLogger().info("Loading events...");
114+
getPlugin().getPluginLogger().info("Commands loaded!");
115+
116+
getPlugin().getPluginLogger().info("Loading events...");
142117
plugin.loadEvents();
143-
getPluginLogger().info("Events loaded!");
144-
145-
getPluginLogger().info("Loading cache.data...");
118+
getPlugin().getPluginLogger().info("Events loaded!");
119+
120+
getPlugin().getPluginLogger().info("Loading cache.data...");
146121
getPlayerHandler().load();
147-
148-
getPluginLogger().info("Loading bStats metrics. Disable it in the global config under /plugins/bstats/");
122+
123+
getPlugin().getPluginLogger().info("Loading bStats metrics. Disable it in the global config under /plugins/bstats/");
149124
plugin.loadMetrics();
150-
getPluginLogger().info("Metrics loaded!");
151-
152-
getPluginLogger().info("AdvancedServerList is ready!");
125+
getPlugin().getPluginLogger().info("Metrics loaded!");
126+
127+
getPlugin().getPluginLogger().info("AdvancedServerList is ready!");
153128
}
154129

155130
private void printBanner(){
156-
getPluginLogger().info("");
157-
getPluginLogger().info(" _____ _");
158-
getPluginLogger().info(" /\\ / ____| |");
159-
getPluginLogger().info(" / \\ | (___ | |");
160-
getPluginLogger().info(" / /\\ \\ \\___ \\| |");
161-
getPluginLogger().info(" / ____ \\ ____) | |____");
162-
getPluginLogger().info("/_/ \\_\\_____/|______|");
163-
getPluginLogger().info("");
131+
getPlugin().getPluginLogger().info("");
132+
getPlugin().getPluginLogger().info(" _____ _");
133+
getPlugin().getPluginLogger().info(" /\\ / ____| |");
134+
getPlugin().getPluginLogger().info(" / \\ | (___ | |");
135+
getPlugin().getPluginLogger().info(" / /\\ \\ \\___ \\| |");
136+
getPlugin().getPluginLogger().info(" / ____ \\ ____) | |____");
137+
getPlugin().getPluginLogger().info("/_/ \\_\\_____/|______|");
138+
getPlugin().getPluginLogger().info("");
164139
}
165140

166141
private void resolveVersion(){

core/src/main/java/ch/andre601/advancedserverlist/core/file/FileHandler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ public class FileHandler{
5252

5353
private ConfigurationNode node = null;
5454

55-
public FileHandler(AdvancedServerList plugin){
56-
this.plugin = plugin;
57-
this.logger = plugin.getPluginLogger();
55+
public FileHandler(AdvancedServerList core){
56+
this.plugin = core;
57+
this.logger = core.getPlugin().getPluginLogger();
5858

59-
this.config = plugin.getPath().resolve("config.yml");
60-
this.profilesFolder = plugin.getPath().resolve("profiles");
59+
this.config = core.getPlugin().getFolderPath().resolve("config.yml");
60+
this.profilesFolder = core.getPlugin().getFolderPath().resolve("profiles");
6161
}
6262

6363
public List<ServerListProfile> getProfiles(){

core/src/main/java/ch/andre601/advancedserverlist/core/interfaces/PluginCore.java renamed to core/src/main/java/ch/andre601/advancedserverlist/core/interfaces/core/PluginCore.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
*
2424
*/
2525

26-
package ch.andre601.advancedserverlist.core.interfaces;
26+
package ch.andre601.advancedserverlist.core.interfaces.core;
2727

2828
import ch.andre601.advancedserverlist.core.AdvancedServerList;
29+
import ch.andre601.advancedserverlist.core.interfaces.PluginLogger;
2930
import ch.andre601.advancedserverlist.core.profiles.favicon.FaviconHandler;
3031

3132
import java.nio.file.Path;
3233

33-
public interface PluginCore<T>{
34+
public interface PluginCore<F>{
3435

3536
void loadCommands();
3637

@@ -42,11 +43,11 @@ public interface PluginCore<T>{
4243

4344
AdvancedServerList getCore();
4445

45-
Path getPath();
46+
Path getFolderPath();
4647

4748
PluginLogger getPluginLogger();
4849

49-
FaviconHandler<T> getFaviconHandler();
50+
FaviconHandler<F> getFaviconHandler();
5051

5152
String getPlatformName();
5253

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2022 Andre_601
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
*/
25+
26+
package ch.andre601.advancedserverlist.core.interfaces.core;
27+
28+
import ch.andre601.advancedserverlist.core.profiles.replacer.placeholders.Placeholders;
29+
30+
import java.util.List;
31+
32+
public interface ProxyCore<F, P> extends PluginCore<F>{
33+
List<P> createPlayers(List<String> lines, Placeholders... placeholders);
34+
}

0 commit comments

Comments
 (0)