Skip to content

Commit

Permalink
MAJOR
Browse files Browse the repository at this point in the history
 SpigotTester is not longer loaded in the POSTWORD (onLoad) method since
 Bukkit not allow to register events/commands before plugin is fully loaded.
 SpiogtTester will be waiting for all others plugins to be loaded then launch tests

  SpigotTesterSetup new interface
    could be applied on JavaPlugin class
    contains onSpigotTesterSetup(TestRunnerBuilder builder) method
    to configure tests and inject parameters
    by parameters I mean objects that will be available during the tests

  SpigotTest has been changed from Interface to abstract Class
  SpigotTest class offers
   - creating fake players
   - asserting objects
   - getting parameters via constructor or getParameter(Class<T> clazz) method

   Fake players
     can be created via method SpigotTest::addPlayer() after adding
     fake player automatically join to server when tests for certain class
     will be done player is automatically disconnected
  • Loading branch information
jwdeveloper committed Feb 15, 2023
1 parent 6f9c358 commit f36f663
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 54 deletions.
2 changes: 1 addition & 1 deletion ExamplePluginToTest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
<dependency>
<groupId>io.github.jwdeveloper.spigot.tester</groupId>
<artifactId>spigot-tester</artifactId>
<version>0.3.2</version>
<version>0.2.13-Release</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down
11 changes: 9 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@
</build>

<repositories>

<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
Expand All @@ -98,7 +105,7 @@
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<artifactId>spigot-api</artifactId>
<version>1.19-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package io.github.jwdeveloper.spigot.tester.implementation.players;

import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class FakePlayer {
Expand All @@ -40,7 +41,16 @@ public FakePlayer(Player player, Object entityPlayerHandle, NmsCommunicator comm
}

public void connect() {
communicator.connect(handle);
try
{
communicator.connect(handle);
}
catch (Exception e)
{
Bukkit.getConsoleSender().sendMessage("UNABLE TO CONNECT FAKE PLAYER");
e.printStackTrace();
}

}

public void disconnect() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@

package io.github.jwdeveloper.spigot.tester.implementation.players;

import com.mojang.authlib.GameProfile;
import io.github.jwdeveloper.reflect.implementation.FluentReflect;
import io.github.jwdeveloper.reflect.implementation.models.JavaConstructorModel;
import io.github.jwdeveloper.reflect.implementation.models.JavaMethodModel;
import io.github.jwdeveloper.spigot.tester.plugin.PluginMain;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.protocol.EnumProtocolDirection;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

Expand All @@ -45,8 +42,12 @@ public class NmsCommunicator {
private final JavaConstructorModel CTR_CRAFT_PLAYER;
private final JavaMethodModel M_CONNECT_PLAYER;
private final JavaMethodModel M_DISCONNECT_PLAYER;
private final JavaConstructorModel CTR_GAMEPROFILE;
private final JavaConstructorModel CTR_NETWORKMANAGER;

public NmsCommunicator(FluentReflect fluentReflect) throws IllegalAccessException {
private Object PROTOCOL_ENUM_VALUE;

public NmsCommunicator(FluentReflect fluentReflect) throws IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {


var C_CRAFT_SERVER = Bukkit.getServer().getClass();
Expand All @@ -71,7 +72,18 @@ public NmsCommunicator(FluentReflect fluentReflect) throws IllegalAccessExceptio

SERVER_WORLD = F_WORLD.getValue(Bukkit.getWorlds().get(0));

var C_NETWORK_MANAGER = fluentReflect.findClass().forAnyVersion(finder ->
{
finder.withName("net.minecraft.network.NetworkManager");
}).find();

CTR_NETWORKMANAGER = C_NETWORK_MANAGER.findConstructor().forAnyVersion(finder ->
{
finder.withParameter("net.minecraft.network.protocol.EnumProtocolDirection");
}).find();


// Entity Player
var C_ENTITY_PLAYER = fluentReflect.findClass().forAnyVersion(finder ->
{
finder.withName("net.minecraft.server.level.EntityPlayer");
Expand All @@ -81,12 +93,13 @@ public NmsCommunicator(FluentReflect fluentReflect) throws IllegalAccessExceptio
{
finder.withPublic();
finder.withParameterCount(3);
}).forVersion("v1_19_R1",finder ->
}).forVersion("v1_19_R1", finder ->
{
finder.withParameterCount(4);
finder.withParameterMatcher(input -> new Object[]{input[0], input[1],input[2], null});
finder.withParameterMatcher(input -> new Object[]{input[0], input[1], input[2], null});
}).find();

//Craft Player
var C_CRAFT_PLAYER = fluentReflect.findClass().forAnyVersion(finder ->
{
finder.withName("org.bukkit.craftbukkit." + PluginMain.getVersion() + ".entity.CraftPlayer");
Expand All @@ -99,6 +112,7 @@ public NmsCommunicator(FluentReflect fluentReflect) throws IllegalAccessExceptio
finder.withParameterCount(2);
}).find();

//PlayerList
var F_PLAYER_LIST = fluentReflect.findField(Bukkit.getServer().getClass())
.forAnyVersion(finder ->
{
Expand All @@ -111,7 +125,7 @@ public NmsCommunicator(FluentReflect fluentReflect) throws IllegalAccessExceptio
M_CONNECT_PLAYER = fluentReflect.findMethod(PLAYER_LIST.getClass()).forAnyVersion(finder ->
{
finder.withPublic()
.withParameter(NetworkManager.class)
.withParameter(C_NETWORK_MANAGER.getClassType())
.withParameter(C_ENTITY_PLAYER.getClassType());
}).find();

Expand All @@ -125,18 +139,34 @@ public NmsCommunicator(FluentReflect fluentReflect) throws IllegalAccessExceptio
{
finder.withName("disconnect");
}).find();
}

//GameProfile
CTR_GAMEPROFILE = fluentReflect.findClass().forAnyVersion(finder ->
{
finder.withName("com.mojang.authlib.GameProfile");
})
.find()
.findConstructor()
.forAnyVersion(finder ->
{
finder.withParameterCount(2);
}).find();

public void connect(Object entityPlayer) {
var manager = new NetworkManager(EnumProtocolDirection.a);
try {
M_CONNECT_PLAYER.invoke(PLAYER_LIST, manager, entityPlayer);
} catch (Exception e) {
e.printStackTrace();
var E_PROTOCOL_DIRECTION = fluentReflect.findClass().forAnyVersion(finder ->
{
finder.withName("net.minecraft.network.protocol.EnumProtocolDirection");
}).find().getClassType();
for (Object obj : E_PROTOCOL_DIRECTION.getEnumConstants()) {
PROTOCOL_ENUM_VALUE = obj;
}
}


public void connect(Object entityPlayer) throws InvocationTargetException, IllegalAccessException, InstantiationException {
var networkManager = CTR_NETWORKMANAGER.newInstance(PROTOCOL_ENUM_VALUE);
M_CONNECT_PLAYER.invoke(PLAYER_LIST, networkManager, entityPlayer);
}

public void disconnect(Object entityPlayer) {
try {
M_DISCONNECT_PLAYER.invoke(PLAYER_LIST, entityPlayer);
Expand All @@ -146,8 +176,8 @@ public void disconnect(Object entityPlayer) {
}

public FakePlayer createFakePlayer(UUID uuid, String name) throws InvocationTargetException, IllegalAccessException, InstantiationException {
GameProfile profile = new GameProfile(uuid, name);
Object entityPlayer = CTR_ENTITY_PLAYER.newInstance(MINECRAFT_SERVER, SERVER_WORLD, profile);
Object gameProfile = CTR_GAMEPROFILE.newInstance(uuid, name);
Object entityPlayer = CTR_ENTITY_PLAYER.newInstance(MINECRAFT_SERVER, SERVER_WORLD, gameProfile);
Player player = CTR_CRAFT_PLAYER.newInstance(Bukkit.getServer(), entityPlayer);
var fakePlayer = new FakePlayer(player, entityPlayer, this);
return fakePlayer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.lang.reflect.InvocationTargetException;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.function.Consumer;
Expand Down Expand Up @@ -102,7 +103,7 @@ private Optional<NmsCommunicator> createNmsCommunicator() {
return Optional.of(nmsCommunicator);
} catch (ValidationException e) {
ValidationExceptionDisplay.showError(e);
} catch (IllegalAccessException e) {
} catch (Exception e) {
e.printStackTrace();
}
return Optional.empty();
Expand Down
33 changes: 0 additions & 33 deletions src/test/java/Dupa.java

This file was deleted.

0 comments on commit f36f663

Please sign in to comment.