diff --git a/src/me/mctp/MCTPAudioCmd.java b/src/me/mctp/MCTPAudioCmd.java index 1783011..a3aecfd 100644 --- a/src/me/mctp/MCTPAudioCmd.java +++ b/src/me/mctp/MCTPAudioCmd.java @@ -1,11 +1,17 @@ package me.mctp; +import me.mctp.api.AudioType; import me.mctp.managers.PinManager; +import me.mctp.utils.SpigotPlayerSelector; +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; +import org.json.simple.JSONObject; + +import java.util.ArrayList; public class MCTPAudioCmd implements CommandExecutor { @@ -13,7 +19,45 @@ public class MCTPAudioCmd implements CommandExecutor { public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) { if (cmd.getName().equalsIgnoreCase("mctpaudio")) { - sender.sendMessage(prefix + "MCTP Audio command!"); + // /mctpaudio play SELECTOR TYPE URL + if (args.length == 4 && args[0].equalsIgnoreCase("play")) { + AudioType type; + try { + type = AudioType.valueOf(args[2].toUpperCase()); + } catch (IllegalArgumentException ex) { + sender.sendMessage(prefix + args[1] + " is geen correcte type."); + return true; + } + + ArrayList players = new ArrayList<>(); + Player target = Bukkit.getPlayer(args[1]); + if (target != null) { + if (!PinManager.hasPin(target.getUniqueId())) { + sender.sendMessage(prefix + "Die speler is niet verbonden met de client."); + return true; + } + + players.add(target); + } else { + SpigotPlayerSelector selector = new SpigotPlayerSelector(args[1]); + players.addAll(selector.getPlayers(sender)); + } + + JSONObject data; + for (Player p : players) { + data = new JSONObject(); + + if (!PinManager.hasPin(p.getUniqueId())) continue; + + data.put("action", type.name()); + data.put("path", args[3]); + data.put("uuid", p.getUniqueId().toString().replace("-", "")); + Main.getClient().sendData(data); + } + + sender.sendMessage(prefix + "Gestart met afspelen!"); + return true; + } } else if (cmd.getName().equalsIgnoreCase("audio")) { if (!(sender instanceof Player)) { sender.sendMessage(prefix + "Alleen spelers kunnen verbinden met onze audioclient."); diff --git a/src/me/mctp/api/AudioType.java b/src/me/mctp/api/AudioType.java new file mode 100644 index 0000000..8c6fc71 --- /dev/null +++ b/src/me/mctp/api/AudioType.java @@ -0,0 +1,5 @@ +package me.mctp.api; + +public enum AudioType { + MUSIC, SFX, RADIO +} diff --git a/src/me/mctp/utils/SpigotPlayerSelector.java b/src/me/mctp/utils/SpigotPlayerSelector.java new file mode 100644 index 0000000..90e8108 --- /dev/null +++ b/src/me/mctp/utils/SpigotPlayerSelector.java @@ -0,0 +1,148 @@ +package me.mctp.utils; + +import org.bukkit.Bukkit; +import org.bukkit.Location; + +import org.bukkit.command.BlockCommandSender; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +public class SpigotPlayerSelector { + + private String selector; + + public SpigotPlayerSelector(String selector) { + this.selector = selector; + } + + /** + * this turns selectors like @a[r=5] into a usable list, since + * 1.13 spigot removed this feature, FOR SOME REASON.. thanks guys.. + * + * @param commandSender the sender + * @return players following the selector + */ + public List getPlayers(CommandSender commandSender) { + List players = new ArrayList<>(); + + if (selector.startsWith("@p")) { + //get Location + Location standPoint = getLocation(commandSender); + + if (getArgument("r").length() != 0) { + int radius = Integer.parseInt(getArgument("r")); + Player nearest = Bukkit.getOnlinePlayers().stream() + .filter(player -> player.getLocation().getWorld().getName().equals(standPoint.getWorld().getName())) + .min(Comparator.comparing(player -> player.getLocation().distance(standPoint))) + .filter(player -> radius > player.getLocation().distance(standPoint)) + .get(); + players.add(nearest); + } + + if (getArgument("distance").length() != 0) { + int distance = Integer.parseInt(getArgument("distance")); + Player nearest = Bukkit.getOnlinePlayers().stream() + .filter(player -> player.getLocation().getWorld().getName().equals(standPoint.getWorld().getName())) + .min(Comparator.comparing(player -> player.getLocation().distance(standPoint))) + .filter(player -> distance > player.getLocation().distance(standPoint)) + .get(); + players.add(nearest); + } + + else { + Bukkit.getOnlinePlayers().stream() + .filter(player -> player.getLocation().getWorld().getName().equals(standPoint.getWorld().getName())) + .min(Comparator.comparing(player -> player.getLocation().distance(standPoint))) + .ifPresent(players::add); + } + } + else if (selector.startsWith("@a")) { + //everyone + Location standPoint = getLocation(commandSender); + + if (getArgument("region").length() != 0) { + //TODO FIX REGION SELECTOR + } else if (getArgument("r").length() != 0) { + int radius = Integer.parseInt(getArgument("r")); + players.addAll(Bukkit.getOnlinePlayers().stream() + .filter(player -> player.getLocation().getWorld().getName().equals(standPoint.getWorld().getName())) + .filter(player -> radius > player.getLocation().distance(standPoint)) + .collect(Collectors.toList())); + } else if (getArgument("distance").length() != 0) { + int distance = Integer.parseInt(getArgument("distance")); + players.addAll(Bukkit.getOnlinePlayers().stream() + .filter(player -> player.getLocation().getWorld().getName().equals(standPoint.getWorld().getName())) + .filter(player -> distance > player.getLocation().distance(standPoint)) + .collect(Collectors.toList())); + } + + else { + players.addAll(Bukkit.getOnlinePlayers().stream() + .filter(player -> player.getLocation().getWorld().getName().equals(standPoint.getWorld().getName())) + .collect(Collectors.toList())); + } + } + else if (selector.length() <= 16) { + //player + Player player = Bukkit.getPlayer(selector); + if (player != null) players.add(player); + } + else { + //you fucked it + commandSender.sendMessage("Invalid player query. Try something like @a, @p, username or other arguments."); + } + return players; + } + + /** + * attempt to parse the location + * + * @param commandSender the sender + * @return the location or null + */ + private Location getLocation(CommandSender commandSender) { + Location initialLocation = new Location(Bukkit.getWorlds().get(0), 0, 0, 0); + + if (commandSender instanceof Player) { + initialLocation = ((Player) commandSender).getLocation(); + } else if (commandSender instanceof BlockCommandSender) { + initialLocation = ((BlockCommandSender) commandSender).getBlock().getLocation(); + } + + if (!getArgument("x").equals("") && !getArgument("y").equals("") && !getArgument("z").equals("")) { + try { + int x = Integer.parseInt(getArgument("x")); + int y = Integer.parseInt(getArgument("y")); + int z = Integer.parseInt(getArgument("z")); + return new Location(initialLocation.getWorld(), x, y, z); + } catch (Exception e) { + commandSender.sendMessage("An error occurred when parsing the location as an Integer"); + return initialLocation; + } + } + + return initialLocation; + } + + private String getArgument(String key) { + StringBuilder result = new StringBuilder(); + String[] arguments = selector.split(key + "="); + if (arguments.length == 1) return ""; + for (byte type : arguments[1].getBytes()) { + char element = (char) type; + if (element == ',' || element == ']') { + return result.toString(); + } else { + result.append(element); + } + } + + return result.toString().replaceAll(".", ""); + } + +} \ No newline at end of file