diff --git a/src/main/java/net/packet/out/ShowNotesPacket.java b/src/main/java/net/packet/out/ShowNotesPacket.java new file mode 100644 index 0000000000..f2b05160ac --- /dev/null +++ b/src/main/java/net/packet/out/ShowNotesPacket.java @@ -0,0 +1,30 @@ +package net.packet.out; + +import model.Note; +import net.opcodes.SendOpcode; +import net.packet.ByteBufOutPacket; + +import java.util.List; +import java.util.Objects; + +import static tools.PacketCreator.getTime; + +public final class ShowNotesPacket extends ByteBufOutPacket { + + public ShowNotesPacket(List notes) { + super(SendOpcode.MEMO_RESULT); + Objects.requireNonNull(notes); + + writeByte(3); + writeByte(notes.size()); + notes.forEach(this::writeNote); + } + + private void writeNote(Note note) { + writeInt(note.id()); + writeString(note.from() + " "); //Stupid nexon forgot space lol + writeString(note.message()); + writeLong(getTime(note.timestamp())); + writeByte(note.fame()); + } +} diff --git a/src/main/java/net/server/channel/handlers/NoteActionHandler.java b/src/main/java/net/server/channel/handlers/NoteActionHandler.java index ed919b8c5b..2be3a48466 100644 --- a/src/main/java/net/server/channel/handlers/NoteActionHandler.java +++ b/src/main/java/net/server/channel/handlers/NoteActionHandler.java @@ -64,7 +64,7 @@ public final class NoteActionHandler extends AbstractPacketHandler { int id = p.readInt(); p.readByte(); //Fame, but we read it from the database :) - Optional discardedNote = noteService.discard(id); + Optional discardedNote = noteService.delete(id); if (discardedNote.isEmpty()) { log.warn("Note with id {} not able to be discarded. Already discarded?", id); continue; diff --git a/src/main/java/service/NoteService.java b/src/main/java/service/NoteService.java index c7ee334403..2221083e2f 100644 --- a/src/main/java/service/NoteService.java +++ b/src/main/java/service/NoteService.java @@ -4,12 +4,12 @@ import client.Character; import database.DaoException; import database.note.NoteDao; import model.Note; -import net.packet.Packet; +import net.packet.out.ShowNotesPacket; import net.server.Server; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import tools.PacketCreator; +import java.util.Collections; import java.util.List; import java.util.Optional; @@ -62,32 +62,43 @@ public class NoteService { throw new IllegalArgumentException("Unable to show notes - chr is null"); } - final List notes; - try { - notes = noteDao.findAllByTo(chr.getName()); - } catch (DaoException e) { - log.error("Failed to find notes sent to chr name {}", chr.getName(), e); + List notes = getNotes(chr.getName()); + if (notes.isEmpty()) { return; } - Packet showNotesPacket = PacketCreator.showNotes(notes); - chr.sendPacket(showNotesPacket); + chr.sendPacket(new ShowNotesPacket(notes)); + } + + private List getNotes(String to) { + final List notes; + try { + notes = noteDao.findAllByTo(to); + } catch (DaoException e) { + log.error("Failed to find notes sent to chr name {}", to, e); + return Collections.emptyList(); + } + + if (notes == null || notes.isEmpty()) { + return Collections.emptyList(); + } + + return notes; } /** - * Discard a read note + * Delete a read note * - * @param id Id of note to discard + * @param noteId Id of note to discard * @return Discarded note. Empty optional if failed to discard. */ - public Optional discard(int id) { + public Optional delete(int noteId) { try { - return noteDao.delete(id); + return noteDao.delete(noteId); } catch (DaoException e) { - log.error("Failed to discard note with id {}", id, e); + log.error("Failed to discard note with id {}", noteId, e); return Optional.empty(); } } - } diff --git a/src/main/java/tools/PacketCreator.java b/src/main/java/tools/PacketCreator.java index 2fb79103db..7fe6a0bb83 100644 --- a/src/main/java/tools/PacketCreator.java +++ b/src/main/java/tools/PacketCreator.java @@ -40,7 +40,6 @@ import constants.inventory.ItemConstants; import constants.skills.Buccaneer; import constants.skills.Corsair; import constants.skills.ThunderBreaker; -import model.Note; import net.encryption.InitializationVector; import net.opcodes.SendOpcode; import net.packet.ByteBufOutPacket; @@ -72,7 +71,6 @@ import server.movement.LifeMovementFragment; import java.awt.*; import java.net.InetAddress; -import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.*; @@ -5423,20 +5421,6 @@ public class PacketCreator { return p; } - public static Packet showNotes(List notes) { - final OutPacket p = OutPacket.create(SendOpcode.MEMO_RESULT); - p.writeByte(3); - p.writeByte(notes.size()); - notes.forEach(note -> { - p.writeInt(note.id()); - p.writeString(note.from() + " ");//Stupid nexon forgot space lol - p.writeString(note.message()); - p.writeLong(getTime(note.timestamp())); - p.writeByte(note.fame());//FAME :D - }); - return p; - } - public static Packet useChalkboard(Character chr, boolean close) { OutPacket p = OutPacket.create(SendOpcode.CHALKBOARD); p.writeInt(chr.getId());