From d75b47fda85376b28b12c9dbd627d317fb5a68af Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Thu, 31 Oct 2024 21:21:22 +0100 Subject: [PATCH] Add chat client options (#7105) * init commit * update * update * update doc * weird error * weird error * Apply suggestions from code review Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com> * ok * add new cond * remove chat visibility type * remove old * fix names * oops * oops * aight --------- Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com> --- .../skript/conditions/CondChatColors.java | 36 ++++++++ .../skript/conditions/CondChatFiltering.java | 35 ++++++++ .../skript/conditions/CondChatVisibility.java | 83 +++++++++++++++++++ .../conditions/CondChatVisibilityTest.java | 48 +++++++++++ src/test/skript/junit/CondChatVisibility.sk | 18 ++++ 5 files changed, 220 insertions(+) create mode 100644 src/main/java/ch/njol/skript/conditions/CondChatColors.java create mode 100644 src/main/java/ch/njol/skript/conditions/CondChatFiltering.java create mode 100644 src/main/java/ch/njol/skript/conditions/CondChatVisibility.java create mode 100644 src/test/java/org/skriptlang/skript/test/tests/syntaxes/conditions/CondChatVisibilityTest.java create mode 100644 src/test/skript/junit/CondChatVisibility.sk diff --git a/src/main/java/ch/njol/skript/conditions/CondChatColors.java b/src/main/java/ch/njol/skript/conditions/CondChatColors.java new file mode 100644 index 00000000000..25f90805835 --- /dev/null +++ b/src/main/java/ch/njol/skript/conditions/CondChatColors.java @@ -0,0 +1,36 @@ +package ch.njol.skript.conditions; + +import ch.njol.skript.Skript; +import ch.njol.skript.conditions.base.PropertyCondition; +import ch.njol.skript.doc.*; +import com.destroystokyo.paper.ClientOption; +import org.bukkit.entity.Player; + +@Name("Can See Chat Colors") +@Description("Checks whether a player can see chat colors.") +@Examples({ + "if player can see chat colors:", + "\tsend \"Find the red word in this message.\"", + "else:", + "\tsend \"You cannot partake in finding the colored word.\"" +}) +@RequiredPlugins("Paper") +@Since("INSERT VERSION") +public class CondChatColors extends PropertyCondition { + + static { + if (Skript.classExists("com.destroystokyo.paper.ClientOption")) + register(CondChatColors.class, PropertyType.CAN, "see chat colo[u]r[s|ing]", "players"); + } + + @Override + public boolean check(Player player) { + return player.getClientOption(ClientOption.CHAT_COLORS_ENABLED); + } + + @Override + protected String getPropertyName() { + return "see chat colors"; + } + +} diff --git a/src/main/java/ch/njol/skript/conditions/CondChatFiltering.java b/src/main/java/ch/njol/skript/conditions/CondChatFiltering.java new file mode 100644 index 00000000000..0cd28cfab10 --- /dev/null +++ b/src/main/java/ch/njol/skript/conditions/CondChatFiltering.java @@ -0,0 +1,35 @@ +package ch.njol.skript.conditions; + +import ch.njol.skript.Skript; +import ch.njol.skript.conditions.base.PropertyCondition; +import ch.njol.skript.doc.*; +import com.destroystokyo.paper.ClientOption; +import org.bukkit.entity.Player; + +@Name("Has Chat Filtering") +@Description("Checks whether a player has chat filtering enabled.") +@Examples({ + "if player doesn't have chat filtering enabled:", + "send \"This server may contain mature chat messages. You have been warned!\" to player", +}) +@RequiredPlugins("Paper") +@Since("INSERT VERSION") +public class CondChatFiltering extends PropertyCondition { + + static { + if (Skript.classExists("com.destroystokyo.paper.ClientOption")) + register(CondChatFiltering.class, PropertyType.HAVE, + "(chat|text) filtering (on|enabled)", "players"); + } + + @Override + public boolean check(Player player) { + return player.getClientOption(ClientOption.TEXT_FILTERING_ENABLED); + } + + @Override + protected String getPropertyName() { + return "chat filtering enabled"; + } + +} diff --git a/src/main/java/ch/njol/skript/conditions/CondChatVisibility.java b/src/main/java/ch/njol/skript/conditions/CondChatVisibility.java new file mode 100644 index 00000000000..b8045cc952f --- /dev/null +++ b/src/main/java/ch/njol/skript/conditions/CondChatVisibility.java @@ -0,0 +1,83 @@ +package ch.njol.skript.conditions; + +import ch.njol.skript.Skript; +import ch.njol.skript.doc.*; +import ch.njol.skript.lang.Condition; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.util.Kleenean; +import com.destroystokyo.paper.ClientOption; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.jetbrains.annotations.Nullable; + +@Name("Can See Messages") +@Description("Checks whether a player can see specific message types in chat.") +@Examples({ + "if player can see all messages:", + "\tsend \"You can see all messages.\"", + "if player can only see commands:", + "\tsend \"This game doesn't work with commands-only chat.\"", + "if player can't see any messages:", + "\tsend action bar \"Server shutting down in 5 minutes!\"" +}) +@RequiredPlugins("Paper") +@Since("INSERT VERSION") +public class CondChatVisibility extends Condition { + + static { + if (Skript.classExists("com.destroystokyo.paper.ClientOption$ChatVisibility")) + Skript.registerCondition(CondChatVisibility.class, + "%player% can see all messages [in chat]", + "%player% can only see (commands|system messages) [in chat]", + "%player% can('t|[ ]not) see any (command[s]|message[s]) [in chat]", + "%player% can('t|[ ]not) see all messages [in chat]", + "%player% can('t|[ ]not) only see (commands|system messages) [in chat]"); + } + + private int pattern = 0; + private Expression player; + + @Override + @SuppressWarnings("unchecked") + public boolean init(Expression[] expressions, int matchedPattern, + Kleenean isDelayed, ParseResult parseResult) { + pattern = matchedPattern; + player = (Expression) expressions[0]; + + setNegated(matchedPattern > 1); + return true; + } + + @Override + public boolean check(Event event) { + Player player = this.player.getSingle(event); + + if (player == null) + return false; + + ClientOption.ChatVisibility current = player.getClientOption(ClientOption.CHAT_VISIBILITY); + + return switch (pattern) { + case 0 -> current == ClientOption.ChatVisibility.FULL; + case 1 -> current == ClientOption.ChatVisibility.SYSTEM; + case 2 -> current == ClientOption.ChatVisibility.HIDDEN; + case 3 -> current != ClientOption.ChatVisibility.FULL; + case 4 -> current != ClientOption.ChatVisibility.SYSTEM; + default -> throw new IllegalStateException("Unexpected value: " + pattern); + }; + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return switch (pattern) { + case 0 -> player.toString(event, debug) + " can see all messages"; + case 1 -> player.toString(event, debug) + " can only see commands"; + case 2 -> player.toString(event, debug) + " can't see any messages"; + case 3 -> player.toString(event, debug) + " can't see all messages"; + case 4 -> player.toString(event, debug) + " can't only see commands"; + default -> throw new IllegalStateException("Unexpected value: " + pattern); + }; + } + +} diff --git a/src/test/java/org/skriptlang/skript/test/tests/syntaxes/conditions/CondChatVisibilityTest.java b/src/test/java/org/skriptlang/skript/test/tests/syntaxes/conditions/CondChatVisibilityTest.java new file mode 100644 index 00000000000..1dbcdc98c19 --- /dev/null +++ b/src/test/java/org/skriptlang/skript/test/tests/syntaxes/conditions/CondChatVisibilityTest.java @@ -0,0 +1,48 @@ +package org.skriptlang.skript.test.tests.syntaxes.conditions; + +import ch.njol.skript.Skript; +import ch.njol.skript.test.runner.SkriptJUnitTest; +import com.destroystokyo.paper.ClientOption; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerJoinEvent; +import org.easymock.EasyMock; +import org.junit.Before; +import org.junit.Test; + +public class CondChatVisibilityTest extends SkriptJUnitTest { + + private static final boolean SUPPORTS_CHAT_VISIBILITY = + Skript.classExists("com.destroystokyo.paper.ClientOption$ChatVisibility"); + + static { + setShutdownDelay(1); + } + + private Player player; + + @Before + public void setup() { + if (!SUPPORTS_CHAT_VISIBILITY) + return; + + player = EasyMock.niceMock(Player.class); + + EasyMock.expect(player.getClientOption(ClientOption.CHAT_VISIBILITY)) + .andReturn(ClientOption.ChatVisibility.SYSTEM); + EasyMock.expect(player.getClientOption(ClientOption.TEXT_FILTERING_ENABLED)) + .andReturn(false); + EasyMock.expect(player.getClientOption(ClientOption.CHAT_COLORS_ENABLED)) + .andReturn(true); + EasyMock.replay(player); + } + + @Test + public void test() { + if (!SUPPORTS_CHAT_VISIBILITY) + return; + + Bukkit.getPluginManager().callEvent(new PlayerJoinEvent(player, "hi")); + } + +} diff --git a/src/test/skript/junit/CondChatVisibility.sk b/src/test/skript/junit/CondChatVisibility.sk new file mode 100644 index 00000000000..9ce5f39dd9c --- /dev/null +++ b/src/test/skript/junit/CondChatVisibility.sk @@ -0,0 +1,18 @@ +test "CondChatVisibilityJUnit" when running JUnit: + set {_tests::1} to "chat visibility is only commands" + set {_tests::2} to "no chat filtering" + set {_tests::3} to "can see chat colours" + ensure junit test "org.skriptlang.skript.test.tests.syntaxes.conditions.CondChatVisibilityTest" completes {_tests::*} + +on join: + set {_test} to "org.skriptlang.skript.test.tests.syntaxes.conditions.CondChatVisibilityTest" + junit test is {_test} + + if player can only see commands: + complete objective "chat visibility is only commands" for {_test} + + if player doesn't have chat filtering enabled: + complete objective "no chat filtering" for {_test} + + if player can see chat colours: + complete objective "can see chat colours" for {_test}