Fixed in/out playback for regions on SFX
This commit is contained in:
parent
af6a849a58
commit
a93179ba68
5 changed files with 211 additions and 4 deletions
|
@ -42,7 +42,7 @@ public final class MCTPAudio extends JavaPlugin {
|
|||
saveConfig();
|
||||
|
||||
Bukkit.getLogger().info("[MCTPAudio] Connecting with socket...");
|
||||
client = new Client("ws://173.249.31.58:8166");
|
||||
client = new Client("ws://81.16.136.67:25564");
|
||||
client.connect();
|
||||
|
||||
Bukkit.getLogger().info("[MCTPAudio] Loading commands and events...");
|
||||
|
|
|
@ -6,17 +6,20 @@ import co.aikar.commands.annotation.*;
|
|||
import nl.sbdeveloper.mctpaudio.MCTPAudio;
|
||||
import nl.sbdeveloper.mctpaudio.api.AudioType;
|
||||
import nl.sbdeveloper.mctpaudio.api.HueType;
|
||||
import nl.sbdeveloper.mctpaudio.listener.PlayInRegionHandler;
|
||||
import nl.sbdeveloper.mctpaudio.managers.PinManager;
|
||||
import nl.sbdeveloper.mctpaudio.utils.HeadUtil;
|
||||
import nl.sbdeveloper.mctpaudio.utils.SpigotPlayerSelector;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/* Copyright (C) McThemeParks - All Rights Reserved
|
||||
* Unauthorized copying of this file, via any medium is strictly prohibited
|
||||
|
@ -70,8 +73,17 @@ public class MCTPAudioCMD extends BaseCommand {
|
|||
players.add(target);
|
||||
} else {
|
||||
SpigotPlayerSelector sel = new SpigotPlayerSelector(selector);
|
||||
|
||||
if (sel.getArgument("region").length() != 0) {
|
||||
String regionID = sel.getArgument("region");
|
||||
new PlayInRegionHandler(regionID, url, sel.getPlayers(sender).stream().map(Entity::getUniqueId).collect(Collectors.toList()));
|
||||
|
||||
sender.sendMessage(ChatColor.GRAY + "Gestart met afspelen!");
|
||||
return;
|
||||
} else {
|
||||
players.addAll(sel.getPlayers(sender));
|
||||
}
|
||||
}
|
||||
|
||||
JSONObject data;
|
||||
for (Player p : players) {
|
||||
|
|
|
@ -0,0 +1,191 @@
|
|||
package nl.sbdeveloper.mctpaudio.listener;
|
||||
|
||||
import com.mpatric.mp3agic.InvalidDataException;
|
||||
import com.mpatric.mp3agic.UnsupportedTagException;
|
||||
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
|
||||
import net.raidstone.wgevents.events.RegionEnteredEvent;
|
||||
import net.raidstone.wgevents.events.RegionLeftEvent;
|
||||
import nl.sbdeveloper.mctpaudio.MCTPAudio;
|
||||
import nl.sbdeveloper.mctpaudio.api.AudioType;
|
||||
import nl.sbdeveloper.mctpaudio.managers.PinManager;
|
||||
import nl.sbdeveloper.mctpaudio.managers.WGManager;
|
||||
import nl.sbdeveloper.mctpaudio.utils.HeadUtil;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/* Copyright (C) McThemeParks - All Rights Reserved
|
||||
* Unauthorized copying of this file, via any medium is strictly prohibited
|
||||
* Proprietary and confidential
|
||||
* Written by Stijn Bannink <stijnbannink23@gmail.com>, July 2020
|
||||
*/
|
||||
|
||||
/**
|
||||
* This class handles playing numbers in a region.
|
||||
* This will NOT be used for region much, only if you start playing music in a region with the @a[region=...] selector.
|
||||
*/
|
||||
public class PlayInRegionHandler implements Listener {
|
||||
private final String region;
|
||||
private final String url;
|
||||
|
||||
private final List<UUID> players;
|
||||
private int currentTick = 0;
|
||||
private boolean isListening = true;
|
||||
|
||||
public PlayInRegionHandler(String region, String url, List<UUID> players) {
|
||||
this.region = region;
|
||||
this.url = url;
|
||||
this.players = players;
|
||||
|
||||
load();
|
||||
Bukkit.getPluginManager().registerEvents(this, MCTPAudio.getPlugin());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the song to every audio client, and starts it.
|
||||
*/
|
||||
private void load() {
|
||||
int ticks;
|
||||
try {
|
||||
ticks = HeadUtil.getTicksOfFile(url);
|
||||
} catch (IOException | InvalidDataException | UnsupportedTagException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
currentTick++;
|
||||
if (currentTick == ticks) {
|
||||
isListening = false;
|
||||
cancel();
|
||||
}
|
||||
}
|
||||
}.runTaskTimer(MCTPAudio.getPlugin(), 0L, 1L);
|
||||
|
||||
Player p;
|
||||
JSONObject data;
|
||||
Iterator<UUID> uuidS = players.iterator();
|
||||
while (uuidS.hasNext()) {
|
||||
p = Bukkit.getPlayer(uuidS.next());
|
||||
if (p == null || !PinManager.hasPin(p.getUniqueId())) {
|
||||
uuidS.remove();
|
||||
continue;
|
||||
}
|
||||
|
||||
data = new JSONObject();
|
||||
data.put("task", AudioType.SFX.name());
|
||||
data.put("path", url);
|
||||
data.put("uuid", p.getUniqueId().toString().replace("-", ""));
|
||||
data.put("play", false);
|
||||
MCTPAudio.getClient().sendData(data);
|
||||
}
|
||||
|
||||
startForAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the song for every player.
|
||||
*/
|
||||
private void startForAll() {
|
||||
players.forEach(this::start);
|
||||
}
|
||||
|
||||
private void start(UUID pUUID) {
|
||||
start(pUUID, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the song for a Player.
|
||||
* @param pUUID The player to start it for.
|
||||
*/
|
||||
private void start(UUID pUUID, int ms) {
|
||||
if (!isListening) return;
|
||||
|
||||
Player p = Bukkit.getPlayer(pUUID);
|
||||
if (p == null) return;
|
||||
if (!PinManager.hasPin(p.getUniqueId())) return;
|
||||
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("task", AudioType.SFX.name());
|
||||
data.put("path", url);
|
||||
data.put("uuid", p.getUniqueId().toString().replace("-", ""));
|
||||
data.put("play", true);
|
||||
data.put("at", ms);
|
||||
MCTPAudio.getClient().sendData(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the song for a Player.
|
||||
* @param pUUID The player to stop it for.
|
||||
*/
|
||||
private void stop(UUID pUUID) {
|
||||
if (!isListening) return;
|
||||
|
||||
Player p = Bukkit.getPlayer(pUUID);
|
||||
if (p == null) return;
|
||||
if (!PinManager.hasPin(p.getUniqueId())) return;
|
||||
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("task", AudioType.SFX.name());
|
||||
data.put("path", url);
|
||||
data.put("uuid", p.getUniqueId().toString().replace("-", ""));
|
||||
data.put("play", false);
|
||||
MCTPAudio.getClient().sendData(data);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
private void onEnterRegion(RegionEnteredEvent e) {
|
||||
if (e.getPlayer() == null) return;
|
||||
|
||||
if (e.getRegionName().equals(region)) {
|
||||
if (!players.contains(e.getPlayer().getUniqueId())) {
|
||||
players.add(e.getPlayer().getUniqueId());
|
||||
start(e.getPlayer().getUniqueId(), getMs());
|
||||
} else {
|
||||
start(e.getPlayer().getUniqueId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
private void onLeaveRegion(RegionLeftEvent e) {
|
||||
if (e.getPlayer() == null) return;
|
||||
|
||||
if (e.getRegionName().equals(region)) {
|
||||
if (!players.contains(e.getPlayer().getUniqueId())) return;
|
||||
stop(e.getPlayer().getUniqueId());
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
private void onJoin(PlayerJoinEvent e) {
|
||||
List<ProtectedRegion> regionsIn = WGManager.getRegionsIn(e.getPlayer().getLocation());
|
||||
if (regionsIn.stream().anyMatch(reg -> reg.getId().equals(region))) {
|
||||
players.add(e.getPlayer().getUniqueId());
|
||||
start(e.getPlayer().getUniqueId(), getMs());
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
private void onQuit(PlayerQuitEvent e) {
|
||||
if (!players.contains(e.getPlayer().getUniqueId())) return;
|
||||
stop(e.getPlayer().getUniqueId());
|
||||
players.remove(e.getPlayer().getUniqueId());
|
||||
}
|
||||
|
||||
private int getMs() {
|
||||
return Math.round(currentTick * 50);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package nl.sbdeveloper.mctpaudio.managers;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.UUID;
|
||||
import java.util.WeakHashMap;
|
||||
|
@ -59,6 +61,8 @@ public class PinManager {
|
|||
return false;
|
||||
}
|
||||
|
||||
Bukkit.getLogger().info(pins.toString());
|
||||
|
||||
return pins.containsKey(pUUID) && pin.equals(pins.get(pUUID));
|
||||
}
|
||||
|
||||
|
|
|
@ -111,7 +111,7 @@ public class SpigotPlayerSelector {
|
|||
* @param commandSender the sender
|
||||
* @return the location or null
|
||||
*/
|
||||
private Location getLocation(CommandSender commandSender) {
|
||||
public Location getLocation(CommandSender commandSender) {
|
||||
Location initialLocation = new Location(Bukkit.getWorlds().get(0), 0, 0, 0);
|
||||
|
||||
if (commandSender instanceof Player) {
|
||||
|
@ -135,7 +135,7 @@ public class SpigotPlayerSelector {
|
|||
return initialLocation;
|
||||
}
|
||||
|
||||
private String getArgument(String key) {
|
||||
public String getArgument(String key) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
String[] arguments = selector.split(key + "=");
|
||||
if (arguments.length == 1) return "";
|
||||
|
|
Loading…
Add table
Reference in a new issue