Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chat client options #7105

Merged
merged 24 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.stream.Collectors;

import ch.njol.skript.bukkitutil.BukkitUtils;
import com.destroystokyo.paper.ClientOption;
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Difficulty;
Expand Down Expand Up @@ -1526,6 +1527,14 @@ public String toVariableNameString(EnchantmentOffer eo) {
.name("Transform Reason")
.description("Represents a transform reason of an <a href='events.html#entity transform'>entity transform event</a>.")
.since("2.8.0"));

if (Skript.classExists("com.destroystokyo.paper.ClientOption$ChatVisibility"))
Classes.registerClass(new EnumClassInfo<>(ClientOption.ChatVisibility.class, "chatvisibility", "chat visibilities")
.user("chat ?visibility")
.name("Chat Visibility")
.description("The chat visibility of a player.")
.requiredPlugins("Paper")
.since("INSERT VERSION"));
}

}
39 changes: 39 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondChatColors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 <red>this<reset> message.\"",
"else:",
"\tsend \"You cannot partake in finding the colored word.\""
})
@RequiredPlugins("Paper")
@Since("INSERT VERSION")
public class CondChatColors extends PropertyCondition<Player> {

private static final boolean SUPPORTS_CLIENT_OPTION =
Skript.classExists("com.destroystokyo.paper.ClientOption");

static {
if (SUPPORTS_CLIENT_OPTION)
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 "can see chat colors";
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
}

}
37 changes: 37 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondChatFiltering.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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 \"<gray>This server may contain mature chat messages. You have been warned!\" to player",
})
@RequiredPlugins("Paper")
@Since("INSERT VERSION")
public class CondChatFiltering extends PropertyCondition<Player> {

private static final boolean SUPPORTS_CLIENT_OPTION =
Skript.classExists("com.destroystokyo.paper.ClientOption");

static {
if (SUPPORTS_CLIENT_OPTION)
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 "has chat filtering enabled";
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
}

}
44 changes: 44 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprChatVisibility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.*;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import com.destroystokyo.paper.ClientOption;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;

@Name("Chat Visibility")
@Description("The chat visibility of a player. Can only be read.")
@Examples({
"if chat visibility of player is hidden or commands only:",
"\tsend actionbar \"Server restart in 5 minutes\" to player"
})
@RequiredPlugins("Paper")
@Since("INSERT VERSION")
public class ExprChatVisibility extends SimplePropertyExpression<Player, ClientOption.ChatVisibility> {

private static final boolean SUPPORTS_CHAT_VISIBILITY =
Skript.classExists("com.destroystokyo.paper.ClientOption$ChatVisibility");

static {
if (SUPPORTS_CHAT_VISIBILITY)
register(ExprChatVisibility.class, ClientOption.ChatVisibility.class,
"chat visibility", "players");
}

@Override
public @Nullable ClientOption.ChatVisibility convert(Player from) {
return from.getClientOption(ClientOption.CHAT_VISIBILITY);
}

@Override
public Class<ClientOption.ChatVisibility> getReturnType() {
return ClientOption.ChatVisibility.class;
}

@Override
protected String getPropertyName() {
return "chat visibility";
}

}
8 changes: 8 additions & 0 deletions src/main/resources/lang/default.lang
Original file line number Diff line number Diff line change
Expand Up @@ -2275,6 +2275,13 @@ transform reasons:
unknown: unknown
infection: infection, villager infection

# -- Chat Visibilities --
chat visibilities:
full: full, visible
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
hidden: hidden
system: system, commands only, only commands
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
unknown: unknown

# -- Boolean --
boolean:
true:
Expand Down Expand Up @@ -2354,6 +2361,7 @@ types:
quitreason: quit reason¦s @a
inventoryclosereason: inventory close reason¦s @an
transformreason: transform reason¦s @a
chatvisibility: chat visibility¦s @a

# Skript
weathertype: weather type¦s @a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.skriptlang.skript.test.tests.syntaxes.expressions;

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 ExprChatVisibilityTest 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"));
}

}
18 changes: 18 additions & 0 deletions src/test/skript/junit/ExprChatVisibility.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
test "ExprChatVisibilityJUnit" 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.expressions.ExprChatVisibilityTest" completes {_tests::*}

on join:
set {_test} to "org.skriptlang.skript.test.tests.syntaxes.expressions.ExprChatVisibilityTest"
junit test is {_test}

if player's chat visibility is only commands:
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
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}
Efnilite marked this conversation as resolved.
Show resolved Hide resolved