From 86ef27f742419e8e85fbbbd58c4cef3953c40218 Mon Sep 17 00:00:00 2001 From: Alejandro <25-alejandro@users.noreply.git.krews.org> Date: Sun, 16 Jun 2019 14:44:20 +0300 Subject: [PATCH] Add add youtube playlist command and fix update command --- sqlupdates/2_0_0_TO_2_1_0-RC-1.sql | 2 +- sqlupdates/2_1_0-RC-1_TO_2_1_0-RC-2.sql | 8 +++ .../commands/AddYoutubePlaylistCommand.java | 59 +++++++++++++++++++ .../habbohotel/commands/CommandHandler.java | 2 + .../habbohotel/items/YoutubeManager.java | 14 +++-- 5 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 sqlupdates/2_1_0-RC-1_TO_2_1_0-RC-2.sql create mode 100644 src/main/java/com/eu/habbo/habbohotel/commands/AddYoutubePlaylistCommand.java diff --git a/sqlupdates/2_0_0_TO_2_1_0-RC-1.sql b/sqlupdates/2_0_0_TO_2_1_0-RC-1.sql index 8d2c7349..be6dc99c 100644 --- a/sqlupdates/2_0_0_TO_2_1_0-RC-1.sql +++ b/sqlupdates/2_0_0_TO_2_1_0-RC-1.sql @@ -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; diff --git a/sqlupdates/2_1_0-RC-1_TO_2_1_0-RC-2.sql b/sqlupdates/2_1_0-RC-1_TO_2_1_0-RC-2.sql new file mode 100644 index 00000000..c3781754 --- /dev/null +++ b/sqlupdates/2_1_0-RC-1_TO_2_1_0-RC-2.sql @@ -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!'); \ No newline at end of file diff --git a/src/main/java/com/eu/habbo/habbohotel/commands/AddYoutubePlaylistCommand.java b/src/main/java/com/eu/habbo/habbohotel/commands/AddYoutubePlaylistCommand.java new file mode 100644 index 00000000..5318102b --- /dev/null +++ b/src/main/java/com/eu/habbo/habbohotel/commands/AddYoutubePlaylistCommand.java @@ -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; + } +} diff --git a/src/main/java/com/eu/habbo/habbohotel/commands/CommandHandler.java b/src/main/java/com/eu/habbo/habbohotel/commands/CommandHandler.java index 1ba4be67..d51eb126 100644 --- a/src/main/java/com/eu/habbo/habbohotel/commands/CommandHandler.java +++ b/src/main/java/com/eu/habbo/habbohotel/commands/CommandHandler.java @@ -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()); } diff --git a/src/main/java/com/eu/habbo/habbohotel/items/YoutubeManager.java b/src/main/java/com/eu/habbo/habbohotel/items/YoutubeManager.java index 7e6ece37..631f15d5 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/YoutubeManager.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/YoutubeManager.java @@ -90,11 +90,9 @@ public class YoutubeManager { youtubeDataLoaderPool.submit(() -> { ArrayList 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 getPlaylistsForItemId(int itemId) { return this.playlists.get(itemId); } + + public void addPlaylistToItem(int itemId, YoutubePlaylist playlist) { + this.playlists.computeIfAbsent(itemId, k -> new ArrayList<>()).add(playlist); + } } \ No newline at end of file