Moved to maven

This commit is contained in:
stijnb1234 2020-11-16 18:52:42 +01:00
parent 833b55d6ca
commit 78c68f4f32
32 changed files with 382 additions and 2739 deletions

View file

@ -0,0 +1,88 @@
package me.mctp;
import me.mctp.commands.MCTPAudioCMD;
import me.mctp.listener.LogoutListener;
import me.mctp.listener.WGListener;
import me.mctp.managers.WGManager;
import me.mctp.radio.Playlist;
import me.mctp.socket.Client;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin implements Listener {
private static Plugin pl;
private static Client client;
private static Playlist playlist;
public static String prefix = (ChatColor.GOLD + "[" + ChatColor.YELLOW + "MCTP" + ChatColor.GOLD + "] " + ChatColor.GRAY);
public void onEnable(){
Bukkit.getServer().getPluginManager().registerEvents(this, this);
Bukkit.getLogger().info(prefix + "loading...");
if (!setupPlugins()) {
Bukkit.getLogger().severe(String.format("[%s] Disabled due to no WorldGuard dependency found!", getDescription().getName()));
getServer().getPluginManager().disablePlugin(this);
return;
}
getConfig().addDefault("Regions.demosound", "https://audiopagina.mcthemeparks.eu/gallery/voletarium.mp3");
getConfig().addDefault("HueRegions.demosound", "255_0_0_254");
getConfig().options().copyDefaults(true);
saveConfig();
pl = this;
client = new Client("ws://173.249.31.58:8166");
client.connect();
getCommand("audio").setExecutor(new MCTPAudioCMD());
getCommand("mctpaudio").setExecutor(new MCTPAudioCMD());
Bukkit.getPluginManager().registerEvents(new WGListener(), this);
Bukkit.getPluginManager().registerEvents(new LogoutListener(), this);
playlist = new Playlist();
Bukkit.getLogger().info(prefix + " __ __ ____ _____ ____ _ _ _ ");
Bukkit.getLogger().info(prefix + " | \\/ |/ ___|_ _| _ \\ / \\ _ _ __| (_) ___ ");
Bukkit.getLogger().info(prefix + " | |\\/| | | | | | |_) / _ \\| | | |/ _` | |/ _ \\ ");
Bukkit.getLogger().info(prefix + " | | | | |___ | | | __/ ___ \\ |_| | (_| | | (_) |");
Bukkit.getLogger().info(prefix + " |_| |_|\\____| |_| |_| /_/ \\_\\__,_|\\__,_|_|\\___/ ");
Bukkit.getLogger().info(prefix + " ");
Bukkit.getLogger().info(prefix + "successfully enabled!");
}
public void onDisable() {
client.disconnect();
Bukkit.getLogger().info(prefix + "successfully disabled!");
}
private boolean setupPlugins() {
if (hasWorldGuardOnServer()) {
WGManager.setWorldGuard(getServer().getPluginManager().getPlugin("WorldGuard"));
return true;
}
return false;
}
private static boolean hasWorldGuardOnServer() {
return Bukkit.getPluginManager().getPlugin("WorldGuard") != null;
}
public static Plugin getPlugin() {
return pl;
}
public static Client getClient() {
return client;
}
public static Playlist getPlaylist() {
return playlist;
}
}

View file

@ -0,0 +1,5 @@
package me.mctp.api;
public enum AudioType {
MUSIC, SFX, RADIO
}

View file

@ -0,0 +1,5 @@
package me.mctp.api;
public enum HueType {
LEFT, MID, RIGHT, ALL
}

View file

@ -0,0 +1,28 @@
package me.mctp.api.maps;
import java.util.*;
public class Playlist<E> extends ArrayList<E> {
public void shuffle() {
Random random = new Random();
for (int index = 0; index < this.size(); index++) {
int secondIndex = random.nextInt(this.size());
swap(index, secondIndex);
}
}
public E getRandom() {
Random random = new Random();
return this.get(random.nextInt(this.size()));
}
private void swap(int firstIndex, int secondIndex) {
E first = this.get(firstIndex);
E second = this.get(secondIndex);
this.set(secondIndex, first);
this.set(firstIndex, second);
}
}

View file

@ -0,0 +1,231 @@
package me.mctp.commands;
import me.mctp.Main;
import me.mctp.api.AudioType;
import me.mctp.api.HueType;
import me.mctp.managers.PinManager;
import me.mctp.utils.HeadUtil;
import me.mctp.utils.SpigotPlayerSelector;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;
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;
import java.util.List;
public class MCTPAudioCMD implements CommandExecutor {
public static String prefix = (ChatColor.GOLD + "[" + ChatColor.YELLOW + "MCTP" + ChatColor.GOLD + "] " + ChatColor.GRAY);
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
if (cmd.getName().equalsIgnoreCase("mctpaudio")) {
if (!sender.hasPermission("mctp.audio")) {
sender.sendMessage(prefix + "You don't have the permission to do this.");
return true;
}
if (args.length == 1 && args[0].equalsIgnoreCase("toggleradio")) {
if (Main.getPlaylist().isRunning()) {
Main.getPlaylist().stop();
sender.sendMessage(prefix + "De auto radio is stopgezet. Zodra het huidige nummer is afgelopen, gebeurt er niks meer.");
} else {
Main.getPlaylist().init();
sender.sendMessage(prefix + "De auto radio is weer gestart.");
}
return true;
} else if (args.length == 2 && args[0].equalsIgnoreCase("addsong")) {
List<String> urls = Main.getPlugin().getConfig().getStringList("RadioSongs");
urls.add(args[1]);
Main.getPlugin().getConfig().set("RadioSongs", urls);
Main.getPlugin().saveConfig();
Main.getPlaylist().addSong(args[1]);
sender.sendMessage(prefix + "Nummer toegevoegd aan de lijst.");
return true;
} else if (args.length == 4 && args[0].equalsIgnoreCase("play")) {
AudioType type;
try {
type = AudioType.valueOf(args[2].toUpperCase());
} catch (IllegalArgumentException ex) {
sender.sendMessage(prefix + args[2] + " is geen correcte type.");
return true;
}
ArrayList<Player> 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("task", 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 ((args.length == 6 || args.length == 7) && args[0].equalsIgnoreCase("hue")) {
int r;
int g;
int b;
try {
r = Integer.parseInt(args[2]);
g = Integer.parseInt(args[3]);
b = Integer.parseInt(args[4]);
} catch (IllegalArgumentException ex) {
sender.sendMessage(prefix + args[2] + ", " + args[3] + ", " + args[4] + " zijn incorrecte RGB waardes.");
return true;
}
Integer brightness = null;
if (args.length == 7) {
try {
brightness = Integer.parseInt(args[6]);
} catch (IllegalArgumentException ex) {
sender.sendMessage(prefix + args[6] + " is geen correcte brightness.");
return true;
}
if (brightness < 0 || brightness > 254) {
sender.sendMessage(prefix + args[6] + " is geen correcte brightness.");
return true;
}
}
HueType type;
try {
type = HueType.valueOf(args[5].toUpperCase());
} catch (IllegalArgumentException ex) {
sender.sendMessage(prefix + args[5] + " is geen correcte type.");
return true;
}
ArrayList<Player> 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));
}
//CHECK FOR THE REGION SELECTOR -> Then save
if (args[1].startsWith("@a") && HeadUtil.getArgument(args[1], "region").length() != 0) {
String regionID = HeadUtil.getArgument(args[1], "region");
String data = r + "_" + g + "_" + b + "_" + type.name();
if (brightness != null) data += "_" + brightness;
Main.getPlugin().getConfig().set("HueRegions." + regionID, data);
Main.getPlugin().saveConfig();
}
JSONObject data;
for (Player p : players) {
data = new JSONObject();
if (!PinManager.hasPin(p.getUniqueId())) continue;
data.put("task", "HUE");
data.put("rgb", r + ":" + g + ":" + b);
data.put("type", type.name());
if (brightness != null) data.put("brightness", brightness);
data.put("uuid", p.getUniqueId().toString().replace("-", ""));
Main.getClient().sendData(data);
}
sender.sendMessage(prefix + "Indien de speler(s) is/zijn verbonden met Philips Hue, is de kleur veranderd.");
return true;
} else if (args.length == 3 && args[0].equalsIgnoreCase("setregion")) {
String regionName = args[1];
String url = args[2];
Main.getPlugin().getConfig().set("Regions." + regionName, url);
Main.getPlugin().saveConfig();
sender.sendMessage(prefix + "De region zal vanaf nu muziek afspelen.");
return true;
} else if (args.length == 3 && args[0].equalsIgnoreCase("sethueregion")) {
String regionName = args[1];
String url = args[2];
Main.getPlugin().getConfig().set("Regions." + regionName, url);
Main.getPlugin().saveConfig();
sender.sendMessage(prefix + "De region zal vanaf nu muziek afspelen.");
return true;
} else if (args.length == 2 && args[0].equalsIgnoreCase("stop")) {
ArrayList<Player> 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("task", "STOP");
data.put("uuid", p.getUniqueId().toString().replace("-", ""));
Main.getClient().sendData(data);
}
sender.sendMessage(prefix + "Gestopt met afspelen!");
return true;
}
} else if (cmd.getName().equalsIgnoreCase("audio")) {
if (!(sender instanceof Player)) {
sender.sendMessage(prefix + "Alleen spelers kunnen verbinden met onze audioclient.");
return true;
}
Player p = (Player) sender;
String pin = PinManager.getPIN(p.getUniqueId());
String url = "http://audio.mcthemeparks.eu/";
url = url + "?uuid=" + p.getUniqueId().toString().replace("-", "") + "&pin=" + pin;
TextComponent message = new TextComponent(TextComponent.fromLegacyText(prefix + "Click here to connect to the audio client."));
message.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url));
p.spigot().sendMessage(message);
return true;
}
return false;
}
}

View file

@ -0,0 +1,17 @@
package me.mctp.listener;
import me.mctp.Main;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.json.simple.JSONObject;
public class LogoutListener implements Listener {
@EventHandler
public void onDisconnect(PlayerQuitEvent e) {
JSONObject data = new JSONObject();
data.put("task", "LOGOUT");
data.put("uuid", e.getPlayer().getUniqueId().toString().replace("-", ""));
Main.getClient().sendData(data);
}
}

View file

@ -0,0 +1,107 @@
package me.mctp.listener;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import me.mctp.Main;
import me.mctp.managers.PinManager;
import me.mctp.managers.WGManager;
import net.raidstone.wgevents.events.RegionsEnteredEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.json.simple.JSONObject;
import java.util.*;
import java.util.stream.Collectors;
public class WGListener implements Listener {
/** Music detection */
@EventHandler
public void onMove(PlayerMoveEvent e) {
if (e.getTo() == null || e.getFrom().getWorld() == null || e.getTo().getWorld() == null) return;
if (e.getFrom().getBlockX() != e.getTo().getBlockX() || e.getFrom().getBlockY() != e.getTo().getBlockY() || e.getFrom().getBlockZ() != e.getTo().getBlockZ()) {
Set<String> list = Main.getPlugin().getConfig().getConfigurationSection("Regions").getKeys(false);
if (!PinManager.hasPin(Objects.requireNonNull(e.getPlayer()).getUniqueId())) return;
List<ProtectedRegion> regions = WGManager.getRegionsIn(e.getFrom());
List<String> regionNames = regions.stream().map(ProtectedRegion::getId).collect(Collectors.toList());
List<ProtectedRegion> regions2 = WGManager.getRegionsIn(e.getTo());
List<String> regionNames2 = regions2.stream().map(ProtectedRegion::getId).collect(Collectors.toList());
if ((Collections.disjoint(list, regionNames) && !Collections.disjoint(list, regionNames2)) || (!Collections.disjoint(list, regionNames) && !Collections.disjoint(list, regionNames2))) {
//Walked in a region
if (!Collections.disjoint(list, regionNames) && !Collections.disjoint(list, regionNames2)) {
Optional<String> name = regionNames.stream().filter(list::contains).findFirst();
Optional<String> name2 = regionNames2.stream().filter(list::contains).findFirst();
if (name.isPresent() && name2.isPresent() && name.get().equals(name2.get())) {
return;
}
if (name.isPresent() && name2.isPresent()) {
String regionURL = Main.getPlugin().getConfig().getString("Regions." + name.get());
String regionURL2 = Main.getPlugin().getConfig().getString("Regions." + name2.get());
if (regionURL.equals(regionURL2)) {
return;
}
}
}
Optional<String> name = regionNames2.stream().filter(list::contains).findFirst();
if (!name.isPresent()) return;
String regionURL = Main.getPlugin().getConfig().getString("Regions." + name.get());
JSONObject data = new JSONObject();
data.put("task", "MUSIC");
data.put("path", regionURL);
data.put("uuid", e.getPlayer().getUniqueId().toString().replace("-", ""));
Main.getClient().sendData(data);
} else if (!Collections.disjoint(list, regionNames) && Collections.disjoint(list, regionNames2)) {
//Not in a region, stop...
JSONObject data = new JSONObject();
data.put("task", "MUSIC");
data.put("path", "");
data.put("uuid", e.getPlayer().getUniqueId().toString().replace("-", ""));
Main.getClient().sendData(data);
}
}
}
/** Hue detection */
@EventHandler
public void onRegionEnter(RegionsEnteredEvent e) {
if (e.getPlayer() == null) return;
Set<String> list2 = Main.getPlugin().getConfig().getConfigurationSection("HueRegions").getKeys(false);
if (!Collections.disjoint(list2, e.getRegionsNames())) {
//One element is the same -> In a region
Optional<String> name = e.getRegionsNames().stream().filter(list2::contains).findFirst();
if (!name.isPresent()) return;
String configData = Main.getPlugin().getConfig().getString("HueRegions." + name.get());
if (configData == null) return;
String[] configDataSplit = configData.split("_");
int r = Integer.parseInt(configDataSplit[0]);
int g = Integer.parseInt(configDataSplit[1]);
int b = Integer.parseInt(configDataSplit[2]);
String type = configDataSplit[3];
Integer brightness = null;
if (configDataSplit.length == 5) brightness = Integer.parseInt(configDataSplit[4]);
if (!PinManager.hasPin(e.getPlayer().getUniqueId())) return;
JSONObject data = new JSONObject();
data.put("task", "HUE");
data.put("rgb", r + ":" + g + ":" + b);
data.put("type", type);
if (brightness != null) data.put("brightness", brightness);
data.put("uuid", e.getPlayer().getUniqueId().toString().replace("-", ""));
Main.getClient().sendData(data);
}
}
}

View file

@ -0,0 +1,72 @@
package me.mctp.managers;
import java.security.SecureRandom;
import java.util.UUID;
import java.util.WeakHashMap;
public class PinManager {
private static final WeakHashMap<UUID, String> pins = new WeakHashMap<>();
/**
* Get the pin of a player
*
* @param pUUID The player UUID
*
* @return The pin
*/
public static String getPIN(UUID pUUID) {
if (pUUID == null) return null;
if (!pins.containsKey(pUUID)) {
StringBuilder builder = new StringBuilder();
SecureRandom random = new SecureRandom();
for (int i = 0; i < 9; ++i) {
builder.append(random.nextInt(9));
}
String pin = builder.toString();
pins.put(pUUID, pin);
return pin;
} else {
return pins.get(pUUID);
}
}
/**
* Check if a player has a pin
*
* @param pUUID The player UUID
*
* @return The pin
*/
public static boolean hasPin(UUID pUUID) {
return pUUID != null && pins.containsKey(pUUID);
}
/**
* Check if the pin is correct
*
* @param pUUID The player UUID
* @param pin The pin
*
* @return true/false
*/
public static boolean checkPin(UUID pUUID, String pin) {
if (pUUID == null || pin == null || pin.isEmpty()) {
return false;
}
return pins.containsKey(pUUID) && pin.equals(pins.get(pUUID));
}
/**
* Remove the pin
*
* @param pUUID The player UUID
*/
public static void removePin(UUID pUUID) {
if (pUUID != null) {
pins.remove(pUUID);
}
}
}

View file

@ -0,0 +1,295 @@
package me.mctp.managers;
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.regions.Polygonal2DRegion;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.domains.DefaultDomain;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.flags.RegionGroup;
import com.sk89q.worldguard.protection.flags.StateFlag.State;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.managers.storage.StorageException;
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
/**
* WorldGuard class to make the usage of WorldGuard easy. This is the 1.14.x version!
*
* <i>Note that if you do use this in one of your projects, leave this notice.</i>
* <i>Please do credit me if you do use this in one of your projects.</i>
*
* @author SBDeveloper [Fixed 1.13+ support]
*/
public class WGManager {
public static WorldGuardPlugin wgp;
public static WorldEditPlugin wep;
public static boolean hasWorldGuard() {
return wgp != null;
}
public static boolean hasWorldEdit() {
return wep != null;
}
public static boolean setWorldGuard(Plugin plugin) {
wgp = (WorldGuardPlugin) plugin;
return true;
}
public static boolean setWorldEdit(Plugin plugin) {
wep = (WorldEditPlugin) plugin;
return true;
}
public static ProtectedRegion createRegion(Player p, String id) throws StorageException {
LocalSession l = WorldEdit.getInstance().getSessionManager().get(BukkitAdapter.adapt(p));
Region s;
try {
s = l.getSelection(l.getSelectionWorld());
} catch (IncompleteRegionException e) {
return null;
}
RegionManager rm = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(p.getWorld()));
rm.removeRegion(id);
ProtectedRegion region;
// Detect the type of region from WorldEdit
if (s instanceof Polygonal2DRegion) {
Polygonal2DRegion polySel = (Polygonal2DRegion) s;
int minY = polySel.getMinimumY();
int maxY = polySel.getMaximumY();
region = new ProtectedPolygonalRegion(id, polySel.getPoints(), minY, maxY);
} else { /// default everything to cuboid
region = new ProtectedCuboidRegion(id,
s.getMinimumPoint(),
s.getMaximumPoint());
}
region.setPriority(11); /// some relatively high priority
region.setFlag(Flags.INTERACT, State.DENY);
region.setFlag(Flags.INTERACT.getRegionGroupFlag(), RegionGroup.NON_MEMBERS);
rm.addRegion(region);
rm.save();
return region;
}
public static ArrayList<ProtectedRegion> getRegionsIn(Location loc) {
ArrayList<ProtectedRegion> inRegions = new ArrayList<>();
RegionManager rm = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(loc.getWorld()));
for (ProtectedRegion protectedRegion : rm.getApplicableRegions(BukkitAdapter.asBlockVector(loc))) inRegions.add(protectedRegion);
return inRegions;
}
public static ProtectedRegion getRegionfromStringList(List<String> str, Location loc) {
RegionManager rm = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(loc.getWorld()));
ApplicableRegionSet mogreg = rm.getApplicableRegions(BukkitAdapter.asBlockVector(loc));
for (ProtectedRegion mogregion : mogreg) {
for (String strgo : str) {
if(strgo.equalsIgnoreCase(mogregion.getId())) {
return mogregion;
}
}
}
//Niks gevonden!
return null;
}
public static ProtectedRegion getRegionfromString(String str, Location loc) {
RegionManager rm = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(loc.getWorld()));
ApplicableRegionSet mogreg = rm.getApplicableRegions(BukkitAdapter.asBlockVector(loc));
for (ProtectedRegion mogregion : mogreg) {
if(str.equalsIgnoreCase(mogregion.getId())) {
return mogregion;
}
}
//Niks gevonden!
return null;
}
public static ProtectedRegion getRegionfromStringListInWorld(List<String> str, Location loc) {
RegionManager rm = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(loc.getWorld()));
Map<String, ProtectedRegion> mp = rm.getRegions();
for (Entry<String, ProtectedRegion> pair : mp.entrySet()) {
for (String string : str) {
if (pair.getValue().getId().equals(string)) {
return pair.getValue();
}
}
}
//Niks gevonden!
return null;
}
public static ProtectedRegion getRegionfromStringInWorld(String str, Location loc) {
String strgood = str.toLowerCase();
RegionManager rm = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(loc.getWorld()));
Map<String, ProtectedRegion> mp = rm.getRegions();
return mp.get(strgood);
}
public static boolean isMember(Player p, ProtectedRegion region, OfflinePlayer target) {
World w = p.getWorld();
RegionManager rm = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(w));
ProtectedRegion currentRegion = rm.getRegion(region.getId());
DefaultDomain currentMembers = currentRegion.getMembers();
return currentMembers.contains(target.getUniqueId());
}
public static boolean isOwner(Player p, ProtectedRegion region, OfflinePlayer target) {
World w = p.getWorld();
RegionManager rm = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(w));
ProtectedRegion currentRegion = rm.getRegion(region.getId());
DefaultDomain currentOwners = currentRegion.getOwners();
return currentOwners.contains(target.getUniqueId());
}
public static boolean addMember(Player p, ProtectedRegion region, OfflinePlayer newmember) {
try {
World w = p.getWorld();
RegionManager rm = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(w));
ProtectedRegion currentRegion = rm.getRegion(region.getId());
DefaultDomain currentMembers = currentRegion.getMembers();
currentMembers.addPlayer(UUID.fromString(newmember.getUniqueId().toString()));
rm.save();
return true;
}
catch (StorageException e) {
return false;
}
}
public static boolean addOwner(Player p, ProtectedRegion region, OfflinePlayer newowner) {
try {
World w = p.getWorld();
RegionManager rm = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(w));
ProtectedRegion currentRegion = rm.getRegion(region.getId());
DefaultDomain currentOwners = currentRegion.getOwners();
currentOwners.addPlayer(UUID.fromString(newowner.getUniqueId().toString()));
rm.save();
return true;
}
catch (StorageException e) {
return false;
}
}
public static boolean removeMember(Player p, ProtectedRegion region, OfflinePlayer newmember) {
try {
World w = p.getWorld();
RegionManager rm = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(w));
ProtectedRegion currentRegion = rm.getRegion(region.getId());
DefaultDomain currentMembers = currentRegion.getMembers();
currentMembers.removePlayer(UUID.fromString(newmember.getUniqueId().toString()));
rm.save();
return true;
}
catch (StorageException e) {
return false;
}
}
public static boolean removeOwner(Player p, ProtectedRegion region, UUID newowner) {
try {
World w = p.getWorld();
RegionManager rm = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(w));
ProtectedRegion currentRegion = rm.getRegion(region.getId());
DefaultDomain currentOwners = currentRegion.getOwners();
currentOwners.removePlayer(newowner);
rm.save();
return true;
}
catch (StorageException e) {
return false;
}
}
public static ArrayList<String> getMembers(Player p, ProtectedRegion region) {
World w = p.getWorld();
RegionManager rm = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(w));
ProtectedRegion currentRegion = rm.getRegion(region.getId());
DefaultDomain currentMembers = currentRegion.getMembers();
ArrayList<String> members = new ArrayList<>();
for (UUID uuid : currentMembers.getUniqueIds()) {
OfflinePlayer member = Bukkit.getServer().getOfflinePlayer(uuid);
members.add(member.getName());
}
return members;
}
public static ArrayList<String> getOwners(Player p, ProtectedRegion region) {
World w = p.getWorld();
RegionManager rm = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(w));
ProtectedRegion currentRegion = rm.getRegion(region.getId());
DefaultDomain currentOwners = currentRegion.getOwners();
ArrayList<String> owners = new ArrayList<>();
for (UUID uuid : currentOwners.getUniqueIds()) {
OfflinePlayer owner = Bukkit.getServer().getOfflinePlayer(uuid);
owners.add(owner.getName());
}
return owners;
}
public static boolean removeRegion(Player p, String name) {
try {
World w = p.getWorld();
RegionManager rm = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(w));
ProtectedRegion currentRegion = rm.getRegion(name);
if (currentRegion == null) {
return false;
}
rm.removeRegion(name);
rm.save();
return true;
}
catch (StorageException e) {
return false;
}
}
}

View file

@ -0,0 +1,116 @@
package me.mctp.radio;
import com.mpatric.mp3agic.InvalidDataException;
import com.mpatric.mp3agic.UnsupportedTagException;
import me.mctp.Main;
import me.mctp.managers.PinManager;
import me.mctp.utils.HeadUtil;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import org.json.simple.JSONObject;
import java.io.IOException;
public class Playlist {
private final me.mctp.api.maps.Playlist<String> songList = new me.mctp.api.maps.Playlist<>();
private boolean running = false;
private BukkitTask currentTimer;
/**
* Init, load all the songs from the data and start first
*/
public Playlist() {
init();
}
/**
* Load the songs into the system and start
*/
public void init() {
for (String URL : Main.getPlugin().getConfig().getStringList("RadioSongs")) {
addSong(URL);
}
Bukkit.getScheduler().runTaskAsynchronously(Main.getPlugin(), this::nextSong);
running = true;
}
/**
* Stop the playlist (clears the queue)
*/
public void stop() {
songList.clear();
if (currentTimer != null) {
currentTimer.cancel();
currentTimer = null;
}
running = false;
}
/**
* Add a song by the url
* @param url The song url (mp3)
*/
public void addSong(String url) {
songList.add(url);
songList.shuffle();
}
/**
* Go to the next song
*/
public void nextSong() {
if (currentTimer != null) return;
if (songList.isEmpty()) return;
//Get song
String nextURL = songList.getRandom();
if (nextURL == null) return;
JSONObject data;
for (Player p : Bukkit.getOnlinePlayers()) {
data = new JSONObject();
if (!PinManager.hasPin(p.getUniqueId())) continue;
data.put("task", "RADIO");
data.put("path", nextURL);
data.put("uuid", p.getUniqueId().toString().replace("-", ""));
Main.getClient().sendData(data);
}
int ticks;
try {
ticks = HeadUtil.getTicksOfFile(nextURL);
} catch (IOException | InvalidDataException | UnsupportedTagException e) {
e.printStackTrace();
nextSong();
return;
}
if (ticks == 0) {
Bukkit.getLogger().info("0 ticks");
nextSong();
return;
}
Bukkit.getLogger().info("Started song with duration: " + ticks/20 + " sec.");
currentTimer = Bukkit.getScheduler().runTaskLaterAsynchronously(Main.getPlugin(), () -> {
currentTimer = null;
nextSong();
}, ticks);
//Shuffle playlist again
songList.shuffle();
}
public boolean isRunning() {
return running;
}
}

View file

@ -0,0 +1,157 @@
package me.mctp.socket;
import me.mctp.commands.MCTPAudioCMD;
import me.mctp.Main;
import me.mctp.managers.PinManager;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.json.simple.JSONObject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.UUID;
public class Client {
private String url;
private int taskID = 0;
private int controlID = 0;
private WebSocketClient wsc;
private boolean connected = false;
public Client(String url) {
this.url = url;
}
public void connect() {
if (!this.connected) {
this.connected = true;
this.controlID = Bukkit.getScheduler().runTaskTimer(Main.getPlugin(), () -> {
if (!this.connected) {
Bukkit.getScheduler().cancelTask(this.controlID);
this.controlID = 0;
} else if (this.wsc == null || !this.wsc.isOpen()) {
if (this.wsc != null) {
this.wsc.closeConnection(404, "Disconnected from socket");
this.wsc = null;
}
this.connect();
}
}, 600L, 600L).getTaskId();
}
if (this.url != null && this.wsc == null) {
try {
URI uri = new URI(this.url + "?type=SERVER&");
this.wsc = new WebSocketClient(uri) {
@Override
public void onOpen(ServerHandshake serverHandshake) {}
@Override
public void onMessage(String s) {
JSONObject json = JSONUtil.parse(s);
if (json == null) return;
String str = JSONUtil.getValue(json, "task");
if (str == null || str.isEmpty()) return;
if (str.equals("VERIFY")) {
String uuid = JSONUtil.getValue(json, "uuid");
if (uuid == null || uuid.isEmpty()) return;
String pin = JSONUtil.getValue(json, "pin");
if (pin == null || pin.isEmpty()) return;
UUID pUUID = JSONUtil.formatFromInput(uuid);
boolean verified = false;
if (Bukkit.getPlayer(pUUID) != null) {
verified = PinManager.checkPin(pUUID, pin);
}
Player p = Bukkit.getPlayer(pUUID);
if (p != null && p.isOnline()) {
p.sendMessage(MCTPAudioCMD.prefix + "You are now connected with the audioclient.");
}
JSONObject reply = new JSONObject();
reply.put("task", "AUTHENTICATION");
reply.put("verified", verified);
reply.put("uuid", uuid);
this.send(reply.toJSONString());
} else if (str.equals("DISCONNECTED")) {
String uuid = JSONUtil.getValue(json, "uuid");
if (uuid == null || uuid.isEmpty()) return;
UUID pUUID = JSONUtil.formatFromInput(uuid);
Player p = Bukkit.getPlayer(pUUID);
if (p != null && p.isOnline()) {
p.sendMessage(MCTPAudioCMD.prefix + "You are now disconnected from the audioclient.");
}
}
}
@Override
public void onClose(int i, String s, boolean b) {
Client.this.wsc = null;
Bukkit.getScheduler().cancelTask(Client.this.taskID);
Client.this.taskID = 0;
}
@Override
public void onError(Exception e) {
e.printStackTrace();
}
};
this.wsc.connect();
if (this.taskID == 0) {
this.taskID = Bukkit.getScheduler().runTaskTimerAsynchronously(Main.getPlugin(), () -> {
if (Client.this.wsc != null && Client.this.wsc.isOpen()) {
Client.this.wsc.send("__PING__");
} else {
if (Client.this.wsc != null) {
Client.this.wsc.closeConnection(404, "Disconnected from socket");
Client.this.wsc = null;
}
Bukkit.getScheduler().cancelTask(Client.this.taskID);
Client.this.taskID = 0;
}
}, 200L, 200L).getTaskId();
}
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
public void disconnect() {
if (this.wsc != null) {
this.wsc.closeConnection(404, "Disconnected from socket");
}
this.wsc = null;
if (this.taskID != 0) {
Bukkit.getScheduler().cancelTask(this.taskID);
this.taskID = 0;
}
this.connected = false;
if (this.controlID != 0) {
Bukkit.getScheduler().cancelTask(this.controlID);
this.controlID = 0;
}
}
public void sendData(JSONObject object) {
if (this.wsc != null && this.wsc.isOpen() && object != null && object.toJSONString() != null) {
this.wsc.send(object.toJSONString());
}
}
}

View file

@ -0,0 +1,49 @@
package me.mctp.socket;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.util.UUID;
public class JSONUtil {
public static JSONObject parse(String string) {
try {
JSONParser parser = new JSONParser();
return (JSONObject) parser.parse(string);
} catch (ParseException ignored) { }
return null;
}
public static String getValue(JSONObject object, String string) {
if (object != null && !object.isEmpty()) {
Object obj = object.get(string);
String str = null;
if (obj != null) str = String.valueOf(obj);
return str;
}
return null;
}
public static UUID formatFromInput(String uuid) throws IllegalArgumentException{
if(uuid == null) throw new IllegalArgumentException();
uuid = uuid.trim();
return uuid.length() == 32 ? fromTrimmed(uuid.replaceAll("-", "")) : UUID.fromString(uuid);
}
public static UUID fromTrimmed(String trimmedUUID) throws IllegalArgumentException{
if(trimmedUUID == null) throw new IllegalArgumentException();
StringBuilder builder = new StringBuilder(trimmedUUID.trim());
/* Backwards adding to avoid index adjustments */
try {
builder.insert(20, "-");
builder.insert(16, "-");
builder.insert(12, "-");
builder.insert(8, "-");
} catch (StringIndexOutOfBoundsException e){
throw new IllegalArgumentException();
}
return UUID.fromString(builder.toString());
}
}

View file

@ -0,0 +1,84 @@
package me.mctp.utils;
import com.mpatric.mp3agic.InvalidDataException;
import com.mpatric.mp3agic.Mp3File;
import com.mpatric.mp3agic.UnsupportedTagException;
import org.apache.commons.io.FilenameUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class HeadUtil {
public static String getArgument(String selector, 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("\\.", "");
}
private static String downloadFromUrl(URL url, String localFilename) throws IOException {
InputStream is = null;
FileOutputStream fos = null;
String tempDir = System.getProperty("java.io.tmpdir");
String outputPath = tempDir + "/" + localFilename;
try {
//connect
URLConnection urlConn = url.openConnection();
//get inputstream from connection
is = urlConn.getInputStream();
fos = new FileOutputStream(outputPath);
// 4KB buffer
byte[] buffer = new byte[4096];
int length;
// read from source and write into local file
while ((length = is.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
return outputPath;
} finally {
try {
if (is != null) {
is.close();
}
} finally {
if (fos != null) {
fos.close();
}
}
}
}
public static int getTicksOfFile(String input) throws IOException, InvalidDataException, UnsupportedTagException {
URL url = new URL(input);
String result = downloadFromUrl(url, FilenameUtils.getName(url.getPath()));
File file = new File(result);
if (!file.exists()) return 0;
Mp3File mp3File = new Mp3File(file);
long sec = mp3File.getLengthInSeconds();
file.delete();
return (int) (sec * 20);
}
}

View file

@ -0,0 +1,156 @@
package me.mctp.utils;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import me.mctp.managers.WGManager;
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 final 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<Player> getPlayers(CommandSender commandSender) {
List<Player> 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) {
String regionID = getArgument("region");
for (Player p : Bukkit.getOnlinePlayers()) {
ArrayList<ProtectedRegion> regions = WGManager.getRegionsIn(p.getLocation());
if (regions.stream().anyMatch(region -> region.getId().equalsIgnoreCase(regionID))) {
players.add(p);
}
}
} 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("\\.", "");
}
}

View file

@ -0,0 +1,18 @@
name: MCTPAudio
version: ${project.version}
main: me.mctp.Main
api-version: 1.16
authors: [ SBDeveloper ]
depend: [ WorldGuard ]
description: Copyright MaybeFromNL & SBDeveloper
commands:
mctpaudio:
description: Main command
audio:
description: Connect command
permissions:
mctp.admin:
description: Admin commands
default: op