Skip to content

Commit

Permalink
Merge pull request #46 from Andre601/feature/make-playersamples-generic
Browse files Browse the repository at this point in the history
Make generic method for Fake player creation
  • Loading branch information
Andre601 authored Apr 29, 2021
2 parents 17f60f9 + 4b0c893 commit 7f13695
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
import com.andre601.oneversionremake.core.interfaces.PluginCore;
import com.andre601.oneversionremake.core.interfaces.ProxyLogger;
import net.kyori.adventure.platform.bungeecord.BungeeAudiences;
import net.md_5.bungee.api.ServerPing;
import net.md_5.bungee.api.plugin.Plugin;
import org.bstats.bungeecord.Metrics;
import org.bstats.charts.DrilldownPie;
import org.bstats.charts.SimplePie;

import java.nio.file.Path;
import java.util.List;

public class BungeeCore extends Plugin implements PluginCore{

Expand Down Expand Up @@ -102,4 +104,9 @@ public CommandHandler getCommandHandler(){
public String getVersion(){
return core.getVersion();
}

public ServerPing.PlayerInfo[] getPlayers(List<String> lines, List<Integer> serverProtocols, int userProtocol, boolean majorOnly){
return core.getPlayers(ServerPing.PlayerInfo.class, lines, serverProtocols, userProtocol, majorOnly)
.toArray(new ServerPing.PlayerInfo[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import java.util.Comparator;
import java.util.List;
import java.util.UUID;

public class BungeePingListener implements Listener{

Expand Down Expand Up @@ -64,8 +63,11 @@ public void onProxyPing(ProxyPingEvent event){
List<String> motd = plugin.getConfigHandler().getStringList("Messages", "Motd");

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

if(!playerCount.isEmpty())
protocol.setName(Parser.toString(playerCount, serverProtocols, userProtocol, majorOnly));
Expand All @@ -89,13 +91,4 @@ public void onProxyPing(ProxyPingEvent event){
event.setResponse(ping);
}
}

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, majorOnly), UUID.randomUUID());
}

return samplePlayers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

public class OneVersionRemake{

Expand Down Expand Up @@ -99,6 +98,24 @@ 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){
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(
Parser.toString(line, serverProtocols, userProtocol, majorOnly), UUID.randomUUID()
));
}

return players;
}catch(NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ex){
return null;
}
}

private void start(){
loadVersion();
printBanner();
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.3.4</plugin.version>
<plugin.version>3.4.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 @@ -34,11 +34,13 @@
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.server.ServerPing;
import org.bstats.charts.DrilldownPie;
import org.bstats.velocity.Metrics;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;
import java.util.List;

public class VelocityCore implements PluginCore{

Expand Down Expand Up @@ -122,4 +124,9 @@ public String getVersion(){
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)
.toArray(new ServerPing.SamplePlayer[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import java.util.Comparator;
import java.util.List;
import java.util.UUID;

public class VelocityPingListener{

Expand Down Expand Up @@ -63,8 +62,11 @@ public void onProxyPing(ProxyPingEvent event){
if(!serverProtocols.contains(userProtocol)){
ServerPing.Builder builder = ping.asBuilder();

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

if(!playerCount.isEmpty()){
playerCount = Parser.toString(playerCount, serverProtocols, userProtocol, majorOnly);
Expand All @@ -82,13 +84,4 @@ public void onProxyPing(ProxyPingEvent event){
event.setPing(builder.build());
}
}

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, majorOnly), UUID.randomUUID());
}

return samplePlayers;
}
}

0 comments on commit 7f13695

Please sign in to comment.