Skip to content

Commit

Permalink
modernize player edits book event
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jun 2, 2022
1 parent f8f4bcf commit e12e9c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
Expand Up @@ -43,7 +43,6 @@ public boolean matches(ScriptPath path) {
if (!runInCheck(path, location)) {
return false;
}

if (!material.tryAdvancedMatcher(path.eventArgLowerAt(0))) {
return false;
}
Expand Down
Expand Up @@ -25,11 +25,14 @@ public class PlayerEditsBookScriptEvent extends BukkitScriptEvent implements Lis
// player edits book
// player signs book
//
// @Regex ^on player (edits|signs) book$
//
// @Group Player
//
// @Cancellable true
//
// @Location true
//
// @Triggers when a player edits or signs a book.
//
// @Context
// <context.title> returns the name of the book, if any.
// <context.pages> returns the number of pages in the book.
Expand All @@ -44,21 +47,25 @@ public class PlayerEditsBookScriptEvent extends BukkitScriptEvent implements Lis
//
// -->

PlayerEditsBookScriptEvent instance;
PlayerEditBookEvent event;
PlayerTag player;

@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith("player edits book") || path.eventLower.startsWith("player signs book");
public PlayerEditsBookScriptEvent() {
instance = this;
registerCouldMatcher("player edits book");
registerCouldMatcher("player signs book");
}

public static PlayerEditsBookScriptEvent instance;
public PlayerEditBookEvent event;
public PlayerTag player;

@Override
public boolean matches(ScriptPath path) {
String action = path.eventArgLowerAt(1);
if (!(action.equals("edits") && !event.isSigning()) && !(action.equals("signs") && event.isSigning())) {
return false;
}
if (!runInCheck(path, player.getLocation())) {
return false;
}
return super.matches(path);
}

Expand Down Expand Up @@ -99,16 +106,14 @@ public ScriptEntryData getScriptEntryData() {

@Override
public ObjectTag getContext(String name) {
if (name.equals("signing")) {
return new ElementTag(event.isSigning());
}
if (name.equals("title")) {
return event.isSigning() ? new ElementTag(event.getNewBookMeta().getTitle()) : null;
}
else if (name.equals("book")) {
ItemStack book = new ItemStack(Material.WRITABLE_BOOK);
book.setItemMeta(event.getNewBookMeta());
return new ItemTag(book);
switch (name) {
case "signing": return new ElementTag(event.isSigning());
case "title": return event.isSigning() ? new ElementTag(event.getNewBookMeta().getTitle()) : null;
case "pages": return new ElementTag(event.getNewBookMeta().getPageCount());
case "book":
ItemStack book = new ItemStack(Material.WRITABLE_BOOK);
book.setItemMeta(event.getNewBookMeta());
return new ItemTag(book);
}
return super.getContext(name);
}
Expand Down

0 comments on commit e12e9c9

Please sign in to comment.