Skip to content

Commit

Permalink
1.8 - 1.12 support
Browse files Browse the repository at this point in the history
  • Loading branch information
YouHaveTrouble committed Aug 2, 2020
1 parent d7e461c commit ca18d17
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 5 deletions.
11 changes: 10 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>eu.endermite</groupId>
<artifactId>CommandWhitelist</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
<packaging>jar</packaging>

<name>CommandWhitelist</name>
Expand Down Expand Up @@ -66,6 +66,10 @@
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>dmulloy2-repo</id>
<url>https://repo.dmulloy2.net/nexus/repository/public/</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -87,5 +91,10 @@
<version>1.15-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>4.5.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package eu.endermite.commandwhitelist.spigot;

import com.comphenix.protocol.ProtocolLib;
import com.comphenix.protocol.ProtocolLibrary;
import eu.endermite.commandwhitelist.spigot.command.MainCommand;
import eu.endermite.commandwhitelist.spigot.config.ConfigCache;
import eu.endermite.commandwhitelist.spigot.listeners.LegacyPlayerTabChatCompleteListener;
import eu.endermite.commandwhitelist.spigot.listeners.PlayerCommandPreProcessListener;
import eu.endermite.commandwhitelist.spigot.listeners.PlayerCommandSendListener;
import org.bukkit.Bukkit;
Expand All @@ -14,15 +17,30 @@ public class CommandWhitelist extends JavaPlugin {

private static CommandWhitelist commandWhitelist;
private static ConfigCache configCache;
private static boolean isLegacy;

@Override
public void onEnable() {

commandWhitelist = this;

isLegacy = checkLegacy();


reloadPluginConfig();

getServer().getPluginManager().registerEvents(new PlayerCommandPreProcessListener(), this);
getServer().getPluginManager().registerEvents(new PlayerCommandSendListener(), this);
if (!isLegacy) {
getServer().getPluginManager().registerEvents(new PlayerCommandSendListener(), this);
} else {
getLogger().info(ChatColor.AQUA+"Running in legacy mode...");
if (getServer().getPluginManager().getPlugin("ProtocolLib") != null) {
LegacyPlayerTabChatCompleteListener.protocol(this);
} else {
getLogger().info(ChatColor.YELLOW+"ProtocolLib is required for tab completion blocking!");
}
}

getCommand("commandwhitelist").setExecutor(new MainCommand());
getCommand("commandwhitelist").setTabCompleter(new MainCommand());

Expand All @@ -37,13 +55,46 @@ public void reloadPluginConfig() {
public void reloadPluginConfig(CommandSender sender) {
getServer().getScheduler().runTaskAsynchronously(this, () -> {
reloadPluginConfig();
for (Player p : Bukkit.getOnlinePlayers()) {
p.updateCommands();
if (!isLegacy()) {
for (Player p : Bukkit.getOnlinePlayers()) {
p.updateCommands();
}
}
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelist.getConfigCache().getPrefix() + CommandWhitelist.getConfigCache().getConfigReloaded()));
});
}

public boolean isLegacy() {
return isLegacy;
}

private boolean checkLegacy() {

String version = getServer().getVersion();

if (version.contains("1.8")) {
return true;
} else if (version.contains("1.9")) {
return true;
} else if (version.contains("1.10")) {
return true;
} else if (version.contains("1.11")) {
return true;
} else if (version.contains("1.12")) {
return true;
} else if (version.contains("1.13")) {
return false;
} else if (version.contains("1.14")) {
return false;
} else if (version.contains("1.15")) {
return false;
} else if (version.contains("1.16")) {
return false;
}

return false;
}

public static CommandWhitelist getPlugin() {return commandWhitelist;}
public static ConfigCache getConfigCache() {return configCache;}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package eu.endermite.commandwhitelist.spigot.listeners;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import eu.endermite.commandwhitelist.spigot.CommandWhitelist;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LegacyPlayerTabChatCompleteListener {

public static void protocol(CommandWhitelist plugin) {
ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
tabCompleteServerBound(protocolManager, plugin);
tabCompleteClientBound(protocolManager, plugin);
}

public static void tabCompleteServerBound(ProtocolManager protocolManager, Plugin plugin) {
protocolManager.addPacketListener(new PacketAdapter(plugin, ListenerPriority.HIGHEST, PacketType.Play.Server.TAB_COMPLETE) {
@Override
public void onPacketSending(PacketEvent event) {

try {
Player player = event.getPlayer();
if (player.hasPermission("commandwhitelist.bypass")) {
return;
}
PacketContainer packet = event.getPacket();
String[] message = packet.getSpecificModifier(String[].class).read(0);

List<String> commandList = new ArrayList<>();
for (Map.Entry<String, List<String>> s : CommandWhitelist.getConfigCache().getPermList().entrySet()) {
if (player.hasPermission("commandwhitelist.commands." + s.getKey())) {
commandList.addAll(s.getValue());
}
}

List<String> finalList = new ArrayList<>();
int components = 0;

for (String cmd : message) {
for (String cmdFromList : commandList) {
if (cmd.equalsIgnoreCase("/" + cmdFromList) || !cmd.startsWith("/")) {
finalList.add(components++, cmd);
break;
}
}
}

String[] toWrite = new String[components];
int counter = 0;
for (String cmd : finalList) {
toWrite[counter++] = cmd;
}

packet.getSpecificModifier(String[].class).write(0, toWrite);

} catch (Exception e) {
e.printStackTrace();
}

}
});
}

public static void tabCompleteClientBound(ProtocolManager protocolManager, Plugin plugin) {
protocolManager.addPacketListener(new PacketAdapter(plugin, ListenerPriority.HIGHEST, PacketType.Play.Client.TAB_COMPLETE) {
@Override
public void onPacketReceiving(PacketEvent event) {
try {
Player player = event.getPlayer();
if (player.hasPermission("commandwhitelist.bypass")) {
return;
}
PacketContainer packet = event.getPacket();
String command = packet.getSpecificModifier(String.class).read(0);
System.out.println(command);

for (Map.Entry<String, List<String>> s : CommandWhitelist.getConfigCache().getPermList().entrySet()) {
if (player.hasPermission("commandwhitelist.commands." + s.getKey())) {
for (String comm : s.getValue()) {
comm = comm.toLowerCase();
if (command.equalsIgnoreCase("/"+comm))
return;
else if (command.startsWith("/" + comm + " ")) {
return;
}
}
}
}
event.setCancelled(true);


} catch (Exception e) {
e.printStackTrace();
}

}
});
}

}
3 changes: 2 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
name: CommandWhitelist
version: ${project.version}
main: eu.endermite.commandwhitelist.spigot.CommandWhitelist
api-version: 1.13
authors: [YouHaveTrouble]
softdepend:
- ProtocolLib
description: Control what commands players can use
commands:
commandwhitelist:
Expand Down

0 comments on commit ca18d17

Please sign in to comment.