Add add youtube playlist command and fix update command

This commit is contained in:
Alejandro 2019-06-16 14:44:20 +03:00
parent 166a4cd77b
commit 86ef27f742
5 changed files with 80 additions and 5 deletions

View File

@ -67,7 +67,7 @@ DROP PROCEDURE IF EXISTS DEFAULT_YTTV_PLAYLISTS;
ALTER TABLE `permissions`
ADD COLUMN `cmd_update_youtube_playlists` enum('0','1') NOT NULL DEFAULT '0';
INSERT INTO `emulator_texts`(`key`, `value`) VALUES ('commands.keys.cmd_update_youtube_playlists', 'update_youtube;update_youtube_playlists')
INSERT INTO `emulator_texts`(`key`, `value`) VALUES ('commands.keys.cmd_update_youtube_playlists', 'update_youtube;update_youtube_playlists');
INSERT INTO `emulator_texts`(`key`, `value`) VALUES ('commands.succes.cmd_update_youtube_playlists', 'YouTube playlists have been refreshed!');
DROP PROCEDURE IF EXISTS UPDATE_TEAM_WIREDS;

View File

@ -0,0 +1,8 @@
ALTER TABLE `permissions`
ADD COLUMN `cmd_add_youtube_playlist` enum('0','1') NOT NULL DEFAULT '0';
INSERT INTO `emulator_texts`(`key`, `value`) VALUES ('commands.keys.cmd_add_youtube_playlist', 'add_youtube;add_playlist;add_youtube_playlist');
INSERT INTO `emulator_texts`(`key`, `value`) VALUES ('commands.error.cmd_add_youtube_playlist.usage', 'Usage: base_item_id youtube_playlist_id');
INSERT INTO `emulator_texts`(`key`, `value`) VALUES ('commands.error.cmd_add_youtube_playlist.no_base_item', 'A base item with that ID could not be found.');
INSERT INTO `emulator_texts`(`key`, `value`) VALUES ('commands.error.cmd_add_youtube_playlist.failed_playlist', 'Error: unable to fetch the given YouTube playlist.');
INSERT INTO `emulator_texts`(`key`, `value`) VALUES ('commands.succes.cmd_add_youtube_playlist', 'The playlist has been added successfully!');

View File

@ -0,0 +1,59 @@
package com.eu.habbo.habbohotel.commands;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.gameclients.GameClient;
import com.eu.habbo.habbohotel.items.YoutubeManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class AddYoutubePlaylistCommand extends Command {
public AddYoutubePlaylistCommand() {
super("cmd_add_youtube_playlist", Emulator.getTexts().getValue("commands.keys.cmd_add_youtube_playlist").split(";"));
}
@Override
public boolean handle(GameClient gameClient, String[] params) throws Exception {
if (params.length < 3) {
gameClient.getHabbo().whisper(Emulator.getTexts().getValue("commands.error.cmd_add_youtube_playlist.usage"));
return true;
}
int itemId;
try {
itemId = Integer.valueOf(params[1]);
} catch (NumberFormatException e) {
gameClient.getHabbo().whisper(Emulator.getTexts().getValue("commands.error.cmd_add_youtube_playlist.no_base_item"));
return true;
}
if (Emulator.getGameEnvironment().getItemManager().getItem(itemId) == null) {
gameClient.getHabbo().whisper(Emulator.getTexts().getValue("commands.error.cmd_add_youtube_playlist.no_base_item"));
return true;
}
YoutubeManager.YoutubePlaylist playlist = Emulator.getGameEnvironment().getItemManager().getYoutubeManager().getPlaylistDataById(params[2]);
if (playlist == null) {
gameClient.getHabbo().whisper(Emulator.getTexts().getValue("commands.error.cmd_add_youtube_playlist.failed_playlist"));
return true;
}
Emulator.getGameEnvironment().getItemManager().getYoutubeManager().addPlaylistToItem(Integer.valueOf(params[1]), playlist);
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO `youtube_playlists` (`item_id`, `playlist_id`) VALUES (?, ?)")) {
statement.setInt(1, itemId);
statement.setString(2, params[2]);
statement.execute();
} catch (SQLException e) {
Emulator.getLogging().logSQLException(e);
}
gameClient.getHabbo().whisper(Emulator.getTexts().getValue("commands.succes.cmd_add_youtube_playlist"));
return true;
}
}

View File

@ -276,6 +276,8 @@ public class CommandHandler {
addCommand(new UpdateWordFilterCommand());
addCommand(new UserInfoCommand());
addCommand(new WordQuizCommand());
addCommand(new UpdateYoutubePlaylistsCommand());
addCommand(new AddYoutubePlaylistCommand());
addCommand(new TestCommand());
}

View File

@ -90,11 +90,9 @@ public class YoutubeManager {
youtubeDataLoaderPool.submit(() -> {
ArrayList<YoutubePlaylist> playlists = this.playlists.getOrDefault(itemId, new ArrayList<>());
YoutubePlaylist playlist = this.playlistCache.containsKey(playlistId) ? this.playlistCache.get(playlistId) : this.getPlaylistDataById(playlistId);
YoutubePlaylist playlist = this.getPlaylistDataById(playlistId);
if (playlist != null) {
playlists.add(playlist);
this.playlistCache.put(playlistId, playlist);
} else {
Emulator.getLogging().logErrorLine("Failed to load YouTube playlist: " + playlistId);
}
@ -117,7 +115,9 @@ public class YoutubeManager {
Emulator.getLogging().logStart("YouTube Manager -> Loaded! (" + (System.currentTimeMillis() - millis) + " MS)");
}
private YoutubePlaylist getPlaylistDataById(String playlistId) {
public YoutubePlaylist getPlaylistDataById(String playlistId) {
if (this.playlistCache.containsKey(playlistId)) return this.playlistCache.get(playlistId);
try {
URL myUrl = new URL("https://www.youtube.com/playlist?list=" + playlistId);
@ -159,6 +159,8 @@ public class YoutubeManager {
br.close();
this.playlistCache.put(playlistId, playlist);
return playlist;
} catch (java.io.IOException e) {
e.printStackTrace();
@ -170,4 +172,8 @@ public class YoutubeManager {
public ArrayList<YoutubePlaylist> getPlaylistsForItemId(int itemId) {
return this.playlists.get(itemId);
}
public void addPlaylistToItem(int itemId, YoutubePlaylist playlist) {
this.playlists.computeIfAbsent(itemId, k -> new ArrayList<>()).add(playlist);
}
}