Skip to content

Commit

Permalink
Add op permissions forwarding (Fixes #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
games647 committed Jul 26, 2017
1 parent e55a48e commit 85c7e4d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 76 deletions.
4 changes: 2 additions & 2 deletions 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>commandforward</artifactId>
<version>0.2</version>
<version>0.3</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand All @@ -28,7 +28,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.10-R0.1-SNAPSHOT</version>
<version>1.12-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}

if (messageSender == null) {
sender.sendMessage(ChatColor.DARK_RED + "Player not online for fowarding this command");
sender.sendMessage(ChatColor.DARK_RED + "Player not online for forwarding this command");
} else {
dataOutput.writeUTF(args[1]);
dataOutput.writeUTF(Joiner.on(' ').join(Arrays.copyOfRange(args, 2, args.length)));
dataOutput.writeBoolean(sender.isOp());
messageSender.sendPluginMessage(this, getName(), dataOutput.toByteArray());
}
}
Expand Down
12 changes: 2 additions & 10 deletions 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>commandforward</artifactId>
<version>0.2</version>
<version>0.3</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand All @@ -16,19 +16,11 @@
<!--Represents the main plugin-->
<name>CommandForwardBungee</name>

<repositories>
<!--BungeeCord with also the part outside the API-->
<repository>
<id>myplayplanet-REPO</id>
<url>http://maven.myplayplanet.net/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>1.8-SNAPSHOT</version>
<version>1.12-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
package com.github.games647.commandforward.bungee;

import com.google.common.cache.CacheBuilder;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.api.plugin.PluginManager;
import net.md_5.bungee.event.EventHandler;

public class CommandForwardBungee extends Plugin implements Listener {

private static final boolean REMOVE_DUPLICATES = false;

private final ConcurrentMap<String, Object> prevCommands = CacheBuilder.newBuilder()
.expireAfterWrite(10, TimeUnit.SECONDS)
.<String, Object>build().asMap();


@Override
public void onEnable() {
//this is required to listen to messages from the server
Expand All @@ -43,26 +40,49 @@ public void onServerConnected(PluginMessageEvent messageEvent) {

//check if the message is sent from the server
if (Server.class.isAssignableFrom(messageEvent.getSender().getClass())) {
ByteArrayDataInput dataInput = ByteStreams.newDataInput(messageEvent.getData());
boolean isPlayer = dataInput.readBoolean();
String command = dataInput.readUTF();
String arguments = dataInput.readUTF();
parseMessage((ProxiedPlayer) messageEvent.getReceiver(), ByteStreams.newDataInput(messageEvent.getData()));
}
}

ProxiedPlayer invoker = (ProxiedPlayer) messageEvent.getReceiver();
String commandLine = command + ' ' + arguments;
if (REMOVE_DUPLICATES && prevCommands.containsKey(commandLine)) {
return;
}
private void parseMessage(ProxiedPlayer sender, ByteArrayDataInput dataInput) {
boolean isPlayer = dataInput.readBoolean();
String command = dataInput.readUTF();
String arguments = dataInput.readUTF();

CommandSender invoker = getProxy().getConsole();
if (isPlayer) {
invoker = sender;
}

prevCommands.put(commandLine, new Object());
invokeCommand(invoker, dataInput.readBoolean(), command, arguments);
}

private void invokeCommand(CommandSender invoker, boolean isOp, String command, String arguments) {
PluginManager pluginManager = getProxy().getPluginManager();
if (isOp) {
try {
Map<String, Command> commandMap = (Map<String, Command>) pluginManager.getClass()
.getField("commandMap").get(pluginManager);

if (isPlayer) {
//the proxied player is the actual invoker other it's the console
getProxy().getPluginManager().dispatchCommand(invoker, commandLine);
} else {
CommandSender console = getProxy().getConsole();
getProxy().getPluginManager().dispatchCommand(console, commandLine);
Command pluginCmd = commandMap.get(command);
if (pluginCmd == null) {
invoker.sendMessage(new ComponentBuilder("Command not known")
.color(ChatColor.RED)
.create());
} else {
pluginCmd.execute(invoker, arguments.split(" "));
}
} catch (NoSuchFieldException | IllegalAccessException ex) {
String exMess = ex.getMessage();
BaseComponent[] message = new ComponentBuilder("Error occurred executing command " + exMess)
.color(ChatColor.RED)
.create();
invoker.sendMessage(message);
getLogger().log(Level.WARNING, "Cannot access command map for executing command", ex);
}
} else {
String commandLine = command + ' ' + arguments;
pluginManager.dispatchCommand(invoker, commandLine);
}
}
}
37 changes: 2 additions & 35 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<packaging>pom</packaging>

<name>CommandForward</name>
<version>0.2</version>
<version>0.3</version>
<inceptionYear>2016</inceptionYear>
<url>https://www.spigotmc.org/resources/</url>
<description>
Expand All @@ -17,8 +17,6 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--Possibility to deploy directly to the plugins folder-->
<outputDir>${basedir}/target</outputDir>
</properties>

<modules>
Expand All @@ -27,17 +25,6 @@
<module>universal</module>
</modules>

<issueManagement>
<system>GitHub</system>
<url>https://github.com/games647/CommandForward/issues</url>
</issueManagement>

<scm>
<url>https://github.com/games647/CommandForward</url>
<connection>scm:git:git://github.com/games647/CommandForward.git</connection>
<developerConnection>scm:git:ssh://git@github.com:games647/CommandForward.git</developerConnection>
</scm>

<build>
<defaultGoal>install</defaultGoal>
<!--Just use the project name to replace an old version of the plugin if the user does only copy-paste-->
Expand All @@ -47,21 +34,10 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<outputDirectory>${outputDir}</outputDirectory>
</configuration>
</plugin>
</plugins>
Expand All @@ -72,15 +48,6 @@
<!--Replace variables-->
<filtering>true</filtering>
</resource>

<!--Add the license to jar in order to see it in the final jar-->
<resource>
<!--Parent folder-->
<directory>${basedir}/..</directory>
<includes>
<include>LICENSE</include>
</includes>
</resource>
</resources>
</build>
</project>
4 changes: 2 additions & 2 deletions 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>commandforward</artifactId>
<version>0.2</version>
<version>0.3</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand All @@ -22,7 +22,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<version>3.0.0</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<shadedArtifactAttached>false</shadedArtifactAttached>
Expand Down

3 comments on commit 85c7e4d

@YZhang-Jeremy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey thx for your work so much! But can you plz generate a jar file for me? I don't know how to use github to generate a jar file. thx!
xd

@games647
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@YZhang-Jeremy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx!

Please sign in to comment.