Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/items/YoutubeManager.java

102 lines
3.3 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.items;
import com.eu.habbo.Emulator;
import gnu.trove.map.hash.THashMap;
2018-09-28 21:25:00 +02:00
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
2018-07-06 15:30:00 +02:00
import java.util.ArrayList;
2019-05-26 20:14:53 +02:00
public class YoutubeManager {
2018-09-28 21:25:00 +02:00
public THashMap<Integer, ArrayList<YoutubeItem>> playLists = new THashMap<>();
public THashMap<Integer, YoutubeItem> videos = new THashMap<>();
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public void load() {
2018-07-06 15:30:00 +02:00
this.videos.clear();
this.playLists.clear();
2019-05-26 20:14:53 +02:00
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection()) {
try (Statement statement = connection.createStatement()) {
try (ResultSet set = statement.executeQuery("SELECT * FROM youtube_items")) {
while (set.next()) {
2018-07-06 15:30:00 +02:00
this.videos.put(set.getInt("id"), new YoutubeItem(set));
}
}
2019-05-26 20:14:53 +02:00
try (ResultSet set = statement.executeQuery("SELECT * FROM youtube_playlists ORDER BY `order` ASC")) {
while (set.next()) {
if (!this.playLists.containsKey(set.getInt("item_id"))) {
2018-09-28 21:25:00 +02:00
this.playLists.put(set.getInt("item_id"), new ArrayList<>());
2018-07-06 15:30:00 +02:00
}
YoutubeItem item = this.videos.get(set.getInt("video_id"));
2019-05-26 20:14:53 +02:00
if (item != null) {
2018-07-06 15:30:00 +02:00
this.playLists.get(set.getInt("item_id")).add(item);
}
}
}
}
2019-05-26 20:14:53 +02:00
} catch (SQLException e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logSQLException(e);
}
}
2019-05-26 20:14:53 +02:00
public ArrayList<YoutubeItem> getPlaylist(Item item) {
if (this.playLists.containsKey(item.getId())) {
2018-07-06 15:30:00 +02:00
return this.playLists.get(item.getId());
}
2018-09-28 21:25:00 +02:00
return new ArrayList<>();
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public YoutubeItem getVideo(Item item, String video) {
if (this.playLists.contains(item.getId())) {
for (YoutubeItem v : this.playLists.get(item.getId())) {
if (v.video.equalsIgnoreCase(video)) {
2018-07-06 15:30:00 +02:00
return v;
}
}
}
return null;
}
2019-05-26 20:14:53 +02:00
public String getPreviewImage(Item item) {
if (this.playLists.contains(item.getId())) {
if (!this.playLists.get(item.getId()).isEmpty()) {
2018-07-06 15:30:00 +02:00
return this.playLists.get(item.getId()).get(0).video;
}
}
return "";
}
2019-05-26 20:14:53 +02:00
public YoutubeItem getVideo(Item item, int index) {
if (this.playLists.containsKey(item.getId())) {
2018-07-06 15:30:00 +02:00
return this.playLists.get(item.getId()).get(index);
}
return null;
}
2019-05-26 20:14:53 +02:00
public class YoutubeItem {
2018-07-06 15:30:00 +02:00
public final int id;
public final String video;
public final String title;
public final String description;
public final int startTime;
public final int endTime;
2019-05-26 20:14:53 +02:00
public YoutubeItem(ResultSet set) throws SQLException {
2018-07-06 15:30:00 +02:00
this.id = set.getInt("id");
this.video = set.getString("video");
this.title = set.getString("title");
this.description = set.getString("description");
this.startTime = set.getInt("start_time");
this.endTime = set.getInt("end_time");
}
}
}