From 99168d57a5e2331dd5a2d7a6c3fb4f141e80aaec Mon Sep 17 00:00:00 2001 From: Alejandro <25-alejandro@users.noreply.git.krews.org> Date: Sat, 1 Feb 2020 14:09:59 +0200 Subject: [PATCH] Fix jukebox disc adding/removing --- .../interactions/InteractionMusicDisc.java | 13 +-- .../habbo/habbohotel/rooms/TraxManager.java | 103 ++++++++++++------ .../jukebox/JukeBoxAddSoundTrackEvent.java | 17 ++- .../rooms/items/jukebox/JukeBoxEventOne.java | 2 +- .../rooms/items/jukebox/JukeBoxEventTwo.java | 2 +- .../jukebox/JukeBoxRequestPlayListEvent.java | 2 +- 6 files changed, 84 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionMusicDisc.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionMusicDisc.java index a0e245b7..9a163257 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionMusicDisc.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionMusicDisc.java @@ -13,7 +13,6 @@ import java.sql.SQLException; public class InteractionMusicDisc extends HabboItem { private int songId; - private boolean inQueue; public InteractionMusicDisc(ResultSet set, Item baseItem) throws SQLException { super(set, baseItem); @@ -74,21 +73,13 @@ public class InteractionMusicDisc extends HabboItem { public void onPlace(Room room) { super.onPlace(room); - room.sendComposer(new JukeBoxMySongsComposer(room.getTraxManager().myList()).compose()); + room.getTraxManager().sendUpdatedSongList(); } @Override public void onPickUp(Room room) { super.onPickUp(room); - room.getTraxManager().removeSong(this.getId()); - } - - public boolean inQueue() { - return this.inQueue; - } - - public void inQueue(boolean inQueue) { - this.inQueue = inQueue; + room.getTraxManager().sendUpdatedSongList(); } } diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/TraxManager.java b/src/main/java/com/eu/habbo/habbohotel/rooms/TraxManager.java index 075ce283..04b13d82 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/TraxManager.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/TraxManager.java @@ -3,11 +3,15 @@ package com.eu.habbo.habbohotel.rooms; import com.eu.habbo.Emulator; import com.eu.habbo.core.Disposable; import com.eu.habbo.habbohotel.achievements.AchievementManager; +import com.eu.habbo.habbohotel.gameclients.GameClient; import com.eu.habbo.habbohotel.items.SoundTrack; import com.eu.habbo.habbohotel.items.interactions.InteractionJukeBox; import com.eu.habbo.habbohotel.items.interactions.InteractionMusicDisc; import com.eu.habbo.habbohotel.users.Habbo; import com.eu.habbo.habbohotel.users.HabboItem; +import com.eu.habbo.messages.outgoing.inventory.AddHabboItemComposer; +import com.eu.habbo.messages.outgoing.inventory.InventoryRefreshComposer; +import com.eu.habbo.messages.outgoing.inventory.RemoveHabboItemComposer; import com.eu.habbo.messages.outgoing.rooms.items.jukebox.JukeBoxMySongsComposer; import com.eu.habbo.messages.outgoing.rooms.items.jukebox.JukeBoxNowPlayingMessageComposer; import com.eu.habbo.messages.outgoing.rooms.items.jukebox.JukeBoxPlayListComposer; @@ -18,6 +22,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class TraxManager implements Disposable { private final Room room; @@ -38,8 +43,18 @@ public class TraxManager implements Disposable { statement.setInt(1, this.room.getId()); try (ResultSet set = statement.executeQuery()) { while (set.next()) { - HabboItem musicDisc = this.room.getHabboItem(set.getInt("item_id")); - if (musicDisc instanceof InteractionMusicDisc) { + HabboItem musicDisc = Emulator.getGameEnvironment().getItemManager().loadHabboItem(set.getInt("item_id")); + + if (!(musicDisc instanceof InteractionMusicDisc) || musicDisc.getRoomId() != -1) { + try (PreparedStatement stmt = connection.prepareStatement("DELETE FROM room_trax_playlist WHERE room_id = ? AND item_id = ? LIMIT 1")) { + stmt.setInt(1, this.room.getId()); + stmt.setInt(2, musicDisc.getId()); + stmt.execute(); + } catch (SQLException e) { + Emulator.getLogging().logSQLException(e); + return; + } + } else { SoundTrack track = Emulator.getGameEnvironment().getItemManager().getSoundTrack(((InteractionMusicDisc) musicDisc).getSongId()); if (track != null) { @@ -128,29 +143,32 @@ public class TraxManager implements Disposable { return null; } - public void addSong(int itemId) { - HabboItem musicDisc = this.room.getHabboItem(itemId); + public void addSong(InteractionMusicDisc musicDisc, Habbo habbo) { + SoundTrack track = Emulator.getGameEnvironment().getItemManager().getSoundTrack(musicDisc.getSongId()); - if (musicDisc instanceof InteractionMusicDisc) { - SoundTrack track = Emulator.getGameEnvironment().getItemManager().getSoundTrack(((InteractionMusicDisc) musicDisc).getSongId()); + if (track != null) { + this.totalLength += track.getLength(); + this.songs.add(musicDisc); - if (track != null) { - this.totalLength += track.getLength(); - this.songs.add((InteractionMusicDisc) musicDisc); - - try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO room_trax_playlist (room_id, item_id) VALUES (?, ?)")) { - statement.setInt(1, this.room.getId()); - statement.setInt(2, itemId); - statement.execute(); - } catch (SQLException e) { - Emulator.getLogging().logSQLException(e); - return; - } - - this.room.sendComposer(new JukeBoxPlayListComposer(this.songs, this.totalLength).compose()); - this.room.sendComposer(new JukeBoxMySongsComposer(this.myList()).compose()); + try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO room_trax_playlist (room_id, item_id) VALUES (?, ?)")) { + statement.setInt(1, this.room.getId()); + statement.setInt(2, musicDisc.getId()); + statement.execute(); + } catch (SQLException e) { + Emulator.getLogging().logSQLException(e); + return; } + + this.room.sendComposer(new JukeBoxPlayListComposer(this.songs, this.totalLength).compose()); + + musicDisc.setRoomId(-1); + musicDisc.needsUpdate(true); + + habbo.getInventory().getItemsComponent().removeHabboItem(musicDisc); + habbo.getClient().sendResponse(new RemoveHabboItemComposer(musicDisc.getGiftAdjustedId())); } + + this.sendUpdatedSongList(); } public void removeSong(int itemId) { @@ -174,8 +192,34 @@ public class TraxManager implements Disposable { } this.room.sendComposer(new JukeBoxPlayListComposer(this.songs, this.totalLength).compose()); + + musicDisc.setRoomId(0); + musicDisc.needsUpdate(true); + + Habbo owner = Emulator.getGameEnvironment().getHabboManager().getHabbo(musicDisc.getUserId()); + + if (owner != null) { + owner.getInventory().getItemsComponent().addItem(musicDisc); + + GameClient client = owner.getClient(); + if (client != null) { + client.sendResponse(new AddHabboItemComposer(musicDisc)); + client.sendResponse(new InventoryRefreshComposer()); + } + } } - this.room.sendComposer(new JukeBoxMySongsComposer(this.myList()).compose()); + + this.sendUpdatedSongList(); + } + + public void sendUpdatedSongList() { + this.room.getHabbos().forEach(h -> { + GameClient client = h.getClient(); + + if (client != null) { + client.sendResponse(new JukeBoxMySongsComposer(this.myList(h))); + } + }); } public int timePlaying() { @@ -208,16 +252,11 @@ public class TraxManager implements Disposable { return trax; } - public List myList() { - List trax = new ArrayList<>(); - - for (HabboItem item : this.room.getRoomSpecialTypes().getItemsOfType(InteractionMusicDisc.class)) { - if (!this.songs.contains(item)) { - trax.add((InteractionMusicDisc) item); - } - } - - return trax; + public List myList(Habbo habbo) { + return habbo.getInventory().getItemsComponent().getItems().valueCollection().stream() + .filter(i -> i instanceof InteractionMusicDisc && i.getRoomId() == 0) + .map(i -> (InteractionMusicDisc) i) + .collect(Collectors.toList()); } public InteractionMusicDisc fromItemId(int itemId) { diff --git a/src/main/java/com/eu/habbo/messages/incoming/rooms/items/jukebox/JukeBoxAddSoundTrackEvent.java b/src/main/java/com/eu/habbo/messages/incoming/rooms/items/jukebox/JukeBoxAddSoundTrackEvent.java index 273e62e4..8585c567 100644 --- a/src/main/java/com/eu/habbo/messages/incoming/rooms/items/jukebox/JukeBoxAddSoundTrackEvent.java +++ b/src/main/java/com/eu/habbo/messages/incoming/rooms/items/jukebox/JukeBoxAddSoundTrackEvent.java @@ -2,26 +2,25 @@ package com.eu.habbo.messages.incoming.rooms.items.jukebox; import com.eu.habbo.habbohotel.items.interactions.InteractionMusicDisc; import com.eu.habbo.habbohotel.rooms.Room; +import com.eu.habbo.habbohotel.users.Habbo; import com.eu.habbo.habbohotel.users.HabboItem; import com.eu.habbo.messages.incoming.MessageHandler; public class JukeBoxAddSoundTrackEvent extends MessageHandler { @Override public void handle() throws Exception { + if (!this.client.getHabbo().getHabboInfo().getCurrentRoom().hasRights(this.client.getHabbo())) return; + int itemId = this.packet.readInt(); int unknown = this.packet.readInt(); - Room room = this.client.getHabbo().getHabboInfo().getCurrentRoom(); + Habbo habbo = this.client.getHabbo(); - if (room != null) { - HabboItem item = room.getHabboItem(itemId); + if (habbo != null) { + HabboItem item = habbo.getInventory().getItemsComponent().getHabboItem(itemId); - if (item instanceof InteractionMusicDisc) { - - - if (this.client.getHabbo().getHabboInfo().getCurrentRoom().hasRights(this.client.getHabbo())) { - this.client.getHabbo().getHabboInfo().getCurrentRoom().getTraxManager().addSong(itemId); - } + if (item != null && item instanceof InteractionMusicDisc && item.getRoomId() == 0) { + this.client.getHabbo().getHabboInfo().getCurrentRoom().getTraxManager().addSong((InteractionMusicDisc) item, habbo); } } } diff --git a/src/main/java/com/eu/habbo/messages/incoming/rooms/items/jukebox/JukeBoxEventOne.java b/src/main/java/com/eu/habbo/messages/incoming/rooms/items/jukebox/JukeBoxEventOne.java index 3e4dd7d6..ad2929f7 100644 --- a/src/main/java/com/eu/habbo/messages/incoming/rooms/items/jukebox/JukeBoxEventOne.java +++ b/src/main/java/com/eu/habbo/messages/incoming/rooms/items/jukebox/JukeBoxEventOne.java @@ -10,7 +10,7 @@ public class JukeBoxEventOne extends MessageHandler { public void handle() throws Exception { TraxManager traxManager = this.client.getHabbo().getHabboInfo().getCurrentRoom().getTraxManager(); this.client.sendResponse(new JukeBoxPlayListComposer(traxManager.getSongs(), traxManager.totalLength())); - this.client.sendResponse(new JukeBoxMySongsComposer(traxManager.myList())); + this.client.sendResponse(new JukeBoxMySongsComposer(traxManager.myList(this.client.getHabbo()))); this.client.getHabbo().getHabboInfo().getCurrentRoom().getTraxManager().updateCurrentPlayingSong(this.client.getHabbo()); } } diff --git a/src/main/java/com/eu/habbo/messages/incoming/rooms/items/jukebox/JukeBoxEventTwo.java b/src/main/java/com/eu/habbo/messages/incoming/rooms/items/jukebox/JukeBoxEventTwo.java index 41a86a7d..15c53b0e 100644 --- a/src/main/java/com/eu/habbo/messages/incoming/rooms/items/jukebox/JukeBoxEventTwo.java +++ b/src/main/java/com/eu/habbo/messages/incoming/rooms/items/jukebox/JukeBoxEventTwo.java @@ -10,7 +10,7 @@ public class JukeBoxEventTwo extends MessageHandler { public void handle() throws Exception { TraxManager traxManager = this.client.getHabbo().getHabboInfo().getCurrentRoom().getTraxManager(); this.client.sendResponse(new JukeBoxPlayListComposer(traxManager.getSongs(), traxManager.totalLength())); - this.client.sendResponse(new JukeBoxMySongsComposer(traxManager.myList())); + this.client.sendResponse(new JukeBoxMySongsComposer(traxManager.myList(this.client.getHabbo()))); this.client.getHabbo().getHabboInfo().getCurrentRoom().getTraxManager().updateCurrentPlayingSong(this.client.getHabbo()); } } diff --git a/src/main/java/com/eu/habbo/messages/incoming/rooms/items/jukebox/JukeBoxRequestPlayListEvent.java b/src/main/java/com/eu/habbo/messages/incoming/rooms/items/jukebox/JukeBoxRequestPlayListEvent.java index 88bcd1c4..a5468d59 100644 --- a/src/main/java/com/eu/habbo/messages/incoming/rooms/items/jukebox/JukeBoxRequestPlayListEvent.java +++ b/src/main/java/com/eu/habbo/messages/incoming/rooms/items/jukebox/JukeBoxRequestPlayListEvent.java @@ -10,7 +10,7 @@ public class JukeBoxRequestPlayListEvent extends MessageHandler { public void handle() throws Exception { TraxManager traxManager = this.client.getHabbo().getHabboInfo().getCurrentRoom().getTraxManager(); this.client.sendResponse(new JukeBoxPlayListComposer(traxManager.getSongs(), traxManager.totalLength())); - this.client.sendResponse(new JukeBoxMySongsComposer(traxManager.myList())); + this.client.sendResponse(new JukeBoxMySongsComposer(traxManager.myList(this.client.getHabbo()))); this.client.getHabbo().getHabboInfo().getCurrentRoom().getTraxManager().updateCurrentPlayingSong(this.client.getHabbo()); } }