Bugfixes in the audio client

This commit is contained in:
stijnb1234 2021-02-01 13:02:15 +01:00
parent a93179ba68
commit af84892323
4 changed files with 47 additions and 26 deletions

View file

@ -1,6 +1,7 @@
package nl.sbdeveloper.mctpaudio.api.events;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
@ -11,11 +12,13 @@ import org.jetbrains.annotations.NotNull;
* Written by Stijn Bannink <stijnbannink23@gmail.com>, July 2020
*/
public class AudioConnectionUpdateEvent extends Event {
public class AudioConnectionUpdateEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final Player player;
private final boolean connected;
private boolean cancelled = false;
public AudioConnectionUpdateEvent(@NotNull Player who, boolean connected) {
super(true);
this.player = who;
@ -40,4 +43,15 @@ public class AudioConnectionUpdateEvent extends Event {
public static HandlerList getHandlerList() {
return handlers;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
if (!isConnected()) return; //Only works if the state is connected.
this.cancelled = cancelled;
}
}

View file

@ -7,6 +7,7 @@ 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.api.events.AudioConnectionUpdateEvent;
import nl.sbdeveloper.mctpaudio.managers.PinManager;
import nl.sbdeveloper.mctpaudio.managers.WGManager;
import nl.sbdeveloper.mctpaudio.utils.HeadUtil;
@ -170,21 +171,21 @@ public class PlayInRegionHandler implements Listener {
}
@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());
private void onConnectionUpdate(AudioConnectionUpdateEvent e) {
if (e.isConnected()) {
List<ProtectedRegion> regionsIn = WGManager.getRegionsIn(e.getPlayer().getLocation());
if (regionsIn.stream().anyMatch(reg -> reg.getId().equals(region))) {
players.add(e.getPlayer().getUniqueId());
e.setCancelled(true);
start(e.getPlayer().getUniqueId(), getMs());
}
} else {
if (!players.contains(e.getPlayer().getUniqueId())) return;
stop(e.getPlayer().getUniqueId());
players.remove(e.getPlayer().getUniqueId());
}
}
@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);
}

View file

@ -61,8 +61,6 @@ public class PinManager {
return false;
}
Bukkit.getLogger().info(pins.toString());
return pins.containsKey(pUUID) && pin.equals(pins.get(pUUID));
}

View file

@ -100,18 +100,26 @@ public class Client {
AudioConnectionUpdateEvent event = new AudioConnectionUpdateEvent(p, true);
Bukkit.getPluginManager().callEvent(event);
List<String> regions = WGManager.getRegionsIn(p.getLocation()).stream().map(ProtectedRegion::getId).collect(Collectors.toList());
Set<String> list = MCTPAudio.getPlugin().getConfig().getConfigurationSection("Regions").getKeys(false);
Optional<String> regionName = regions.stream().filter(list::contains).findFirst();
regionName.ifPresent(name -> {
String regionURL = MCTPAudio.getPlugin().getConfig().getString("Regions." + name);
if (!event.isCancelled()) {
List<String> regions = WGManager.getRegionsIn(p.getLocation()).stream().map(ProtectedRegion::getId).collect(Collectors.toList());
Set<String> list = MCTPAudio.getPlugin().getConfig().getConfigurationSection("Regions").getKeys(false);
Optional<String> regionName = regions.stream().filter(list::contains).findFirst();
regionName.ifPresentOrElse(name -> {
String regionURL = MCTPAudio.getPlugin().getConfig().getString("Regions." + name);
JSONObject data = new JSONObject();
data.put("task", "MUSIC");
data.put("path", regionURL);
data.put("uuid", p.getUniqueId().toString().replace("-", ""));
MCTPAudio.getClient().sendData(data);
});
JSONObject data = new JSONObject();
data.put("task", "MUSIC");
data.put("path", regionURL);
data.put("uuid", p.getUniqueId().toString().replace("-", ""));
MCTPAudio.getClient().sendData(data);
}, () -> {
JSONObject data = new JSONObject();
data.put("task", "SFX");
data.put("path", "http://audio.mcthemeparks.eu/assets/music/oaintro.mp3");
data.put("uuid", p.getUniqueId().toString().replace("-", ""));
MCTPAudio.getClient().sendData(data);
});
}
}
JSONObject reply = new JSONObject();