Skip to content

Commit

Permalink
Start working on sponge support
Browse files Browse the repository at this point in the history
  • Loading branch information
virustotalop committed Sep 25, 2020
1 parent 7519465 commit a773cc2
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ repositories {
jcenter()
maven { url "https://hub.spigotmc.org/nexus/content/repositories/snapshots" }
maven { url = "https://oss.sonatype.org/content/repositories/snapshots" }
maven { url = 'https://repo.spongepowered.org/maven' }
}

configurations {
Expand All @@ -40,6 +41,7 @@ dependencies {
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'
compileOnly 'org.spongepowered:spongeapi:7.2.0'
}

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

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,61 @@
package com.github.ravenlab.commander.platform.sponge;

import java.util.List;
import java.util.Optional;

import org.spongepowered.api.command.CommandCallable;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

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

public class SpongeCommandWrapper implements CommandCallable {

private CommanderExecutor<CommandSource> executor;

public SpongeCommandWrapper(CommanderCommand<CommandSource> command) {
this.executor = new CommanderExecutor<>(command, new SpongeTypeResolver());
}

@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
// TODO Auto-generated method stub
return null;
}

@Override
public List<String> getSuggestions(CommandSource source, String arguments, Location<World> targetPosition)
throws CommandException {
// TODO Auto-generated method stub
return null;
}

@Override
public boolean testPermission(CommandSource source) {
// TODO Auto-generated method stub
return false;
}

@Override
public Optional<Text> getShortDescription(CommandSource source) {
// TODO Auto-generated method stub
return null;
}

@Override
public Optional<Text> getHelp(CommandSource source) {
// TODO Auto-generated method stub
return null;
}

@Override
public Text getUsage(CommandSource source) {
// TODO Auto-generated method stub
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.github.ravenlab.commander.platform.sponge;

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

import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.plugin.PluginContainer;

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

public class SpongeCommander extends Commander<PluginContainer, CommandExecutor, CommandSource>{

private Map<String, CommandExecutor> knownCommands;

//Look at https://github.com/SpongePowered/SpongeCommon/blob/cd424add778a1059f036c052c3f685619305d001/src/main/java/org/spongepowered/common/command/SpongeCommandManager.java

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

@Override
protected Optional<String> registerAlias(PluginContainer plugin, CommandExecutor 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 CommandExecutor createCommandWrapper(CommandData data, CommanderCommand<CommandSource> command) {
return new SpongeCommandWrapper(command);
}

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

import java.util.UUID;

import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.text.Text;

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

public class SpongeCommanderPlayer extends CommanderPlayer<Player> {

public SpongeCommanderPlayer(Player 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);
}

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

import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.text.Text;

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

public class SpongeCommanderSender extends CommanderSender<CommandSource> {

public SpongeCommanderSender(CommandSource sender) {
super(sender);
}

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

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

@Override
public void sendMessage(String message) {
this.getNative().sendMessages(Text.of(message));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.ravenlab.commander.platform.sponge;

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

import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.world.World;

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

public class SpongeTypeResolver implements TypeResolver<Player, World> {

@Override
public Optional<Player> getPlayer(String name) {
return Sponge.getServer().getPlayer(name);
}

@Override
public Optional<Player> getPlayer(UUID uuid) {
return Sponge.getServer().getPlayer(uuid);
}

@Override
public Optional<World> getWorld(String name) {
return Sponge.getServer().getWorld(name);
}

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

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

@Override
public Optional<Class<World>> getWorldClass() {
return Optional.of(World.class);
}
}

0 comments on commit a773cc2

Please sign in to comment.