Improved code and fixed some things
This commit is contained in:
parent
af84892323
commit
fd67ae6f32
7 changed files with 128 additions and 57 deletions
16
pom.xml
16
pom.xml
|
@ -80,6 +80,10 @@
|
|||
<id>sk89q-repo</id>
|
||||
<url>https://maven.enginehub.org/repo/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>MG-Dev Jenkins CI Maven Repository</id>
|
||||
<url>https://ci.mg-dev.eu/plugin/repository/everything</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
|
@ -127,5 +131,17 @@
|
|||
<version>0.5.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.bergerkiller.bukkit</groupId>
|
||||
<artifactId>BKCommonLib</artifactId>
|
||||
<version>1.16.5-v2</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.bergerkiller.bukkit</groupId>
|
||||
<artifactId>TrainCarts</artifactId>
|
||||
<version>1.16.5-v1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -15,26 +15,38 @@ import java.util.Random;
|
|||
* @param <E> The type you want to store
|
||||
*/
|
||||
public class SongList<E> extends ArrayList<E> {
|
||||
public void shuffle() {
|
||||
Random random = new Random();
|
||||
private final Random r = new Random();
|
||||
|
||||
for (int index = 0; index < this.size(); index++) {
|
||||
int secondIndex = random.nextInt(this.size());
|
||||
/**
|
||||
* Shuffle this List
|
||||
*/
|
||||
public void shuffle() {
|
||||
for (int index = 0; index < size(); index++) {
|
||||
int secondIndex = r.nextInt(size());
|
||||
swap(index, secondIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a random item from this List
|
||||
*
|
||||
* @return The random element
|
||||
*/
|
||||
public E getRandom() {
|
||||
Random random = new Random();
|
||||
|
||||
return this.get(random.nextInt(this.size()));
|
||||
return get(r.nextInt(size()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Swaps two elements in an {@link ArrayList}
|
||||
*
|
||||
* @param firstIndex The first index
|
||||
* @param secondIndex The second index
|
||||
*/
|
||||
private void swap(int firstIndex, int secondIndex) {
|
||||
E first = this.get(firstIndex);
|
||||
E second = this.get(secondIndex);
|
||||
E first = get(firstIndex);
|
||||
E second = get(secondIndex);
|
||||
|
||||
this.set(secondIndex, first);
|
||||
this.set(firstIndex, second);
|
||||
set(secondIndex, first);
|
||||
set(firstIndex, second);
|
||||
}
|
||||
}
|
|
@ -15,8 +15,6 @@ 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;
|
||||
|
||||
|
|
|
@ -1,11 +1,19 @@
|
|||
package nl.sbdeveloper.mctpaudio.listener;
|
||||
|
||||
import com.bergerkiller.bukkit.common.entity.CommonEntityController;
|
||||
import com.bergerkiller.bukkit.common.events.EntityMoveEvent;
|
||||
import com.bergerkiller.bukkit.tc.controller.MinecartGroup;
|
||||
import com.bergerkiller.bukkit.tc.controller.MinecartGroupStore;
|
||||
import com.bergerkiller.bukkit.tc.controller.MinecartMember;
|
||||
import com.bergerkiller.bukkit.tc.controller.MinecartMemberStore;
|
||||
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
|
||||
import net.raidstone.wgevents.events.RegionsEnteredEvent;
|
||||
import nl.sbdeveloper.mctpaudio.MCTPAudio;
|
||||
import nl.sbdeveloper.mctpaudio.managers.PinManager;
|
||||
import nl.sbdeveloper.mctpaudio.managers.WGManager;
|
||||
import net.raidstone.wgevents.events.RegionsEnteredEvent;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
@ -27,28 +35,53 @@ public class WGListener implements Listener {
|
|||
* Music detection
|
||||
*/
|
||||
@EventHandler
|
||||
public void onMove(PlayerMoveEvent e) {
|
||||
public void onPlayerMove(PlayerMoveEvent e) {
|
||||
handlePlayerMovement(e.getPlayer(), e.getFrom(), e.getTo());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onTeleport(PlayerTeleportEvent e) {
|
||||
public void onPlayerTeleport(PlayerTeleportEvent e) {
|
||||
handlePlayerMovement(e.getPlayer(), e.getFrom(), e.getTo());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onEntityMove(EntityMoveEvent e) {
|
||||
MinecartGroup group = MinecartGroupStore.get(e.getEntity());
|
||||
MinecartMember<?> member = MinecartMemberStore.getFromEntity(e.getEntity());
|
||||
|
||||
if (group != null && group.stream().map(gMember -> gMember.getEntity().getPlayerPassengers()).mapToLong(Collection::size).sum() != 0) {
|
||||
group.stream().map(CommonEntityController::getEntity).forEach(ent ->
|
||||
ent.getPlayerPassengers().forEach(p -> handlePlayerMovement(p, ent.getLastLocation(), ent.getLocation())));
|
||||
} else if (member != null && !member.getEntity().getPlayerPassengers().isEmpty()) {
|
||||
member.getEntity().getPlayerPassengers().forEach(p ->
|
||||
handlePlayerMovement(p, member.getEntity().getLastLocation(), member.getEntity().getLocation()));
|
||||
}
|
||||
}
|
||||
|
||||
private void handlePlayerMovement(Player player, Location from, Location to) {
|
||||
if (to == null || from.getWorld() == null || to.getWorld() == null) return;
|
||||
|
||||
if (from.getBlockX() != to.getBlockX() || from.getBlockY() != to.getBlockY() || from.getBlockZ() != to.getBlockZ()) {
|
||||
Set<String> list = MCTPAudio.getPlugin().getConfig().getConfigurationSection("Regions").getKeys(false);
|
||||
if (!PinManager.hasPin(player.getUniqueId())) return;
|
||||
|
||||
if (!PinManager.hasPin(Objects.requireNonNull(player).getUniqueId())) return;
|
||||
Set<String> list = MCTPAudio.getPlugin().getConfig().getConfigurationSection("Regions").getKeys(false);
|
||||
|
||||
List<String> fromRegions = WGManager.getRegionsIn(from).stream().map(ProtectedRegion::getId).collect(Collectors.toList());
|
||||
List<String> toRegions = WGManager.getRegionsIn(to).stream().map(ProtectedRegion::getId).collect(Collectors.toList());
|
||||
|
||||
if ((Collections.disjoint(list, fromRegions) && !Collections.disjoint(list, toRegions)) || (!Collections.disjoint(list, fromRegions) && !Collections.disjoint(list, toRegions))) {
|
||||
//Walked in a region
|
||||
boolean found = false;
|
||||
for (Entity ent : to.getChunk().getEntities()) {
|
||||
MinecartMember<?> member = MinecartMemberStore.getFromEntity(ent);
|
||||
if (member == null) continue;
|
||||
if (member.getEntity().getPlayerPassengers().contains(player)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found) return; //The player is inside a vehicle, ignore...
|
||||
|
||||
if (!Collections.disjoint(list, fromRegions) && !Collections.disjoint(list, toRegions)) {
|
||||
Optional<String> name = fromRegions.stream().filter(list::contains).findFirst();
|
||||
|
|
|
@ -25,6 +25,7 @@ public class Playlist {
|
|||
|
||||
private boolean running = false;
|
||||
private BukkitTask currentTimer;
|
||||
private int faultCounter = 0;
|
||||
|
||||
/**
|
||||
* Create a new PlayList object
|
||||
|
@ -75,21 +76,44 @@ public class Playlist {
|
|||
* Go to the next song
|
||||
*/
|
||||
public void nextSong() {
|
||||
if (currentTimer != null) return;
|
||||
if (currentTimer != null) return; //A song is playing?
|
||||
|
||||
if (faultCounter > 4) stop(); //FALLBACK! 4 errors occured.
|
||||
|
||||
if (playList.isEmpty()) {
|
||||
//All numbers played
|
||||
playList = (SongList<String>) playedList.clone(); //Copy a clone.
|
||||
playedList.clear();
|
||||
start();
|
||||
}
|
||||
|
||||
//Get song
|
||||
String nextURL = playList.getRandom();
|
||||
if (nextURL == null) return;
|
||||
|
||||
//Remove from PlayList and add to PlayedList
|
||||
playList.remove(nextURL);
|
||||
playedList.add(nextURL);
|
||||
|
||||
//Get ticks of song
|
||||
int ticks;
|
||||
try {
|
||||
ticks = HeadUtil.getTicksOfFile(nextURL);
|
||||
} catch (InvalidDataException | UnsupportedTagException ex) {
|
||||
ex.printStackTrace();
|
||||
nextSong();
|
||||
return;
|
||||
} catch (IOException ex) {
|
||||
faultCounter++;
|
||||
nextSong();
|
||||
return;
|
||||
}
|
||||
|
||||
if (ticks == 0) {
|
||||
Bukkit.getLogger().info("0 ticks");
|
||||
nextSong();
|
||||
return;
|
||||
}
|
||||
|
||||
//Send to client
|
||||
JSONObject data;
|
||||
for (Player p : Bukkit.getOnlinePlayers()) {
|
||||
data = new JSONObject();
|
||||
|
@ -102,29 +126,17 @@ public class Playlist {
|
|||
MCTPAudio.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.");
|
||||
|
||||
faultCounter = 0;
|
||||
|
||||
//And started timer, so that it starts a new song if the old one is done
|
||||
currentTimer = Bukkit.getScheduler().runTaskLaterAsynchronously(MCTPAudio.getPlugin(), () -> {
|
||||
currentTimer = null;
|
||||
nextSong();
|
||||
}, ticks);
|
||||
|
||||
//Shuffle playlist again
|
||||
//And shuffle the playlist now
|
||||
playList.shuffle();
|
||||
}
|
||||
|
||||
|
|
|
@ -100,26 +100,26 @@ public class Client {
|
|||
AudioConnectionUpdateEvent event = new AudioConnectionUpdateEvent(p, true);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
|
||||
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);
|
||||
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", "SFX");
|
||||
data.put("path", "http://audio.mcthemeparks.eu/assets/music/oaintro.mp3");
|
||||
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);
|
||||
}, () -> {
|
||||
if (!event.isCancelled()) return;
|
||||
|
||||
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();
|
||||
|
|
|
@ -2,7 +2,7 @@ name: MCTPAudio
|
|||
version: ${project.version}
|
||||
main: nl.sbdeveloper.mctpaudio.MCTPAudio
|
||||
api-version: 1.16
|
||||
depend: [ WorldGuard ]
|
||||
depend: [ WorldGuard, Train_Carts ]
|
||||
authors: [ SBDeveloper ]
|
||||
description: The audio plugin of MCThemeParks!
|
||||
website: https://mcthemeparks.eu
|
||||
|
|
Loading…
Add table
Reference in a new issue