Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Apr 9, 2020
0 parents commit a9207e8
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.iml
.idea/
target/
dependency-reduced-pom.xml
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SimpleCommandsLegacy

## How to disable commands

Create config.yml at plugin configuration directory, then:
```yaml
disabledCommands:
- suicide # disables /suicide (requires simplecommands.suicide)
- pingall # disables /pingall (requires simplecommands.pingall)
- ping # disables /ping (doesn't require any permissions)
```
51 changes: 51 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>xyz.acrylicstyle</groupId>
<artifactId>SimpleCommandsLegacy</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>acrylic-repo</id>
<url>https://repo.acrylicstyle.xyz/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>xyz.acrylicstyle</groupId>
<artifactId>NMSAPI-1.8.8</artifactId>
<version>0.0.46</version>
</dependency>
<dependency>
<groupId>xyz.acrylicstyle</groupId>
<artifactId>Util</artifactId>
<version>0.8.17</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
23 changes: 23 additions & 0 deletions src/main/java/xyz/acrylicstyle/simplecommands/SimpleCommands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package xyz.acrylicstyle.simplecommands;

import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import xyz.acrylicstyle.simplecommands.commands.Ping;
import xyz.acrylicstyle.simplecommands.commands.PingAll;
import xyz.acrylicstyle.simplecommands.commands.Suicide;

import java.util.List;
import java.util.Objects;

@SuppressWarnings("unused")
public class SimpleCommands extends JavaPlugin implements Listener {
@Override
public void onEnable() {
List<String> disabledCommands = this.getConfig().getStringList("disabledCommands");
if (!disabledCommands.contains("ping")) Objects.requireNonNull(Bukkit.getPluginCommand("ping")).setExecutor(new Ping());
if (!disabledCommands.contains("pingall")) Objects.requireNonNull(Bukkit.getPluginCommand("pingall")).setExecutor(new PingAll());
if (!disabledCommands.contains("suicide")) Objects.requireNonNull(Bukkit.getPluginCommand("suicide")).setExecutor(new Suicide());
Bukkit.getPluginManager().registerEvents(this, this);
}
}
30 changes: 30 additions & 0 deletions src/main/java/xyz/acrylicstyle/simplecommands/commands/Ping.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package xyz.acrylicstyle.simplecommands.commands;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class Ping implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "This command cannot be invoked from console.");
return true;
}
try {
if (args.length == 1) {
Player player = Bukkit.getPlayer(args[0]);
sender.sendMessage(ChatColor.GREEN + "Ping: " + PingAll.getPing(player) + "ms");
} else {
Player player = (Player) sender;
player.sendMessage(ChatColor.GREEN + "Ping: " + PingAll.getPing(player) + "ms");
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package xyz.acrylicstyle.simplecommands.commands;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import xyz.acrylicstyle.simplecommands.utils.Utils;

import java.lang.reflect.InvocationTargetException;

public class PingAll implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
Utils.getOnlinePlayers().forEach(player -> {
try {
sender.sendMessage(ChatColor.GREEN + player.getName() + "'s ping: " + getPing(player) + "ms");
} catch (Exception e) {
e.printStackTrace();
}
});
return true;
}

static String getPing(Player player) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
Object craftPlayer = Utils.getHandle(player);
int ping = (int) craftPlayer.getClass().getField("ping").get(craftPlayer);
String message;
if (ping <= 5) message = "" + net.md_5.bungee.api.ChatColor.LIGHT_PURPLE + ping;
else if (ping <= 50) message = "" + net.md_5.bungee.api.ChatColor.GREEN + ping;
else if (ping <= 150) message = "" + net.md_5.bungee.api.ChatColor.YELLOW + ping;
else if (ping <= 250) message = "" + net.md_5.bungee.api.ChatColor.GOLD + ping;
else if (ping <= 350) message = "" + net.md_5.bungee.api.ChatColor.RED + ping;
else message = "" + net.md_5.bungee.api.ChatColor.DARK_RED + ping;
return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package xyz.acrylicstyle.simplecommands.commands;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class Suicide implements CommandExecutor {
public boolean onCommand(CommandSender sender, Command command, String cmd, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "This command cannot be invoked from console.");
return true;
}
Player player = (Player) sender;
player.setHealth(0.0D);
player.sendMessage(ChatColor.GOLD + "You took your own life.");
return true;
}
}
29 changes: 29 additions & 0 deletions src/main/java/xyz/acrylicstyle/simplecommands/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package xyz.acrylicstyle.simplecommands.utils;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import util.CollectionList;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class Utils {
private Utils() {}

public static CollectionList<Player> getOnlinePlayers() {
CollectionList<Player> players = new CollectionList<>();
players.addAll(Bukkit.getOnlinePlayers());
return players;
}

public static Object getHandle(Object instance) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method method;
try {
method = instance.getClass().getDeclaredMethod("getHandle");
} catch (NoSuchMethodException e) {
method = instance.getClass().getSuperclass().getDeclaredMethod("getHandle");
}
method.setAccessible(true);
return method.invoke(instance);
}
}
12 changes: 12 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: SimpleCommandsLegacy
main: xyz.acrylicstyle.simplecommands.SimpleCommands
version: 1.0-SNAPSHOT
commands:
suicide:
description: "Kills yourself. Why would you do that?"
permission: simplecommands.suicide
ping:
description: "Shows your ping"
pingall:
description: "Shows everyone's ping"
permission: simplecommands.pingall

0 comments on commit a9207e8

Please sign in to comment.