ProtocolLib icon indicating copy to clipboard operation
ProtocolLib copied to clipboard

Add several scoreboard type wrappers

Open vytskalt opened this issue 1 year ago • 2 comments

Added wrappers for these NMS types (Mojang mapped names):

I also fixed a compile error. ~~Marked as draft for now as I haven't fully tested it yet.~~

Closes #2617

vytskalt avatar Feb 26 '24 18:02 vytskalt

I have manually tested everything with the hacky code below on 1.8.9, 1.12.2, 1.13, 1.16.5, 1.17, 1.20.2 and 1.20.4. Everything appears to be working correctly.

public void testProtocolLib() {
  ProtocolManager pm = ProtocolLibrary.getProtocolManager();
  if (new MinecraftVersion(1, 20, 2).atOrAbove()) {
    getLogger().info("Testing DisplaySlot (1.20.2+)");
    pm.createPacket(PacketType.Play.Server.SCOREBOARD_DISPLAY_OBJECTIVE)
      .getDisplaySlots()
      .write(0, EnumWrappers.DisplaySlot.LIST);
  }

  // expect parameters to be supported on 1.17+
  Preconditions.checkState(WrappedTeamParameters.isSupported() == MinecraftVersion.CAVES_CLIFFS_1.atOrAbove());

  if (MinecraftVersion.CAVES_CLIFFS_1.atOrAbove()) {
    getLogger().info("Testing WrappedTeamParameters (1.17+)");
    WrappedChatComponent c = WrappedChatComponent.fromText("test");
    WrappedTeamParameters p = WrappedTeamParameters.newBuilder()
      .displayName(c)
      .prefix(c)
      .suffix(c)
      .nametagVisibility("never")
      .collisionRule("always")
      .color(EnumWrappers.ChatFormatting.OBFUSCATED)
      .options(1)
      .build();

    Preconditions.checkState(p.getDisplayName().getHandle() == c.getHandle());
    Preconditions.checkState(p.getPrefix().getHandle() == c.getHandle());
    Preconditions.checkState(p.getSuffix().getHandle() == c.getHandle());
    Preconditions.checkState(p.getNametagVisibility().equals("never"));
    Preconditions.checkState(p.getCollisionRule().equals("always"));
    Preconditions.checkState(p.getColor() == EnumWrappers.ChatFormatting.OBFUSCATED);
    Preconditions.checkState(p.getOptions() == 1);
  }

  getLogger().info("Testing ChatFormatting");
  Object colorHandle = EnumWrappers.getChatFormattingConverter().getGeneric(EnumWrappers.ChatFormatting.DARK_AQUA);
  Preconditions.checkState(EnumWrappers.ChatFormatting.DARK_AQUA == EnumWrappers.getChatFormattingConverter().getSpecific(colorHandle));

  Preconditions.checkState(WrappedNumberFormat.isSupported() == MinecraftVersion.v1_20_4.atOrAbove());

  getLogger().info("Testing RenderType");
  pm.createPacket(PacketType.Play.Server.SCOREBOARD_OBJECTIVE)
    .getRenderTypes()
    .write(0, EnumWrappers.RenderType.HEARTS);

  {
    getLogger().info("Testing WrappedComponentStyle");
    JsonObject style = (JsonObject) new JsonParser().parse("{\"color\":\"green\",\"bold\":true}");
    WrappedComponentStyle wrappedStyle = WrappedComponentStyle.fromJson(style);
    Preconditions.checkState(wrappedStyle.getJson().equals(style));
  }

  getLogger().info("All tests passed!");
}

vytskalt avatar Mar 02 '24 19:03 vytskalt

Also FYI @dmulloy2 the tests don't currently run because of a version mismatch between spigot-api and spigot. org.spigotmc:spigot:1.20.4-R0.1-SNAPSHOT in the https://repo.dmulloy2.net/ repo needs to be updated to the latest version.

vytskalt avatar Mar 03 '24 08:03 vytskalt

@dmulloy2 pls let me know if you are interested in this PR so I can know if I should update it for 1.20.6

vytskalt avatar May 15 '24 16:05 vytskalt

@vytskalt this looks great! Would love if you could update it for 1.20.6. Seems like it's failing to compile because those new methods already exist in serialized offline player

dmulloy2 avatar Jun 05 '24 16:06 dmulloy2