Bugfixes in the audio client
This commit is contained in:
parent
a93179ba68
commit
af84892323
4 changed files with 47 additions and 26 deletions
|
@ -1,6 +1,7 @@
|
||||||
package nl.sbdeveloper.mctpaudio.api.events;
|
package nl.sbdeveloper.mctpaudio.api.events;
|
||||||
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.Cancellable;
|
||||||
import org.bukkit.event.Event;
|
import org.bukkit.event.Event;
|
||||||
import org.bukkit.event.HandlerList;
|
import org.bukkit.event.HandlerList;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
@ -11,11 +12,13 @@ import org.jetbrains.annotations.NotNull;
|
||||||
* Written by Stijn Bannink <stijnbannink23@gmail.com>, July 2020
|
* 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 static final HandlerList handlers = new HandlerList();
|
||||||
private final Player player;
|
private final Player player;
|
||||||
private final boolean connected;
|
private final boolean connected;
|
||||||
|
|
||||||
|
private boolean cancelled = false;
|
||||||
|
|
||||||
public AudioConnectionUpdateEvent(@NotNull Player who, boolean connected) {
|
public AudioConnectionUpdateEvent(@NotNull Player who, boolean connected) {
|
||||||
super(true);
|
super(true);
|
||||||
this.player = who;
|
this.player = who;
|
||||||
|
@ -40,4 +43,15 @@ public class AudioConnectionUpdateEvent extends Event {
|
||||||
public static HandlerList getHandlerList() {
|
public static HandlerList getHandlerList() {
|
||||||
return handlers;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import net.raidstone.wgevents.events.RegionEnteredEvent;
|
||||||
import net.raidstone.wgevents.events.RegionLeftEvent;
|
import net.raidstone.wgevents.events.RegionLeftEvent;
|
||||||
import nl.sbdeveloper.mctpaudio.MCTPAudio;
|
import nl.sbdeveloper.mctpaudio.MCTPAudio;
|
||||||
import nl.sbdeveloper.mctpaudio.api.AudioType;
|
import nl.sbdeveloper.mctpaudio.api.AudioType;
|
||||||
|
import nl.sbdeveloper.mctpaudio.api.events.AudioConnectionUpdateEvent;
|
||||||
import nl.sbdeveloper.mctpaudio.managers.PinManager;
|
import nl.sbdeveloper.mctpaudio.managers.PinManager;
|
||||||
import nl.sbdeveloper.mctpaudio.managers.WGManager;
|
import nl.sbdeveloper.mctpaudio.managers.WGManager;
|
||||||
import nl.sbdeveloper.mctpaudio.utils.HeadUtil;
|
import nl.sbdeveloper.mctpaudio.utils.HeadUtil;
|
||||||
|
@ -170,21 +171,21 @@ public class PlayInRegionHandler implements Listener {
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
private void onJoin(PlayerJoinEvent e) {
|
private void onConnectionUpdate(AudioConnectionUpdateEvent e) {
|
||||||
List<ProtectedRegion> regionsIn = WGManager.getRegionsIn(e.getPlayer().getLocation());
|
if (e.isConnected()) {
|
||||||
if (regionsIn.stream().anyMatch(reg -> reg.getId().equals(region))) {
|
List<ProtectedRegion> regionsIn = WGManager.getRegionsIn(e.getPlayer().getLocation());
|
||||||
players.add(e.getPlayer().getUniqueId());
|
if (regionsIn.stream().anyMatch(reg -> reg.getId().equals(region))) {
|
||||||
start(e.getPlayer().getUniqueId(), getMs());
|
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() {
|
private int getMs() {
|
||||||
return Math.round(currentTick * 50);
|
return Math.round(currentTick * 50);
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,8 +61,6 @@ public class PinManager {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Bukkit.getLogger().info(pins.toString());
|
|
||||||
|
|
||||||
return pins.containsKey(pUUID) && pin.equals(pins.get(pUUID));
|
return pins.containsKey(pUUID) && pin.equals(pins.get(pUUID));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -100,18 +100,26 @@ public class Client {
|
||||||
AudioConnectionUpdateEvent event = new AudioConnectionUpdateEvent(p, true);
|
AudioConnectionUpdateEvent event = new AudioConnectionUpdateEvent(p, true);
|
||||||
Bukkit.getPluginManager().callEvent(event);
|
Bukkit.getPluginManager().callEvent(event);
|
||||||
|
|
||||||
List<String> regions = WGManager.getRegionsIn(p.getLocation()).stream().map(ProtectedRegion::getId).collect(Collectors.toList());
|
if (!event.isCancelled()) {
|
||||||
Set<String> list = MCTPAudio.getPlugin().getConfig().getConfigurationSection("Regions").getKeys(false);
|
List<String> regions = WGManager.getRegionsIn(p.getLocation()).stream().map(ProtectedRegion::getId).collect(Collectors.toList());
|
||||||
Optional<String> regionName = regions.stream().filter(list::contains).findFirst();
|
Set<String> list = MCTPAudio.getPlugin().getConfig().getConfigurationSection("Regions").getKeys(false);
|
||||||
regionName.ifPresent(name -> {
|
Optional<String> regionName = regions.stream().filter(list::contains).findFirst();
|
||||||
String regionURL = MCTPAudio.getPlugin().getConfig().getString("Regions." + name);
|
regionName.ifPresentOrElse(name -> {
|
||||||
|
String regionURL = MCTPAudio.getPlugin().getConfig().getString("Regions." + name);
|
||||||
|
|
||||||
JSONObject data = new JSONObject();
|
JSONObject data = new JSONObject();
|
||||||
data.put("task", "MUSIC");
|
data.put("task", "MUSIC");
|
||||||
data.put("path", regionURL);
|
data.put("path", regionURL);
|
||||||
data.put("uuid", p.getUniqueId().toString().replace("-", ""));
|
data.put("uuid", p.getUniqueId().toString().replace("-", ""));
|
||||||
MCTPAudio.getClient().sendData(data);
|
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();
|
JSONObject reply = new JSONObject();
|
||||||
|
|
Loading…
Add table
Reference in a new issue