Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.4.2</version>
<version>2.6.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.8</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
63 changes: 63 additions & 0 deletions src/main/java/me/vemacs/executeeverywhere/bukkit/EECommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package me.vemacs.executeeverywhere.bukkit;

import com.google.common.base.Joiner;
import lombok.RequiredArgsConstructor;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisConnectionException;

import java.util.Arrays;

@RequiredArgsConstructor
public class EECommand implements CommandExecutor {
private final ExecuteEverywhere plugin;

@Override
public boolean onCommand(final CommandSender commandSender, Command command, String s, String[] args) {
if (args.length < 1 || (command.getName().equalsIgnoreCase("eeg") && args.length < 2)) {
commandSender.sendMessage(ChatColor.RED + "Invalid usage.");
if (command.getName().equalsIgnoreCase("eec")) {
commandSender.sendMessage(ChatColor.RED + "/" + s + " <group> <command>");
} else {
commandSender.sendMessage(ChatColor.RED + "/" + s + " <command>");
}
return true;
}

final String channel;
final String run;

if (command.getName().equals("eeg")) {
channel = "ee-" + args[0];
run = Joiner.on(' ').join(Arrays.copyOfRange(args, 1, args.length));
} else if (command.getName().equals("ee")) {
channel = "ee";
run = Joiner.on(' ').join(args);
commandSender.sendMessage(ChatColor.GRAY + "(Assuming you want to run this command over all Bukkit servers.)");
} else if (command.getName().equals("eb")) {
channel = "eb";
run = Joiner.on(' ').join(args);
commandSender.sendMessage(ChatColor.GRAY + "(Assuming you want to run this command over all BungeeCord servers.)");
} else {
// Shouldn't happen
return false;
}

plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
try (Jedis jedis = plugin.getPool().getResource()) {
jedis.publish(channel, run);
commandSender.sendMessage(ChatColor.GREEN + "Command successfully queued for execution.");
} catch (JedisConnectionException e) {
commandSender.sendMessage(ChatColor.RED + "Could not send the command! Please try again.");
}
}
});

return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,55 +1,41 @@
package me.vemacs.executeeverywhere.bukkit;

import com.google.common.base.Joiner;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import lombok.Getter;
import me.vemacs.executeeverywhere.common.AbstractJedisPubSub;
import me.vemacs.executeeverywhere.common.EEConfiguration;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;

import java.lang.Override;
import java.util.List;

public class ExecuteEverywhere extends JavaPlugin implements Listener {
@Getter
private JedisPool pool;
private static final Joiner joiner = Joiner.on(" ");
private final String CHANNEL = "ee";
private final String BUNGEE_CHANNEL = "eb";
private static Plugin instance;
@Getter
private EEConfiguration configuration;
private EESubscriber eeSubscriber;

@Override
public void onEnable() {
instance = this;
saveDefaultConfig();

String ip = getConfig().getString("ip");
int port = getConfig().getInt("port");
String password = getConfig().getString("password");
if (password == null || password.equals(""))
pool = new JedisPool(new JedisPoolConfig(), ip, port, 0);
else
pool = new JedisPool(new JedisPoolConfig(), ip, port, 0, password);
new BukkitRunnable() {
@Override
public void run() {
eeSubscriber = new EESubscriber();
Jedis jedis = pool.getResource();
try {
jedis.subscribe(eeSubscriber, CHANNEL);
} catch (Exception e) {
e.printStackTrace();
pool.returnBrokenResource(jedis);
getLogger().severe("Unable to connect to Redis server.");
return;
}
pool.returnResource(jedis);
}
}.runTaskAsynchronously(this);
List<String> serverGroups = getConfig().getStringList("groups");
configuration = new EEConfiguration(serverGroups, ip, port, password);

pool = configuration.getJedisPool();

getServer().getScheduler().runTaskAsynchronously(this, eeSubscriber = new EESubscriber());

EECommand command = new EECommand(this);
getCommand("ee").setExecutor(command);
getCommand("eb").setExecutor(command);
getCommand("eeg").setExecutor(command);
}

@Override
Expand All @@ -58,66 +44,21 @@ public void onDisable() {
pool.destroy();
}

@Override
public boolean onCommand(CommandSender sender, final Command cmd, String label, String[] args) {
if (args.length == 0) return false;
String cmdString = joiner.join(args);
if (cmdString.startsWith("/"))
cmdString = cmdString.substring(1);
final String finalCmdString = cmdString;
new BukkitRunnable() {
@Override
public void run() {
Jedis jedis = pool.getResource();
try {
switch (cmd.getName().toLowerCase()) {
case "eb":
jedis.publish(BUNGEE_CHANNEL, finalCmdString);
break;
default:
jedis.publish(CHANNEL, finalCmdString);
}
} catch (Exception e) {
pool.returnBrokenResource(jedis);
}
pool.returnResource(jedis);
}
}.runTaskAsynchronously(this);
sender.sendMessage(ChatColor.GREEN + "Sent /" + cmdString + " for execution.");
return true;
}
public class EESubscriber extends AbstractJedisPubSub {
public EESubscriber() {
super(pool, configuration);
}

public class EESubscriber extends JedisPubSub {
@Override
public void onMessage(String channel, final String msg) {
// Needs to be done in the server thread
new BukkitRunnable() {
@Override
public void run() {
ExecuteEverywhere.instance.getLogger().info("Dispatching /" + msg);
getLogger().info("Dispatching /" + msg);
getServer().dispatchCommand(getServer().getConsoleSender(), msg);
}
}.runTaskAsynchronously(ExecuteEverywhere.instance);
}

@Override
public void onPMessage(String s, String s2, String s3) {
}

@Override
public void onSubscribe(String s, int i) {
}

@Override
public void onUnsubscribe(String s, int i) {
}

@Override
public void onPUnsubscribe(String s, int i) {
}

@Override
public void onPSubscribe(String s, int i) {
}.runTaskAsynchronously(ExecuteEverywhere.this);
}
}
}
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/me/vemacs/executeeverywhere/bungee/EECommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package me.vemacs.executeeverywhere.bungee;

import com.google.common.base.Joiner;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.plugin.Command;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisConnectionException;

import java.util.Arrays;

public class EECommand extends Command {
private final ExecuteEverywhere plugin;
private final EECommandKind kind;

public EECommand(ExecuteEverywhere plugin, String name, EECommandKind kind) {
super(name, "executeeverywhere.use");
this.kind = kind;
this.plugin = plugin;
}

@Override
public void execute(final CommandSender commandSender, String[] args) {
if (args.length < 1 || (kind == EECommandKind.GROUP && args.length < 2)) {
commandSender.sendMessage(ChatColor.RED + "Invalid usage.");
if (kind == EECommandKind.GROUP) {
commandSender.sendMessage(ChatColor.RED + "/" + getName() + " <group> <command>");
} else {
commandSender.sendMessage(ChatColor.RED + "/" + getName() + " <command>");
}
return;
}

String group = args[0];
final String channel;
final String run;

switch (kind) {
case BUNGEECORD:
channel = "eb";
run = Joiner.on(' ').join(args);
commandSender.sendMessage(ChatColor.GRAY + "(Assuming you want to run this command over all BungeeCord servers.)");
break;
case BUKKIT:
channel = "ee";
run = Joiner.on(' ').join(args);
commandSender.sendMessage(ChatColor.GRAY + "(Assuming you want to run this command over all Bukkit servers.)");
break;
case GROUP:
channel = "ee-" + group;
run = Joiner.on(' ').join(Arrays.copyOfRange(args, 1, args.length));
break;
default:
return;
}

plugin.getProxy().getScheduler().runAsync(plugin, new Runnable() {
@Override
public void run() {
try (Jedis jedis = plugin.getPool().getResource()) {
jedis.publish(channel, run);
commandSender.sendMessage(ChatColor.GREEN + "Command successfully queued for execution.");
} catch (JedisConnectionException e) {
commandSender.sendMessage(ChatColor.RED + "Could not send the command! Please try again.");
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.vemacs.executeeverywhere.bungee;

public enum EECommandKind {
BUNGEECORD,
BUKKIT,
GROUP
}
Loading