Skip to content

Commit

Permalink
Merge pull request #14 from ravenlab/bungeecord
Browse files Browse the repository at this point in the history
Add bungeecord support
  • Loading branch information
virustotalop authored Aug 5, 2020
2 parents 075725d + 14b4c69 commit 4f0eb8d
Show file tree
Hide file tree
Showing 21 changed files with 1,096 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ dependencies {
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:3.3.3'
testImplementation 'org.spigotmc:spigot-api:1.12.2-R0.1-SNAPSHOT'
testImplementation 'net.md-5:bungeecord-api:1.12-SNAPSHOT'
compileOnly 'org.spigotmc:spigot-api:1.12.2-R0.1-SNAPSHOT'
compileOnly 'net.md-5:bungeecord-api:1.12-SNAPSHOT'
}

jacocoTestReport {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.ravenlab.commander.command.platform.bungeecord;

import java.lang.reflect.Field;
import java.util.Map;

import net.md_5.bungee.api.plugin.Command;

public class BungeeCommandMap {

@SuppressWarnings("unchecked")
public Map<String, Command> getMapIfExists(Object manager) {
try {
Field mapField = manager.getClass().getDeclaredField("commandMap");
mapField.setAccessible(true);
return (Map<String, Command>) mapField.get(manager);
} catch (SecurityException | IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.ravenlab.commander.command.platform.bungeecord;

import com.github.ravenlab.commander.command.CommanderCommand;
import com.github.ravenlab.commander.command.CommanderExecutor;
import com.github.ravenlab.commander.sender.CommanderSender;

import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;

public class BungeeCommandWrapper extends Command {

private CommanderExecutor<CommandSender> executor;

public BungeeCommandWrapper(CommanderCommand<CommandSender> command) {
super(command.getData().get().getName());
this.executor = new CommanderExecutor<>(command, new BungeeTypeResolver());
}

@Override
public void execute(CommandSender proxySender, String[] args) {
CommanderSender<?> commanderSender = null;
if(proxySender instanceof ProxiedPlayer) {
ProxiedPlayer player = (ProxiedPlayer) proxySender;
commanderSender = new BungeeCommanderPlayer(player);
} else {
commanderSender = new BungeeCommanderSender(proxySender);
}
this.executor.execute(commanderSender, this.getName(), args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.github.ravenlab.commander.command.platform.bungeecord;


import java.util.Map;
import java.util.Optional;

import com.github.ravenlab.commander.Commander;
import com.github.ravenlab.commander.command.CommandData;
import com.github.ravenlab.commander.command.CommanderCommand;

import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.Plugin;

public class BungeeCommander extends Commander<Plugin, Command, CommandSender>{

private Map<String, Command> knownCommands;

public BungeeCommander() {
this.knownCommands = new BungeeCommandMap()
.getMapIfExists(ProxyServer.getInstance().getPluginManager());
}

@Override
protected Optional<String> registerAlias(Plugin plugin, Command command, String alias, boolean forceRegister) {
String registeredAlias = alias;

if(this.knownCommands.containsKey(alias) && !forceRegister) {
registeredAlias = this.getPluginName(plugin).toLowerCase() + ":" + alias;
}

this.knownCommands.put(registeredAlias, command);
return Optional.of(registeredAlias);
}

@Override
protected boolean unregisterAlias(String command) {
return this.knownCommands.remove(command) != null;
}

@Override
protected Command createCommandWrapper(CommandData data, CommanderCommand<CommandSender> command) {
return new BungeeCommandWrapper(command);
}

@Override
protected String getPluginName(Plugin plugin) {
return plugin.getDescription().getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.ravenlab.commander.command.platform.bungeecord;

import java.util.UUID;

import com.github.ravenlab.commander.player.CommanderPlayer;

import net.md_5.bungee.api.connection.ProxiedPlayer;

public class BungeeCommanderPlayer extends CommanderPlayer<ProxiedPlayer> {

public BungeeCommanderPlayer(ProxiedPlayer player) {
super(player);
}

@Override
public UUID getUniqueId() {
return this.getNative().getUniqueId();
}

@Override
public String getName() {
return this.getNative().getName();
}

@Override
public boolean hasPermission(String permission) {
return this.getNative().hasPermission(permission);
}

@SuppressWarnings("deprecation")
@Override
public void sendMessage(String message) {
this.getNative().sendMessage(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.ravenlab.commander.command.platform.bungeecord;

import com.github.ravenlab.commander.sender.CommanderSender;

import net.md_5.bungee.api.CommandSender;

public class BungeeCommanderSender extends CommanderSender<CommandSender> {

public BungeeCommanderSender(CommandSender sender) {
super(sender);
}

@Override
public String getName() {
return this.getNative().getName();
}

@Override
public boolean hasPermission(String permission) {
return this.getNative().hasPermission(permission);
}

@SuppressWarnings("deprecation")
@Override
public void sendMessage(String message) {
this.getNative().sendMessage(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.github.ravenlab.commander.command.platform.bungeecord;

import java.util.Optional;
import java.util.UUID;

import com.github.ravenlab.commander.resolver.TypeResolver;
import com.github.ravenlab.commander.sender.CommanderSender;

import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;

public class BungeeTypeResolver implements TypeResolver<ProxiedPlayer, Object> {

@Override
public Optional<ProxiedPlayer> getPlayer(String name) {
return Optional.ofNullable(ProxyServer.getInstance().getPlayer(name));
}

@Override
public Optional<ProxiedPlayer> getPlayer(UUID uuid) {
return Optional.ofNullable(ProxyServer.getInstance().getPlayer(uuid));
}

@Override
public Optional<Object> getWorld(String name) {
return Optional.empty();
}

@Override
public CommanderSender<?> getSender(Object nativeSender) {
if(nativeSender instanceof ProxiedPlayer) {
ProxiedPlayer player = (ProxiedPlayer) nativeSender;
return new BungeeCommanderPlayer(player);
} else {
CommandSender commandSender = (CommandSender) nativeSender;
return new BungeeCommanderSender(commandSender);
}
}

@Override
public Class<ProxiedPlayer> getPlayerClass() {
return ProxiedPlayer.class;
}

@Override
public Class<Object> getWorldClass() {
return Object.class;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.github.commander.test.bungeecord;

import static org.junit.Assert.assertTrue;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.junit.Before;
import org.junit.Test;

import com.github.commander.test.bungeecord.mock.BungeeMockFactory;
import com.github.commander.test.bungeecord.mock.MockBungeeServer;
import com.github.ravenlab.commander.command.platform.bungeecord.BungeeCommandMap;

import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.plugin.PluginManager;

public class BungeeCommandMapTest {

private BungeeMockFactory factory;
private MockBungeeServer server;

@Before
public void bootstrapServer() {
this.factory = new BungeeMockFactory();
this.server = this.factory.createServer();
this.setServer(this.server);
}

private void setServer(ProxyServer server) {
try {
Field serverField = ProxyServer.class.getDeclaredField("instance");
serverField.setAccessible(true);
serverField.set(null, server);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}

@Test
public void invalidGetMapIfExists() {
BungeeCommandMap map = new BungeeCommandMap();
assertTrue(map.getMapIfExists("doesnotexist") == null);
}

@Test
public void validGetMapIfExists() {
BungeeCommandMap map = new BungeeCommandMap();
assertTrue(map.getMapIfExists(ProxyServer.getInstance().getPluginManager()) != null);
}

@Test
public void validGetKnownCommands() {
BungeeCommandMap map = new BungeeCommandMap();
try {
Method getKnownCommands = map.getClass().getDeclaredMethod("getKnownCommands");
getKnownCommands.setAccessible(true);
Object knownCommands = getKnownCommands.invoke(map);
assertTrue(knownCommands != null);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}

@Test
public void validGetCommandMap() {
BungeeCommandMap map = new BungeeCommandMap();
try {
Method getCommandMap = map.getClass().getDeclaredMethod("getCommandMap");
getCommandMap.setAccessible(true);
Object BungeeCommandMap = getCommandMap.invoke(map);
assertTrue(BungeeCommandMap != null);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
Loading

0 comments on commit 4f0eb8d

Please sign in to comment.