package protocolsupport.protocol.typeremapper.entity.metadata.value;

import protocolsupport.protocol.typeremapper.legacy.chat.LegacyChat;
import protocolsupport.protocol.utils.datawatcher.DataWatcherObject;
import protocolsupport.protocol.utils.datawatcher.DataWatcherObjectIndex;
import protocolsupport.protocol.utils.datawatcher.objects.DataWatcherObjectOptionalChat;
import protocolsupport.protocol.utils.datawatcher.objects.DataWatcherObjectString;

public class IndexValueRemapperOptionalChatToString extends IndexValueRemapper<DataWatcherObjectOptionalChat> {

	protected final int limit;

	public IndexValueRemapperOptionalChatToString(DataWatcherObjectIndex<DataWatcherObjectOptionalChat> fromIndex, int toIndex, int limit) {
		super(fromIndex, toIndex);
		this.limit = limit;
	}

	@Override
	public DataWatcherObject<?> remapValue(DataWatcherObjectOptionalChat object) {
		//TODO: pass locale
		return new DataWatcherObjectString(object.getValue() != null ? LegacyChat.clampLegacyText(object.getValue().toLegacyText(), limit) : "");
	}

}

@@ -63,16 +63,46 @@
 value;%0A%0A
+import org.bukkit.ChatColor;%0A%0A
 import p
@@ -932,42 +932,11 @@
 e%0A%09%09
-return new DataWatcherObjectString
+if 
 (obj
@@ -961,10 +961,28 @@
 null
- ?
+) %7B%0A%09%09%09String text =
  Leg
@@ -1049,15 +1049,159 @@
 mit)
- : %22%22);
+;%0A%09%09%09return new DataWatcherObjectString(!text.isEmpty() ? text : ChatColor.BLACK.toString());%0A%09%09%7D else %7B%0A%09%09%09return new DataWatcherObjectString(%22%22);%0A%09%09%7D
 %0A%09%7D%0A

Fix optionalchat -> string datawatcher converter.