Skip to content

Commit

Permalink
Add chat client options (#7105)
Browse files Browse the repository at this point in the history
* init commit

* update

* update

* update doc

* weird error

* weird error

* Apply suggestions from code review

Co-authored-by: sovdee <[email protected]>

* ok

* add new cond

* remove chat visibility type

* remove old

* fix names

* oops

* oops

* aight

---------

Co-authored-by: sovdee <[email protected]>
  • Loading branch information
Efnilite and sovdeeth authored Oct 31, 2024
1 parent ead4ca4 commit d75b47f
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 0 deletions.
36 changes: 36 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,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 <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> {

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

}
35 changes: 35 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,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 \"<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> {

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

}
83 changes: 83 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondChatVisibility.java
Original file line number Diff line number Diff line change
@@ -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> player;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] expressions, int matchedPattern,
Kleenean isDelayed, ParseResult parseResult) {
pattern = matchedPattern;
player = (Expression<Player>) 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);
};
}

}
Original file line number Diff line number Diff line change
@@ -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"));
}

}
18 changes: 18 additions & 0 deletions src/test/skript/junit/CondChatVisibility.sk
Original file line number Diff line number Diff line change
@@ -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}

0 comments on commit d75b47f

Please sign in to comment.