Skip to content

Commit

Permalink
Add delete changer to ExprMessage (#6828)
Browse files Browse the repository at this point in the history
* Add delete changer

* Update examples

* use toString

Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>

* Add JUnit test (chat event)

* Call chat event in JUnit Test

* Create viewer Set differently

* Add more tests (join/quit)

* Remove use of components

---------

Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>
  • Loading branch information
Phill310 and sovdeeth authored Jul 1, 2024
1 parent 9db5426 commit abc782c
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 17 deletions.
32 changes: 18 additions & 14 deletions src/main/java/ch/njol/skript/expressions/ExprMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,27 @@
@SuppressWarnings("deprecation")
@Name("Message")
@Description("The (chat) message of a chat event, the join message of a join event, the quit message of a quit event, or the death message on a death event. This expression is mostly useful for being changed.")
@Examples({"on chat:",
" player has permission \"admin\"",
" set message to \"&c%message%\"",
@Examples({
"on chat:",
"\tplayer has permission \"admin\"",
"\tset message to \"&c%message%\"",
"",
"on first join:",
" set join message to \"Welcome %player% to our awesome server!\"",
"\tset join message to \"Welcome %player% to our awesome server!\"",
"",
"on join:",
" player has played before",
" set join message to \"Welcome back, %player%!\"",
"\tplayer has played before",
"\tset join message to \"Welcome back, %player%!\"",
"",
"on quit:",
" set quit message to \"%player% left this awesome server!\"",
"\tif {vanish::%player's uuid%} is set:",
"\t\tclear quit message",
"\telse:",
"\t\tset quit message to \"%player% left this awesome server!\"",
"",
"on death:",
" set the death message to \"%player% died!\""})
@Since("1.4.6 (chat message), 1.4.9 (join & quit messages), 2.0 (death message)")
"\tset the death message to \"%player% died!\""})
@Since("1.4.6 (chat message), 1.4.9 (join & quit messages), 2.0 (death message), INSERT VERSION (clear message)")
@Events({"chat", "join", "quit", "death"})
public class ExprMessage extends SimpleExpression<String> {

Expand Down Expand Up @@ -181,18 +185,18 @@ protected String[] get(final Event e) {
@Override
@Nullable
public Class<?>[] acceptChange(final ChangeMode mode) {
if (mode == ChangeMode.SET)
if (mode == ChangeMode.SET || mode == ChangeMode.DELETE)
return CollectionUtils.array(String.class);
return null;
}

@Override
public void change(final Event e, final @Nullable Object[] delta, final ChangeMode mode) {
assert mode == ChangeMode.SET;
assert delta != null;
assert mode == ChangeMode.SET || mode == ChangeMode.DELETE;
for (final Class<? extends Event> c : type.events) {
if (c.isInstance(e))
type.set(e, "" + delta[0]);
if (c.isInstance(e)) {
type.set(e, (mode == ChangeMode.DELETE) ? "" : delta[0].toString());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package org.skriptlang.skript.test.tests.syntaxes;
package org.skriptlang.skript.test.tests.syntaxes.expressions;

import ch.njol.skript.test.runner.SkriptJUnitTest;
import org.bukkit.entity.Pig;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package org.skriptlang.skript.test.tests.syntaxes.expressions;

import ch.njol.skript.test.runner.SkriptJUnitTest;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.PluginManager;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;

import java.util.HashSet;
import java.util.Set;

public class ExprMessageTest extends SkriptJUnitTest {

private Player testPlayer;

@Before
public void setup() {
testPlayer = EasyMock.niceMock(Player.class);
}

@Test
public void test() {
Set<Player> viewers = new HashSet<>();
viewers.add(testPlayer);
PluginManager manager = Bukkit.getServer().getPluginManager();
manager.callEvent(new AsyncPlayerChatEvent(false, testPlayer, "hi", viewers));
manager.callEvent(new PlayerJoinEvent(testPlayer, "hi"));
manager.callEvent(new PlayerQuitEvent(testPlayer, "hi"));
}
}
4 changes: 2 additions & 2 deletions src/test/skript/junit/ExprDrops.sk
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ test "ExprDropsJUnit" when running JUnit:
set {_tests::11} to "add and remove experience from drops doesn't modify items"
set {_tests::12} to "remove all experience from drops doesn't modify items"
set {_tests::13} to "drops test complete"
ensure junit test "org.skriptlang.skript.test.tests.syntaxes.ExprDropsTest" completes {_tests::*}
ensure junit test "org.skriptlang.skript.test.tests.syntaxes.expressions.ExprDropsTest" completes {_tests::*}

# NOTE: Do NOT take the behavior described in this test as a guide for how ExprDrops SHOULD work, only for how it DOES work in 2.7.x.
# The behavior should change in 2.8 and this test will be updated accordingly.

on death of pig:
set {_test} to "org.skriptlang.skript.test.tests.syntaxes.ExprDropsTest"
set {_test} to "org.skriptlang.skript.test.tests.syntaxes.expressions.ExprDropsTest"
junit test is {_test}

# Items
Expand Down
44 changes: 44 additions & 0 deletions src/test/skript/junit/ExprMessage.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
test "ExprMessageJUnit" when running JUnit:
set {_tests::1} to "set chat message"
set {_tests::2} to "clear chat message"
set {_tests::3} to "set join message"
set {_tests::4} to "clear join message"
set {_tests::5} to "set quit message"
set {_tests::6} to "clear quit message"
ensure junit test "org.skriptlang.skript.test.tests.syntaxes.expressions.ExprMessageTest" completes {_tests::*}

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

set chat message to "hello"
if chat message is "hello":
complete objective "set chat message" for {_test}

clear message
if message is "":
complete objective "clear chat message" for {_test}

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

set login message to "I joined"
if log in message is "I joined":
complete objective "set join message" for {_test}

clear join message
if join message is "":
complete objective "clear join message" for {_test}

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

set logout message to "I left"
if log out message is "I left":
complete objective "set quit message" for {_test}

clear quit message
if leave message is "":
complete objective "clear quit message" for {_test}

0 comments on commit abc782c

Please sign in to comment.