Search in sources :

Example 1 with ChatText

use of com.bergerkiller.bukkit.common.wrappers.ChatText in project BKCommonLib by bergerhealer.

the class BlockStateChangePacketHandler_1_8_to_1_9_2 method enable.

@Override
public void enable() {
    register(PacketType.OUT_UPDATE_SIGN, (player, commonPacket, listener) -> {
        final PacketPlayOutUpdateSignHandle packet = PacketPlayOutUpdateSignHandle.createHandle(commonPacket.getHandle());
        // Stores decoded String lines that it was before, for easier comparison
        // Only set if the deferred metadata supplier is ever called
        final AtomicReference<String[]> linesBeforeRef = new AtomicReference<>();
        // Initialize the metadata once called
        final DeferredSupplier<CommonTagCompound> metadataSupplier = DeferredSupplier.of(() -> {
            ChatText[] lines = packet.getLines();
            CommonTagCompound metadata = new CommonTagCompound();
            String[] linesBefore = new String[4];
            for (int n = 0; n < 4; n++) {
                metadata.putValue(LINE_META_KEYS[n], linesBefore[n] = lines[n].getJson());
            }
            linesBeforeRef.set(linesBefore);
            return metadata;
        });
        // Errors are handled upstream
        if (!listener.onBlockChange(player, BlockStateChange.deferred(packet.getPosition(), BlockStateType.SIGN, metadataSupplier, () -> true))) {
            return false;
        }
        // Only if getMetadata() was ever even called
        if (metadataSupplier.isInitialized()) {
            // Check if the lines in metadata changed compared to the lines in the packet
            // If they are, write them all back (re-serialize them as chat components)
            // Don't touch lines that weren't modified to retain any original json formatting
            CommonTagCompound metadata = metadataSupplier.get();
            String[] linesBefore = linesBeforeRef.get();
            ChatText[] newLines = packet.getLines().clone();
            boolean changed = false;
            for (int n = 0; n < 4; n++) {
                String newLine = metadata.getValue(LINE_META_KEYS[n], "");
                if (!linesBefore[n].equals(newLine)) {
                    newLines[n] = ChatText.fromJson(newLine);
                    changed = true;
                }
            }
            if (changed) {
                packet.setLines(newLines);
            }
        }
        return true;
    });
}
Also used : PacketPlayOutUpdateSignHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutUpdateSignHandle) ChatText(com.bergerkiller.bukkit.common.wrappers.ChatText) CommonTagCompound(com.bergerkiller.bukkit.common.nbt.CommonTagCompound) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 2 with ChatText

use of com.bergerkiller.bukkit.common.wrappers.ChatText in project BKCommonLib by bergerhealer.

the class ChatTextTest method testEmpty.

@Test
public void testEmpty() {
    // Test empty chat text
    ChatText text = ChatText.empty();
    assertEquals("", text.getMessage());
    assertEquals("{\"text\":\"\"}", text.getJson());
}
Also used : ChatText(com.bergerkiller.bukkit.common.wrappers.ChatText) Test(org.junit.Test)

Example 3 with ChatText

use of com.bergerkiller.bukkit.common.wrappers.ChatText in project BKCommonLib by bergerhealer.

the class ChatTextTest method testStyleOnly.

@Test
public void testStyleOnly() {
    String msg = ChatColor.RED.toString();
    ChatText text = ChatText.fromMessage(msg);
    Set<String> allowed = new HashSet<String>();
    allowed.add("{\"extra\":[{\"color\":\"red\",\"text\":\"\"}],\"text\":\"\"}");
    if (Common.evaluateMCVersion(">=", "1.16")) {
        // For some reason Minecraft repeats the color twice now in the json
        // It's still functionally identical, so we'll allow it I guess.
        allowed.add("{\"extra\":[{\"color\":\"red\",\"text\":\"\"},{\"color\":\"red\",\"text\":\"\"}],\"text\":\"\"}");
        // It's even more messed up now :(
        allowed.add("{\"extra\":[{\"bold\":false,\"italic\":false,\"underlined\":false,\"strikethrough\":false,\"obfuscated\":false,\"color\":\"red\",\"text\":\"\"},{\"color\":\"red\",\"text\":\"\"}],\"text\":\"\"}");
    }
    String json = text.getJson();
    if (!allowed.contains(json)) {
        fail("Invalid JSON: " + json);
    }
    assertEquals(msg, text.getMessage());
}
Also used : ChatText(com.bergerkiller.bukkit.common.wrappers.ChatText) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with ChatText

use of com.bergerkiller.bukkit.common.wrappers.ChatText in project BKCommonLib by bergerhealer.

the class ChatTextTest method testStyleBeforeColor.

@Test
@Ignore
public void testStyleBeforeColor() {
    // TODO: This appears to be a bug in Bukkit itself
    // Not fixing this for now. Just put color before the styling.
    String message = ChatColor.BOLD.toString() + ChatColor.RED + "test";
    ChatText text = ChatText.fromMessage(message);
    System.out.println(text.getJson());
    assertEquals(message, text.getMessage());
}
Also used : ChatText(com.bergerkiller.bukkit.common.wrappers.ChatText) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with ChatText

use of com.bergerkiller.bukkit.common.wrappers.ChatText in project BKCommonLib by bergerhealer.

the class ChatTextTest method testChatText.

@Test
public void testChatText() {
    String msg = "Hello, " + ChatColor.RED + "World!";
    ChatText text = ChatText.fromMessage(msg);
    assertEquals(msg, text.getMessage());
    Set<String> allowed = new HashSet<String>();
    if (Common.evaluateMCVersion(">=", "1.16")) {
        // Spigot 1.16 has a (temporary?) bug of including all style modes in the json
        // might get fixed in the future
        allowed.add("{\"extra\":[{\"text\":\"Hello, \"},{\"bold\":false,\"italic\":false,\"underlined\":false,\"strikethrough\":false,\"obfuscated\":false,\"color\":\"red\",\"text\":\"World!\"}],\"text\":\"\"}");
    }
    if (CommonCapabilities.CHAT_TEXT_JSON_VER2) {
        allowed.add("{\"extra\":[{\"text\":\"Hello, \"},{\"color\":\"red\",\"text\":\"World!\"}],\"text\":\"\"}");
    } else {
        allowed.add("{\"extra\":[\"Hello, \",{\"color\":\"red\",\"text\":\"World!\"}],\"text\":\"\"}");
    }
    String result = text.getJson();
    if (!allowed.contains(result)) {
        System.out.println("Incorrect JSON: " + result);
        fail("Chat text conversion to JSON is not working correctly");
    }
}
Also used : ChatText(com.bergerkiller.bukkit.common.wrappers.ChatText) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ChatText (com.bergerkiller.bukkit.common.wrappers.ChatText)8 Test (org.junit.Test)7 HashSet (java.util.HashSet)2 CommonTagCompound (com.bergerkiller.bukkit.common.nbt.CommonTagCompound)1 PacketPlayOutUpdateSignHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutUpdateSignHandle)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Ignore (org.junit.Ignore)1