Skip to content

Commit

Permalink
A new executor, and a renewed executor (#195)
Browse files Browse the repository at this point in the history
* Fix for #TP not allowing other arguments than x,y,z coordinates

* New executor, #KICK

This #KICK Executor simply use to kick player.

1) kick sender - Not giving any argument
#KICK

2) kick sender with custom kick message - giving only one argument
#KICK "You are kicked"

3) kick specific player - giving only one argument
#KICK player("Pro_Snape")

4) kick specific player with custom kick message - giving two arguments
#KICK player("Pro_Snape") "You are kicked lmao"

Tested.

* Made #CLEARCHAT to accept Player instance as arg.

This commit makes #CLEARCHAT able to accept Player instance.

**1)** Clear specific player's chat

    #CLEARCHAT player("Pro_Snape")

* error fixes

print error if there are more than  two arguments.

* error fixes

add validation check when args's length is more than 2.

* Add some test of KICK Executor

same as title.

* error fixes

same as title.

* error fixes

just knew eq() and anyString() don't work, also some elses.

* some errors keep fixed

some error fixed.
Mokito -> Mockito

* one more errror.

same as last commit, 

Mokito -> Mockito

* Final KICK.js error fix

final one

* final KICK.js test script fix

same as title.

* Actually this js should be built after the test script

same as title.

* final CLEARCHAT.js test script

same as title

* times() is static one, fixed error

same as title

* add some more test, also fixed what gerzytet said.

same as title.

* fix what gerzytet said.

fix == to ===.

* Error fixes

* Added one more test for ClearChat

same as title.

* Actually OfflinePlayer does not needed to be imported.

* add some var prefix, and fixed stray == to ===

same as title.

* added var prefix

same as title.
  • Loading branch information
rkdrns4747 authored and wysohn committed Sep 18, 2019
1 parent febd1c7 commit 0ae0cac
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 10 deletions.
31 changes: 23 additions & 8 deletions bukkit/src/main/resources/Executor/CLEARCHAT.js
Expand Up @@ -15,11 +15,26 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
function CLEARCHAT(args){
if(player == null)
return null;

for(var i = 0; i < 30; i++)
player.sendMessage("");

return null;
}
if(player === null){
return null;
}
if(args.length === 0){
for(var i = 0; i < 30; i++){
player.sendMessage("");
}
return null;
}else if(args.length === 1){
var plType = Java.type("org.bukkit.entity.Player")
if(args[0] instanceof plType){
var pl = args[0];
}else {
throw new Error("Found unexpected parameter - player: null")
}
for(var i = 0; i < 30; i++){
pl.sendMessage("");
}
return null;
}else if(args.length >= 2){
throw new Error("Too many parameters found! CLEARCHAT accept up to one parameter.")
}
}
59 changes: 59 additions & 0 deletions bukkit/src/main/resources/Executor/KICK.js
@@ -0,0 +1,59 @@
/*******************************************************************************
* Copyright (C) 2018 wysohn
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
function KICK(args) {
var plType = Java.type("org.bukkit.entity.Player")
if(args.length === 0){
if(player === null){
throw new Error("Too few arguments! You should enter at least on argument if you use KICK executor from console.")
}else {
player.kickPlayer(ChatColor.translateAlternateColorCodes('&', "&c[TR] You've been kicked from the server."));
return null;
}
}else if(args.length === 1) {
var undefinedArgument = args[0]
if(undefinedArgument === null)
throw new Error("Unexpected Error: parameter does not match - player: null")

if(args[0] instanceof plType) {
var definedToPlayer = undefinedArgument;
definedToPlayer.kickPlayer(ChatColor.translateAlternateColorCodes(Char('&'), "&c[TR] You've been kicked from the server."));
return null;
} else if(typeof undefinedArgument === "string"){
var msg = undefinedArgument;
player.kickPlayer(ChatColor.translateAlternateColorCodes(Char('&'), msg));
return null;
}else {
throw new Error("Found unexpected type of argument: "+ undefinedArgument);
}
}else if(args.length === 2) {
var pl = args[0]
var str = args[1]
if(!(pl instanceof plType) || !(typeof str === "string")){
throw new Error("Found unexpected type of argument(s) - player: "+pl+" | msg: "+ str)
}else {
pl.kickPlayer(ChatColor.translateAlternateColorCodes(Char('&'), str))
return null;
}
}else if(args.length > 2){
throw new Error("Too many arguments! KICK Executor accepts up to two arguments.")
}

}




56 changes: 54 additions & 2 deletions bukkit/src/test/java/js/executor/AbstractTestExecutors.java
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.entity.Player;
import org.junit.Test;
import org.mockito.Mockito;
import static org.mockito.Mockito.times;
import org.powermock.api.mockito.PowerMockito;
import java.util.Collection;
import static io.github.wysohn.triggerreactor.core.utils.TestUtil.*;
Expand Down Expand Up @@ -143,7 +144,22 @@ public void testBurn() throws Exception{

@Test
public void testClearChat() throws Exception{

Player vp = Mockito.mock(Player.class);
Player vp2 = Mockito.mock(Player.class);
Player nullP = null;
JsTest test = new ExecutorTest(engine, "CLEARCHAT").addVariable("player", vp);

//case1
test.withArgs().test();
Mockito.verify(vp, times(30)).sendMessage("");

//case2
test.withArgs(vp2).test();
Mockito.verify(vp2, times(30)).sendMessage("");

//Unexpected Cases
assertError(() -> test.withArgs(nullP).test(), "Found unexpected parameter - player: null");
assertError(() -> test.withArgs(1, 2).test(), "Too many parameters found! CLEARCHAT accept up to one parameter.");
}

@Test
Expand Down Expand Up @@ -364,7 +380,7 @@ public void testWeather() throws Exception{
JsTest test = new ExecutorTest(engine, "WEATHER");
World mockWorld = Mockito.mock(World.class);
PowerMockito.when(Bukkit.class, "getWorld", "merp").thenReturn(mockWorld);

test.withArgs("merp", true).test();
Mockito.verify(mockWorld).setStorm(true);

Expand All @@ -374,5 +390,41 @@ public void testWeather() throws Exception{
assertError(() -> test.withArgs(mockWorld, false).test(), "Invalid parameters! [String, Boolean]");
assertError(() -> test.withArgs("merp", true).test(), "Unknown world named merp");
}
@Test
public void testKick() throws Exception{

Player vp = Mockito.mock(Player.class);
Player vp2 = Mockito.mock(Player.class);
Player nullP = null;
String msg = ChatColor.translateAlternateColorCodes('&', "&c[TR] You've been kicked from the server.");
String msg2 = ChatColor.translateAlternateColorCodes('&', "&cKICKED");

//case1
JsTest test = new ExecutorTest(engine, "KICK").addVariable("player", vp);
test.withArgs().test();
Mockito.verify(vp).kickPlayer(msg);

//case2
test.withArgs(msg2).test();
Mockito.verify(vp).kickPlayer(msg2);

//case3
test.withArgs(vp2).test();
Mockito.verify(vp2).kickPlayer(msg);

//case4
test.withArgs(vp2, msg2).test();
Mockito.verify(vp2).kickPlayer(msg2);

//Unexpected Exception Cases
assertError(() -> test.withArgs(1).test(), "Found unexpected type of argument: 1");
assertError(() -> test.withArgs(vp, 232).test(), "Found unexpected type of argument(s) - player: "+vp+" | msg: 232");
assertError(() -> test.withArgs(1 , 2 , 3).test(), "Too many arguments! KICK Executor accepts up to two arguments.");
test.addVariable("player", null);
assertError(() -> test.withArgs().test(), "Too few arguments! You should enter at least on argument if you use KICK executor from console.");
assertError(() -> test.withArgs(null, "msg").test(), "Found unexpected type of argument(s) - player: null | msg: msg");
assertError(() -> test.withArgs(nullP).test(), "Unexpected Error: parameter does not match - player: null");
}


}

0 comments on commit 0ae0cac

Please sign in to comment.