/*
 * Aeronica's mxTune MOD
 * Copyright 2019, Paul Boese a.k.a. Aeronica
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package aeronicamc.mods.mxtune.network;

import net.minecraft.network.PacketBuffer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class NetworkLongUtfHelper
{
    private static final Logger LOGGER = LogManager.getLogger(NetworkLongUtfHelper.class);
    private static final int MAX_STRING_BUFFER = 16384;
    private static final String EMPTY_STRING = "";

    public NetworkLongUtfHelper() { /* NOP */ }

    /**
     * The {@link PacketBuffer} utf strings are limited to 32767 bytes. Therefore they
     * are broken up into separate strings if larger than {@value MAX_STRING_BUFFER} characters
     *
     * @param buffer The vanilla netty packet wrapper.
     * @param stringIn The string to be transferred.
     */
    public void writeLongUtf(PacketBuffer buffer, String stringIn)
    {
        int totalLength = stringIn.length();
        int index = 0;
        int start;
        int end;

        buffer.writeInt(totalLength);
        buffer.writeInt(stringIn.hashCode());
        do
        {
            start = (MAX_STRING_BUFFER * index);
            end = Math.min((MAX_STRING_BUFFER * (index + 1)), totalLength);
            buffer.writeUtf(stringIn.substring(start, end));
            index++;
        } while (end < totalLength);
    }

    public String readLongUtf(PacketBuffer buffer)
    {
        StringBuilder buildString = new StringBuilder();

        int expectedLength = buffer.readInt();
        int expectedHashCode = buffer.readInt();
        do
            buildString.append(buffer.readUtf(MAX_STRING_BUFFER));
        while (buildString.length() < expectedLength);

        String receivedString = buildString.toString();

        if ((expectedHashCode == receivedString.hashCode()) && (expectedLength == receivedString.length()))
        {
            return receivedString;
        }
        else
        {
            LOGGER.error("StringHelper#readLongString received data error: expected length: {}, actual: {}", expectedLength, receivedString.length());
            LOGGER.error("StringHelper#readLongString received data error: expected hash: {}, actual: {}", expectedHashCode, receivedString.hashCode());
            return EMPTY_STRING;
        }
    }
}

@@ -1941,22 +1941,151 @@
 ength);%0A
+        LOGGER.debug(%22writeLongUtf: Buffer Count: %7B%7D, Total Length: %7B%7D, hashcode: %7B%7D%22, index, totalLength, stringIn.hashCode());%0A
     %7D%0A
-
 %0A    pub
@@ -2280,32 +2280,55 @@
 ffer.readInt();%0A
+        int count = 0;%0A
         do%0A     
@@ -2313,32 +2313,34 @@
  = 0;%0A        do
+ %7B
 %0A            bui
@@ -2398,16 +2398,39 @@
 %0A       
+     count++;%0A        %7D
  while (
@@ -2635,16 +2635,16 @@
 gth()))%0A
-
         
@@ -2637,32 +2637,240 @@
 h()))%0A        %7B%0A
+            LOGGER.debug(%22readLongUtf:  Buffer Count: %7B%7D, Total Length: %7B%7D, Hashcode: %7B%7D, Received Hashcode: %7B%7D%22,%0A                         count, expectedLength, expectedHashCode, receivedString.hashCode());%0A
             retu

Add debug Logging for long string network buffers.