33import java .io .File ;
44import java .io .IOException ;
55import java .io .InputStream ;
6+ import java .io .StringWriter ;
67import java .lang .reflect .Field ;
78import java .net .SocketAddress ;
89import java .nio .ByteBuffer ;
6970import net .md_5 .bungee .api .event .ServerSwitchEvent ;
7071import net .md_5 .bungee .api .plugin .Listener ;
7172import net .md_5 .bungee .api .plugin .Plugin ;
73+ import net .md_5 .bungee .config .Configuration ;
7274import net .md_5 .bungee .config .ConfigurationProvider ;
7375import net .md_5 .bungee .config .YamlConfiguration ;
7476import net .md_5 .bungee .event .EventHandler ;
7880
7981public class InteractiveChatBungee extends Plugin implements Listener {
8082
81- public static net .md_5 .bungee .config .Configuration configuration = null ;
82- public static ConfigurationProvider config = null ;
83+ public static Configuration config = null ;
84+ public static ConfigurationProvider yamlConfigProvider = null ;
85+ public static File configFile ;
86+ public static File playerDataFolder ;
8387
8488 public static Plugin plugin ;
8589 public static Metrics metrics ;
@@ -104,15 +108,19 @@ public class InteractiveChatBungee extends Plugin implements Listener {
104108 public void onEnable () {
105109 plugin = this ;
106110
107- config = ConfigurationProvider .getProvider (YamlConfiguration .class );
108- if (!getDataFolder ().exists ())
111+ yamlConfigProvider = ConfigurationProvider .getProvider (YamlConfiguration .class );
112+ if (!getDataFolder ().exists ()) {
109113 getDataFolder ().mkdir ();
114+ }
115+ configFile = new File (getDataFolder (), "bungeeconfig.yml" );
116+ playerDataFolder = new File (getDataFolder (), "player_data" );
117+ if (!playerDataFolder .exists ()) {
118+ playerDataFolder .mkdirs ();
119+ }
110120
111- File file = new File (getDataFolder (), "bungeeconfig.yml" );
112-
113- if (!file .exists ()) {
121+ if (!configFile .exists ()) {
114122 try (InputStream in = getResourceAsStream ("bungeeconfig.yml" )) {
115- Files .copy (in , file .toPath ());
123+ Files .copy (in , configFile .toPath ());
116124 } catch (IOException e ) {
117125 e .printStackTrace ();
118126 }
@@ -142,8 +150,8 @@ public void onDisable() {
142150
143151 public static void loadConfig () {
144152 try {
145- configuration = config .load (new File ( plugin . getDataFolder (), "bungeeconfig.yml" ) );
146- parseCommands = configuration .getStringList ("Settings.CommandsToParse" );
153+ config = yamlConfigProvider .load (configFile );
154+ parseCommands = config .getStringList ("Settings.CommandsToParse" );
147155 } catch (IOException e ) {
148156 e .printStackTrace ();
149157 }
@@ -308,6 +316,13 @@ public void run() {
308316 placeholderList .put (((Server ) event .getSender ()).getInfo ().getName (), list );
309317 forwardPlaceholderList (list , ((Server ) event .getSender ()).getInfo ());
310318 break ;
319+ case 0x12 :
320+ UUID uuid2 = DataTypeIO .readUUID (input );
321+ String playerdata = DataTypeIO .readString (input , StandardCharsets .UTF_8 );
322+ Configuration playerconfig = yamlConfigProvider .load (playerdata );
323+ yamlConfigProvider .save (playerconfig , new File (playerDataFolder , uuid2 .toString ()));
324+ forwardPlayerData (uuid2 , playerdata , ((Server ) event .getSender ()).getInfo ());
325+ break ;
311326 }
312327 } catch (IOException | DataFormatException e ) {
313328 e .printStackTrace ();
@@ -322,6 +337,38 @@ public void run() {
322337 }
323338 }
324339
340+ private void forwardPlayerData (UUID uuid , String playerdata , ServerInfo serverFrom ) throws IOException {
341+ ByteArrayDataOutput output = ByteStreams .newDataOutput ();
342+
343+ DataTypeIO .writeUUID (output , uuid );
344+ DataTypeIO .writeString (output , playerdata , StandardCharsets .UTF_8 );
345+
346+ int packetNumber = random .nextInt ();
347+ int packetId = 0x12 ;
348+ byte [] data = output .toByteArray ();
349+
350+ byte [][] dataArray = CustomArrayUtils .divideArray (CompressionUtils .compress (data ), 32700 );
351+
352+ for (int i = 0 ; i < dataArray .length ; i ++) {
353+ byte [] chunk = dataArray [i ];
354+
355+ ByteArrayDataOutput out = ByteStreams .newDataOutput ();
356+ out .writeInt (packetNumber );
357+
358+ out .writeShort (packetId );
359+ out .writeBoolean (i == (dataArray .length - 1 ));
360+
361+ out .write (chunk );
362+
363+ for (ServerInfo server : getProxy ().getServers ().values ()) {
364+ if (!server .getSocketAddress ().equals (serverFrom .getSocketAddress ())) {
365+ server .sendData ("interchat:main" , out .toByteArray ());
366+ pluginMessagesCounter .incrementAndGet ();
367+ }
368+ }
369+ }
370+ }
371+
325372 private void forwardPlaceholderList (List <ICPlaceholder > serverPlaceholderList , ServerInfo serverFrom ) throws IOException {
326373 ByteArrayDataOutput output = ByteStreams .newDataOutput ();
327374
@@ -539,10 +586,23 @@ public void write(ChannelHandlerContext channelHandlerContext, Object obj, Chann
539586 @ EventHandler
540587 public void onPlayerConnected (PostLoginEvent event ) {
541588 ProxiedPlayer player = event .getPlayer ();
589+ UUID uuid = player .getUniqueId ();
542590 forwardedMessages .put (player .getUniqueId (), new ArrayList <>());
543591 List <UUID > messageQueue = Collections .synchronizedList (new LinkedList <>());
544592 requestedMessageProcesses .put (player .getUniqueId (), messageQueue );
545593
594+ File playerFile = new File (playerDataFolder , uuid .toString ());
595+ if (playerFile .exists ()) {
596+ try {
597+ Configuration playerconfig = yamlConfigProvider .load (playerFile );
598+ StringWriter writer = new StringWriter ();
599+ yamlConfigProvider .save (playerconfig , writer );
600+ forwardPlayerData (uuid , writer .toString (), player .getServer ().getInfo ());
601+ } catch (IOException e ) {
602+ e .printStackTrace ();
603+ }
604+ }
605+
546606 UserConnection userConnection = (UserConnection ) player ;
547607 ChannelWrapper channelWrapper ;
548608 Field channelField = null ;
0 commit comments