Skip to content

Commit

Permalink
Better icon fix + fix broken default server icon
Browse files Browse the repository at this point in the history
Icon fix before would create a lot of Timer threads per user, this new way assigns one thread per user connection to re-use.
Commit also contains other various code cleanup.
  • Loading branch information
Pugmatt committed May 7, 2023
1 parent 585fc38 commit 1e4ef82
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public class BCPlayer {

private int serverLimit;

private Data data;

private String uuid;

private LocalTime lastAction;
Expand Down Expand Up @@ -61,9 +59,8 @@ public class BCPlayer {
}


public BCPlayer(String uuid, Data data, BedrockServerSession session, List<String> serverList, int serverLimit) {
public BCPlayer(String uuid, BedrockServerSession session, List<String> serverList, int serverLimit) {
this.uuid = uuid;
this.data = data;
this.session = session;
this.serverList = serverList;
this.serverLimit = serverLimit;
Expand All @@ -87,7 +84,7 @@ public List<String> getServerList() {
}

public void setServerList(List<String> serverList) {
data.setValueString("servers", UIComponents.serversToFormData(serverList), serverList, uuid);
BedrockConnect.data.setValueString("servers", UIComponents.serversToFormData(serverList), serverList, uuid);
this.serverList = serverList;
}

Expand Down Expand Up @@ -118,7 +115,7 @@ public int getServerLimit() {
}

public void setServerLimit(int serverLimit) {
data.setValueInt("serverLimit", serverLimit, uuid);
BedrockConnect.data.setValueInt("serverLimit", serverLimit, uuid);
this.serverLimit = serverLimit;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
import main.com.pyratron.pugmatt.bedrockconnect.listeners.PacketHandler;
Expand All @@ -25,7 +26,6 @@
public class Server {

private final NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
public Channel server;
public BedrockPong pong;

public static final ObjectMapper JSON_MAPPER = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Expand Down Expand Up @@ -73,7 +73,7 @@ public Server(String bindIp, String port) {
pong.protocolVersion(BedrockProtocol.DEFAULT_BEDROCK_CODEC.getProtocolVersion());
pong.version(BedrockProtocol.DEFAULT_BEDROCK_CODEC.getMinecraftVersion());

server = new ServerBootstrap()
new ServerBootstrap()
.group(this.eventLoopGroup)
.channelFactory(RakChannelFactory.server(NioDatagramChannel.class))
.option(RakChannelOption.RAK_ADVERTISEMENT, pong.toByteBuf())
Expand All @@ -84,8 +84,7 @@ protected void initSession(BedrockServerSession session) {
}
})
.bind(bindAddress)
.awaitUninterruptibly()
.channel();
.syncUninterruptibly();

System.out.println("Bedrock Connection Started: " + bindIp + ":" + port);
if(BedrockConnect.kickInactive) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static ModalFormRequestPacket createMain(List<String> servers, BedrockSer
buttons.add(UIComponents.createButton(BedrockConnect.language.getWording("main", "exitBtn")));

for(int i=0;i<servers.size();i++) {
buttons.add(UIComponents.createButton(UIComponents.getServerDisplayName(servers.get(i)), "https://i.imgur.com/3BmFZRE.png", "url"));
buttons.add(UIComponents.createButton(UIComponents.getServerDisplayName(servers.get(i)), "https://i.imgur.com/nhumQVP.png", "url"));
}

for (CustomEntry cs : customServers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class PacketHandler implements BedrockPacketHandler {

Expand All @@ -44,11 +46,14 @@ public class PacketHandler implements BedrockPacketHandler {

private JSONObject extraData;

// Used for server icon fix
private ScheduledThreadPoolExecutor executor = null;

public void setPlayer(BCPlayer player) {
this.player = player;
}

public static String getIP(String hostname) {
public String getIP(String hostname) {
try {
if(BedrockConnect.fetchFeaturedIps) {
InetAddress host = InetAddress.getByName(hostname);
Expand Down Expand Up @@ -378,11 +383,12 @@ public PacketSignal handle(NetworkStackLatencyPacket packet) {
List<AttributeData> attributes = Collections.singletonList(new AttributeData("minecraft:player.level", 0f, 24791.00f, 0, 0f));
updateAttributesPacket.setAttributes(attributes);

Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() { if(session.isConnected()) { session.sendPacket(updateAttributesPacket); } }
};
timer.schedule(task, 500);
if (executor == null)
executor = new ScheduledThreadPoolExecutor(1);

executor.schedule(() -> {
session.sendPacket(updateAttributesPacket);
}, 500, TimeUnit.MILLISECONDS);

return PacketSignal.HANDLED;
}
Expand Down Expand Up @@ -442,11 +448,14 @@ public PacketHandler(BedrockServerSession session, Server server, boolean packet
@Override
public void onDisconnect(String reason) {
System.out.println(name + " disconnected");
if(executor != null)
executor.shutdown();
if(player != null)
server.removePlayer(player);

}

private static boolean validateChainData(List<SignedJWT> chain) throws Exception {
private boolean validateChainData(List<SignedJWT> chain) throws Exception {
if (chain.size() != 3) {
return false;
}
Expand Down Expand Up @@ -497,7 +506,7 @@ private static boolean validateChainData(List<SignedJWT> chain) throws Exception
}


private static boolean verifyJwt(JWSObject jwt, ECPublicKey key) throws JOSEException {
private boolean verifyJwt(JWSObject jwt, ECPublicKey key) throws JOSEException {
return jwt.verify(new DefaultJWSVerifierFactory().createJWSVerifier(jwt.getHeader(), key));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void Basic_SQL(final String query){

public BCPlayer getPlayer(ResultSet rs, String uuid, BedrockServerSession session) {
try {
BCPlayer p = new BCPlayer(uuid,this, session, UIComponents.getFormData(rs.getString("servers")), rs.getInt("serverLimit"));
BCPlayer p = new BCPlayer(uuid, session, UIComponents.getFormData(rs.getString("servers")), rs.getInt("serverLimit"));
return p;
}
catch(SQLException e) {
Expand Down Expand Up @@ -125,7 +125,7 @@ public void userExists(String uuid, String name, BedrockServerSession session, P
pw.flush();
pw.close();

BCPlayer pl = new BCPlayer(uuid, this, session, new ArrayList<>(), Integer.parseInt(serverLimit));
BCPlayer pl = new BCPlayer(uuid, session, new ArrayList<>(), Integer.parseInt(serverLimit));
packetHandler.setPlayer(pl);
BedrockConnect.server.addPlayer(pl);
} else {
Expand All @@ -137,7 +137,7 @@ public void userExists(String uuid, String name, BedrockServerSession session, P

JSONArray servers = (JSONArray) jo.get("servers");

BCPlayer p = new BCPlayer(uuid,this, session, servers, serverLimit);
BCPlayer p = new BCPlayer(uuid, session, servers, serverLimit);
packetHandler.setPlayer(p);
BedrockConnect.server.addPlayer(p);
}
Expand All @@ -160,7 +160,7 @@ public void addNewUser(String uuid, String name, BedrockServerSession session, P
s.setInt(3, Integer.parseInt(serverLimit));
s.executeUpdate();
System.out.println("[BedrockConnect] Added new user '" + name + "' (" + uuid + ") to Database.");
BCPlayer pl = new BCPlayer(uuid, db, session, new ArrayList<>(), Integer.parseInt(serverLimit));
BCPlayer pl = new BCPlayer(uuid, session, new ArrayList<>(), Integer.parseInt(serverLimit));
packetHandler.setPlayer(pl);
BedrockConnect.server.addPlayer(pl);
}
Expand Down

0 comments on commit 1e4ef82

Please sign in to comment.