-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from ravenlab/bungeecord
Add bungeecord support
- Loading branch information
Showing
21 changed files
with
1,096 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...main/java/com/github/ravenlab/commander/command/platform/bungeecord/BungeeCommandMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../java/com/github/ravenlab/commander/command/platform/bungeecord/BungeeCommandWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/github/ravenlab/commander/command/platform/bungeecord/BungeeCommander.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...java/com/github/ravenlab/commander/command/platform/bungeecord/BungeeCommanderPlayer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...java/com/github/ravenlab/commander/command/platform/bungeecord/BungeeCommanderSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...in/java/com/github/ravenlab/commander/command/platform/bungeecord/BungeeTypeResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
src/test/java/com/github/commander/test/bungeecord/BungeeCommandMapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.