Create packet class for PacketCreator::showNotes

This commit is contained in:
P0nk
2022-12-27 12:10:33 +01:00
parent 2a460de911
commit 65111ae209
4 changed files with 57 additions and 32 deletions

View File

@@ -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<Note> 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());
}
}

View File

@@ -64,7 +64,7 @@ public final class NoteActionHandler extends AbstractPacketHandler {
int id = p.readInt(); int id = p.readInt();
p.readByte(); //Fame, but we read it from the database :) p.readByte(); //Fame, but we read it from the database :)
Optional<Note> discardedNote = noteService.discard(id); Optional<Note> discardedNote = noteService.delete(id);
if (discardedNote.isEmpty()) { if (discardedNote.isEmpty()) {
log.warn("Note with id {} not able to be discarded. Already discarded?", id); log.warn("Note with id {} not able to be discarded. Already discarded?", id);
continue; continue;

View File

@@ -4,12 +4,12 @@ import client.Character;
import database.DaoException; import database.DaoException;
import database.note.NoteDao; import database.note.NoteDao;
import model.Note; import model.Note;
import net.packet.Packet; import net.packet.out.ShowNotesPacket;
import net.server.Server; import net.server.Server;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import tools.PacketCreator;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@@ -62,32 +62,43 @@ public class NoteService {
throw new IllegalArgumentException("Unable to show notes - chr is null"); throw new IllegalArgumentException("Unable to show notes - chr is null");
} }
final List<Note> notes; List<Note> notes = getNotes(chr.getName());
try { if (notes.isEmpty()) {
notes = noteDao.findAllByTo(chr.getName());
} catch (DaoException e) {
log.error("Failed to find notes sent to chr name {}", chr.getName(), e);
return; return;
} }
Packet showNotesPacket = PacketCreator.showNotes(notes); chr.sendPacket(new ShowNotesPacket(notes));
chr.sendPacket(showNotesPacket); }
private List<Note> getNotes(String to) {
final List<Note> 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. * @return Discarded note. Empty optional if failed to discard.
*/ */
public Optional<Note> discard(int id) { public Optional<Note> delete(int noteId) {
try { try {
return noteDao.delete(id); return noteDao.delete(noteId);
} catch (DaoException e) { } 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(); return Optional.empty();
} }
} }
} }

View File

@@ -40,7 +40,6 @@ import constants.inventory.ItemConstants;
import constants.skills.Buccaneer; import constants.skills.Buccaneer;
import constants.skills.Corsair; import constants.skills.Corsair;
import constants.skills.ThunderBreaker; import constants.skills.ThunderBreaker;
import model.Note;
import net.encryption.InitializationVector; import net.encryption.InitializationVector;
import net.opcodes.SendOpcode; import net.opcodes.SendOpcode;
import net.packet.ByteBufOutPacket; import net.packet.ByteBufOutPacket;
@@ -72,7 +71,6 @@ import server.movement.LifeMovementFragment;
import java.awt.*; import java.awt.*;
import java.net.InetAddress; import java.net.InetAddress;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.*; import java.util.*;
@@ -5423,20 +5421,6 @@ public class PacketCreator {
return p; return p;
} }
public static Packet showNotes(List<Note> 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) { public static Packet useChalkboard(Character chr, boolean close) {
OutPacket p = OutPacket.create(SendOpcode.CHALKBOARD); OutPacket p = OutPacket.create(SendOpcode.CHALKBOARD);
p.writeInt(chr.getId()); p.writeInt(chr.getId());