Skip to content

Commit

Permalink
Added forward command from Bukkit to BungeeCord
Browse files Browse the repository at this point in the history
  • Loading branch information
games647 committed Jul 5, 2016
1 parent 2a06c44 commit 92ed029
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 84 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#Changelog

##### 1.11

* Fix command support if the command only runs on the backend server

##### 1.10

* Fix NPE on skinupdate bungeecord
Expand Down
2 changes: 1 addition & 1 deletion bukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github.games647</groupId>
<artifactId>changeskin</artifactId>
<version>1.10</version>
<version>1.11</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public void onEnable() {
getLogger().warning("Cannot check bungeecord support. You use a non-spigot build");
}

getCommand("setskin").setExecutor(new SetSkinCommand(this));
getCommand("skinupdate").setExecutor(new SkinInvalidateCommand(this));

if (bungeeCord) {
getLogger().info("BungeeCord detected. Activating BungeeCord support");
getLogger().info("Make sure you installed the plugin on BungeeCord too");
Expand Down Expand Up @@ -86,9 +89,6 @@ public void onEnable() {

loadLocale();

getCommand("setskin").setExecutor(new SetSkinCommand(this));
getCommand("skinupdate").setExecutor(new SkinInvalidateCommand(this));

getServer().getPluginManager().registerEvents(new PlayerLoginListener(this), this);
getServer().getPluginManager().registerEvents(new AsyncPlayerLoginListener(this), this);
}
Expand Down Expand Up @@ -198,6 +198,10 @@ private void loadLocale() {
}
}

public boolean isBungeeCord() {
return bungeeCord;
}

private <K, V> ConcurrentMap<K, V> buildCache(int seconds, int maxSize) {
SafeCacheBuilder<Object, Object> builder = SafeCacheBuilder.newBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.github.games647.changeskin.bukkit.ChangeSkinBukkit;
import com.github.games647.changeskin.bukkit.tasks.NameResolver;
import com.github.games647.changeskin.bukkit.tasks.SkinDownloader;
import com.google.common.base.Joiner;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;

import java.util.Arrays;
import java.util.UUID;
Expand All @@ -23,6 +26,11 @@ public SetSkinCommand(ChangeSkinBukkit plugin) {

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (plugin.isBungeeCord()) {
onBungeeCord(sender, command.getName(), args);
return true;
}

if (isCooldown(sender)) {
plugin.sendMessage(sender, "cooldown");
return true;
Expand Down Expand Up @@ -93,4 +101,19 @@ private void setSkinUUID(CommandSender sender, Player receiverPayer, String targ
plugin.sendMessage(sender, "invalid-uuid");
}
}

private void onBungeeCord(CommandSender sender, String commandName, String[] args) {
if (!(sender instanceof Player)) {
plugin.sendMessage(sender, "no-console");
return;
}

Player player = (Player) sender;
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("ForwardCmd");
out.writeUTF(commandName);
out.writeUTF(Joiner.on(' ').join(args));

player.sendPluginMessage(plugin, plugin.getName(), out.toByteArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.github.games647.changeskin.bukkit.ChangeSkinBukkit;
import com.github.games647.changeskin.bukkit.tasks.SkinInvalidater;
import com.google.common.base.Joiner;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
Expand All @@ -19,6 +22,11 @@ public SkinInvalidateCommand(ChangeSkinBukkit plugin) {

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (plugin.isBungeeCord()) {
onBungeeCord(sender, command.getName(), args);
return true;
}

if (args.length > 0) {
Player targetPlayer = Bukkit.getPlayerExact(args[0]);
if (targetPlayer == null) {
Expand All @@ -40,4 +48,19 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new SkinInvalidater(plugin, sender, receiver));
return true;
}

private void onBungeeCord(CommandSender sender, String commandName, String[] args) {
if (!(sender instanceof Player)) {
plugin.sendMessage(sender, "no-console");
return;
}

Player player = (Player) sender;
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("ForwardCmd");
out.writeUTF(commandName);
out.writeUTF(Joiner.on(' ').join(args));

player.sendPluginMessage(plugin, plugin.getName(), out.toByteArray());
}
}
2 changes: 1 addition & 1 deletion bungee/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github.games647</groupId>
<artifactId>changeskin</artifactId>
<version>1.10</version>
<version>1.11</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.github.games647.changeskin.bungee.listener.DisconnectListener;
import com.github.games647.changeskin.bungee.listener.JoinListener;
import com.github.games647.changeskin.bungee.listener.LoginListener;
import com.github.games647.changeskin.bungee.listener.PermissionsMessageListener;
import com.github.games647.changeskin.bungee.listener.PluginMessageListener;
import com.github.games647.changeskin.bungee.listener.ServerSwitchListener;
import com.github.games647.changeskin.bungee.tasks.SkinUpdater;
import com.github.games647.changeskin.core.ChangeSkinCore;
Expand Down Expand Up @@ -105,7 +105,7 @@ public void onEnable() {

//this is required to listen to messages from the server
getProxy().registerChannel(getDescription().getName());
getProxy().getPluginManager().registerListener(this, new PermissionsMessageListener(this));
getProxy().getPluginManager().registerListener(this, new PluginMessageListener(this));

getProxy().getPluginManager().registerCommand(this, new SetSkinCommand(this));
getProxy().getPluginManager().registerCommand(this, new SkinInvalidateCommand(this));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.github.games647.changeskin.bungee.listener;

import com.github.games647.changeskin.bungee.ChangeSkinBungee;
import com.github.games647.changeskin.core.SkinData;
import com.github.games647.changeskin.core.UserPreference;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;

import java.util.UUID;

import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.event.EventHandler;

public class PluginMessageListener extends AbstractSkinListener {

public PluginMessageListener(ChangeSkinBungee plugin) {
super(plugin);
}

@EventHandler
public void onPluginMessage(PluginMessageEvent messageEvent) {
String channel = messageEvent.getTag();
if (messageEvent.isCancelled() || !plugin.getDescription().getName().equals(channel)) {
return;
}

ByteArrayDataInput dataInput = ByteStreams.newDataInput(messageEvent.getData());
String subChannel = dataInput.readUTF();

ProxiedPlayer invoker = (ProxiedPlayer) messageEvent.getReceiver();
if ("PermissionsSuccess".equals(subChannel)) {
onPermissionSuccess(dataInput, invoker);
} else if ("PermissionsFailure".equals(subChannel)) {
plugin.sendMessage(invoker, "no-permission");
} else if ("ForwardCmd".equals(subChannel)) {
onCommandForward(invoker, dataInput);
}
}

private void onPermissionSuccess(ByteArrayDataInput dataInput, ProxiedPlayer invoker) {
int skinId = dataInput.readInt();

String encodedData = dataInput.readUTF();
String encodedSignature = dataInput.readUTF();
final SkinData targetSkin = new SkinData(encodedData, encodedSignature);
targetSkin.setSkinId(skinId);

UUID receiverUUID = UUID.fromString(dataInput.readUTF());
ProxiedPlayer receiver = ProxyServer.getInstance().getPlayer(receiverUUID);
if (receiver == null || !receiver.isConnected()) {
//receiver is not online cancel
return;
}

//add cooldown
plugin.addCooldown(invoker.getUniqueId());
//Save the target uuid from the requesting player source
final UserPreference preferences = plugin.getStorage().getPreferences(receiver.getUniqueId());
preferences.setTargetSkin(targetSkin);

ProxyServer.getInstance().getScheduler().runAsync(plugin, new Runnable() {
@Override
public void run() {
if (plugin.getStorage().save(targetSkin)) {
plugin.getStorage().save(preferences);
}
}
});

if (plugin.getConfig().getBoolean("instantSkinChange")) {
plugin.applySkin(receiver, targetSkin);
plugin.sendMessage(invoker, "skin-changed");
} else {
plugin.sendMessage(invoker, "skin-changed-no-instant");
}
}

private void onCommandForward(ProxiedPlayer invoker, ByteArrayDataInput dataInput) {
String commandName = dataInput.readUTF();
String args = dataInput.readUTF();
ProxyServer.getInstance().getPluginManager().dispatchCommand(invoker, commandName + ' ' + args);
}
}
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github.games647</groupId>
<artifactId>changeskin</artifactId>
<version>1.10</version>
<version>1.11</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<packaging>pom</packaging>

<name>ChangeSkin</name>
<version>1.10</version>
<version>1.11</version>
<inceptionYear>2015</inceptionYear>
<url>http://dev.bukkit.org/bukkit-plugins/changeskin/</url>
<description>
Expand Down
2 changes: 1 addition & 1 deletion sponge/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github.games647</groupId>
<artifactId>changeskin</artifactId>
<version>1.10</version>
<version>1.11</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion universal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github.games647</groupId>
<artifactId>changeskin</artifactId>
<version>1.10</version>
<version>1.11</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down

0 comments on commit 92ed029

Please sign in to comment.