Skip to content

Commit 25c1fb3

Browse files
Idiot proofing
Mostly in GhostwriterEditBookScreen, trying to prevent out of bounds crashes. We're also now enforcing the title length restriction everywhere (found in SharedConstants)
1 parent 55bfa42 commit 25c1fb3

File tree

3 files changed

+61
-24
lines changed

3 files changed

+61
-24
lines changed

src/main/java/wafflestomper/ghostwriter/GhostLayer.java

+28-14
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,41 @@ public class GhostLayer {
4444

4545
// These are mostly used for loaded books
4646
// Note that EditBookScreen has its own bookTitle field that we need to keep track of
47-
public String bookTitle = "";
48-
public String bookAuthor = "";
47+
private String bookTitle = "";
48+
private String bookAuthor = "";
4949

5050
private boolean buttonsInitialized = false;
5151

5252

5353
public GhostLayer(IGhostBook parent, Screen screenParent, boolean bookIsEditable) {
5454
this.parent = parent;
55-
this.screen = screenParent; // TODO: Is there a cleaner way to do this?
55+
this.screen = screenParent;
5656
this.bookIsEditable = bookIsEditable;
5757
}
5858

5959

60+
public String getBookAuthor(){
61+
return this.bookAuthor;
62+
}
63+
64+
65+
public String getBookTitle(){
66+
return this.bookTitle;
67+
}
68+
69+
70+
/**
71+
* Set the internal bookTitle field, enforcing the maximum length from SharedConstants
72+
*/
73+
public void setBookTitle(String title){
74+
if (title.length() < SharedConstants.BOOK_TITLE_MAX_LEN) {
75+
this.bookTitle = title;
76+
} else {
77+
this.bookTitle = title.substring(0, SharedConstants.BOOK_TITLE_MAX_LEN);
78+
}
79+
}
80+
81+
6082
/**
6183
* Called by GhostwriterLecternScreen and GhostwriterReadBookScreen
6284
* Extracts title and author
@@ -65,7 +87,7 @@ public void extractTitleAuthor(ItemStack bookStack) {
6587
if (bookStack == null) return;
6688
CompoundNBT compoundnbt = bookStack.getTag();
6789
if (compoundnbt == null) return;
68-
this.bookTitle = compoundnbt.getString("title");
90+
this.setBookTitle(compoundnbt.getString("title"));
6991
this.bookAuthor = compoundnbt.getString("author");
7092
}
7193

@@ -192,7 +214,7 @@ private void addSignaturePages() {
192214
FileHandler fh = new FileHandler(clip);
193215
File sigFile = new File(Ghostwriter.FILE_HANDLER.getSignaturePath(), "default.ghb");
194216
if (fh.loadBook(sigFile) && clip.bookInClipboard) {
195-
this.parent.insertNewPage(this.parent.getBookPageCount(), ""); // TODO: Is this necessary?
217+
this.parent.insertNewPage(this.parent.getBookPageCount(), "");
196218
Ghostwriter.GLOBAL_CLIPBOARD.miscPages.addAll(clip.pages);
197219
pasteMultiplePages(this.parent.getBookPageCount() - 1);
198220
Ghostwriter.PRINTER.gamePrint(Printer.GRAY + "Signature pages added");
@@ -260,7 +282,7 @@ private void collapseTop() {
260282
* Copies a book from the clipboard into the 'real' book
261283
*/
262284
public void clipboardToBook(Clipboard fromBook) {
263-
this.bookTitle = fromBook.title;
285+
this.setBookTitle(fromBook.title);
264286
this.parent.setBookTitle(this.bookTitle);
265287
this.parent.replaceBookPages(fromBook.pages);
266288
if (this.parent.getBookPageCount() == 0) this.parent.insertNewPage(0, "");
@@ -364,14 +386,6 @@ public void updateButtons() {
364386
}
365387

366388
this.buttonDisableAutoReload.active = this.autoReloadFile != null;
367-
368-
// TODO: Is there a better place to do this?
369-
// Trim book title to a max of 32 characters. Anything longer an the book will be marked invalid by
370-
// the client when you try to read it
371-
// updateButtons() is called when the 'sign' button is clicked, so it's a convenient time to check this
372-
if (this.bookTitle.length() > 32) {
373-
this.bookTitle = this.bookTitle.substring(0, 32);
374-
}
375389
}
376390

377391

src/main/java/wafflestomper/ghostwriter/GhostwriterEditBookScreen.java

+31-7
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public GhostwriterEditBookScreen(PlayerEntity editingPlayer, ItemStack book, Han
3838
() -> this.bookTitle,
3939
(p_238772_1_) -> {
4040
this.bookTitle = p_238772_1_;
41-
this.ghostLayer.bookTitle = this.bookTitle;
41+
this.ghostLayer.setBookTitle(this.bookTitle);
4242
},
4343
this::func_238773_g_, // getClipboardText
4444
this::func_238760_a_, // setClipboardText
@@ -214,30 +214,50 @@ public boolean isBookBeingSigned() {
214214

215215
@Override // From IGhostBook
216216
public void insertNewPage(int atPageNum, String pageText) {
217-
// TODO: idiot proofing
217+
// Idiot proofing
218+
if (atPageNum >= SharedConstants.MAX_BOOK_PAGES){
219+
Ghostwriter.LOG.error("Cannot insert a page at index " + atPageNum + ". It would make the book too long");
220+
return;
221+
}
222+
223+
// Add blank pages if necessary to prevent IndexOutOfBoundsException
224+
while(this.bookPages.size() < atPageNum) this.bookPages.add("");
225+
218226
this.bookPages.add(atPageNum, pageText);
219227
this.bookChanged(true);
220228
}
221229

222230

223231
@Override // From IGhostBook
224232
public void setPageText(int pageNum, String pageText) {
225-
// TODO: idiot proofing
233+
// Idiot proofing
234+
if (pageNum < 0 || pageNum > this.bookPages.size() - 1){
235+
Ghostwriter.LOG.error("Couldn't set text on page " + pageNum + " because it doesn't exist");
236+
return;
237+
}
238+
226239
this.bookPages.set(pageNum, pageText);
227240
this.bookChanged(true);
228241
}
229242

230243

231244
@Override // From IGhostBook
232245
public String getPageText(int pageNum) {
233-
// TODO: idiot proofing
246+
// Idiot proofing
247+
if (pageNum < 0 || pageNum > this.bookPages.size() - 1) return "";
248+
234249
return this.bookPages.get(pageNum);
235250
}
236251

237252

238253
@Override // From IGhostBook
239254
public void removePage(int pageNum) {
240-
// TODO: idiot proofing
255+
// Prevent IndexOutOfBoundsException
256+
if (pageNum > this.bookPages.size() - 1){
257+
Ghostwriter.LOG.error("Can't remove page " + pageNum + ". It doesn't exist");
258+
return;
259+
}
260+
241261
this.bookPages.remove(pageNum);
242262
// Add a blank page if the book is empty
243263
if (this.bookPages.size() == 0) this.bookPages.add("");
@@ -247,7 +267,6 @@ public void removePage(int pageNum) {
247267

248268
@Override // From IGhostBook
249269
public void replaceBookPages(List<String> newPages) {
250-
// TODO: idiot proofing
251270
this.bookPages.clear();
252271
this.bookPages.addAll(newPages);
253272
if (this.bookPages.isEmpty()) this.bookPages.add("");
@@ -276,7 +295,12 @@ public int getCurrPage() {
276295

277296
@Override // From IGhostBook
278297
public void setCurrPage(int pageNum) {
279-
// TODO: idiot proofing
298+
// Idiot proofing
299+
if (pageNum < 0 || pageNum > this.bookPages.size() - 1){
300+
Ghostwriter.LOG.error("Couldn't move to page " + pageNum + ". It doesn't exist");
301+
pageNum = 0;
302+
}
303+
280304
this.currPage = pageNum;
281305
this.bookChanged(false);
282306
}

src/main/java/wafflestomper/ghostwriter/GhostwriterFileBrowserScreen.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ public void init() {
127127
new StringTextComponent("filename"));
128128
this.filenameField.setMaxStringLength(100);
129129
// Add default filename to filenameField
130-
String file_title = this.parent.bookTitle;
131-
String file_author = this.parent.bookAuthor;
130+
String file_title = this.parent.getBookTitle();
131+
String file_author = this.parent.getBookAuthor();
132132

133133
file_title = file_title.trim().replaceAll(" ", ".").replaceAll("[^a-zA-Z0-9.]", "");
134134
file_author = file_author.trim().replaceAll(" ", ".").replaceAll("[^a-zA-Z0-9.]", "");
@@ -203,7 +203,6 @@ public void navigateUp() {
203203
}
204204

205205

206-
// TODO: This could probably use some more checks
207206
private boolean isFilenameValid() {
208207
String fn = this.filenameField.getText();
209208
return !fn.equals("");

0 commit comments

Comments
 (0)