Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add book events and expressions #813

Merged
merged 1 commit into from
Aug 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.event.player.PlayerBucketFillEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerEditBookEvent;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
Expand Down Expand Up @@ -846,5 +847,15 @@ public World get(final PortalCreateEvent e) {
return e.getWorld();
}
}, 0);
//PlayerEditBookEvent
EventValues.registerEventValue(PlayerEditBookEvent.class, ItemStack.class, new Getter<ItemStack, PlayerEditBookEvent>() {
@Override
public ItemStack get(PlayerEditBookEvent e) {
ItemStack book = new ItemStack(e.getPlayer().getItemInHand().getType());
book.setItemMeta(e.getNewBookMeta());
return book;
}
}, 0);

}
}
55 changes: 55 additions & 0 deletions src/main/java/ch/njol/skript/events/EvtBookEdit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
*
* Copyright 2011-2017 Peter Güttinger and contributors
*/
package ch.njol.skript.events;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.SkriptParser;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerEditBookEvent;
import org.eclipse.jdt.annotation.Nullable;

public class EvtBookEdit extends SkriptEvent{
static {
Skript.registerEvent("Book Edit", EvtBookEdit.class, PlayerEditBookEvent.class, "book (edit|change|write)")
.description("Called when a player edits a book")
.examples("")
.since("");
}

@Override
public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseResult parseResult) {
return true;
}

@Override
public boolean check(Event e) {
if (!(e instanceof PlayerEditBookEvent)){
return false;
}
return !((PlayerEditBookEvent) e).isSigning();
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return "book edit";
}
}
55 changes: 55 additions & 0 deletions src/main/java/ch/njol/skript/events/EvtBookSign.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
*
* Copyright 2011-2017 Peter Güttinger and contributors
*/
package ch.njol.skript.events;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.SkriptParser;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerEditBookEvent;
import org.eclipse.jdt.annotation.Nullable;

public class EvtBookSign extends SkriptEvent{
static {
Skript.registerEvent("Book Sign", EvtBookSign.class, PlayerEditBookEvent.class, "book sign[ing]")
.description("Called when a player signs a book")
.examples("")
.since("");
}

@Override
public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseResult parseResult) {
return true;
}

@Override
public boolean check(Event e) {
if (!(e instanceof PlayerEditBookEvent)){
return false;
}
return ((PlayerEditBookEvent) e).isSigning();
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return "book sign";
}
}
91 changes: 91 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprBookAuthor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
*
* Copyright 2011-2017 Peter Güttinger and contributors
*/
package ch.njol.skript.expressions;

import ch.njol.skript.classes.Changer;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import ch.njol.skript.lang.Expression;
import org.bukkit.Material;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.eclipse.jdt.annotation.Nullable;

@Name("Book Author")
@Description("The author of a book")
@Examples({"on book sign:",
" message \"Book Title: %author of event-item%\""})
@Since("INSERT VERSION")
public class ExprBookAuthor extends SimplePropertyExpression<ItemStack,String> {
static {
register(ExprBookAuthor.class, String.class, "[book] (author|writer|publisher)", "itemstack");
}

@Override
protected String getPropertyName() {
return "author";
}

@Nullable
@Override
public String convert(ItemStack itemStack) {
if (itemStack.getType() != Material.BOOK_AND_QUILL && itemStack.getType() != Material.WRITTEN_BOOK){
return null;
}
return ((BookMeta) itemStack.getItemMeta()).getAuthor();
}

@Override
public Class<? extends String> getReturnType() {
return String.class;
}

@Nullable
@Override
public Class<?>[] acceptChange(Changer.ChangeMode mode) {
if (mode == Changer.ChangeMode.SET || mode == Changer.ChangeMode.RESET || mode == Changer.ChangeMode.DELETE){
return new Class<?>[]{String.class};
}
return null;
}

@Override
public void change(Event e, @Nullable Object[] delta, Changer.ChangeMode mode) {
ItemStack itemStack = getExpr().getSingle(e);
if (itemStack == null || (itemStack.getType() != Material.WRITTEN_BOOK && itemStack.getType() != Material.BOOK_AND_QUILL)){
return;
}
BookMeta bookMeta = (BookMeta) itemStack.getItemMeta();
switch (mode){
case SET:
bookMeta.setAuthor(delta == null ? "" : (String) delta[0]);
break;
case RESET:
case DELETE:
bookMeta.setAuthor("");
break;
}
itemStack.setItemMeta(bookMeta);
}
}
113 changes: 113 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprBookPages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
*
* Copyright 2011-2017 Peter Güttinger and contributors
*/
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.Converter;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.base.PropertyExpression;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import org.bukkit.Material;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.eclipse.jdt.annotation.Nullable;

import java.util.List;

@Name("Book Pages")
@Description("The pages of a book")
@Examples({"on book sign:",
" message \"Book Pages: %pages of event-item%\"",
" message \"Book Page 1: %page 1 of event-item%\""})
@Since("INSERT VERSION")
public class ExprBookPages extends SimpleExpression<String> {
static {
Skript.registerExpression(ExprBookPages.class, String.class, ExpressionType.PROPERTY, "[all] [the] [book] (pages|content) of %itemstack%", "%itemstack%'s [book] (pages|content)", "[book] page %number% of %itemstack%", "%itemstack%'s [book] page %number%");
}

@SuppressWarnings("null")
private Expression<ItemStack> book;
@Nullable
private Expression<Number> page;

@Nullable
@Override
protected String[] get(Event e) {
ItemStack itemStack = book.getSingle(e);
if (itemStack == null || (itemStack.getType() != Material.BOOK_AND_QUILL && itemStack.getType() != Material.WRITTEN_BOOK)){
return null;
}
List<String> pages = ((BookMeta) itemStack.getItemMeta()).getPages();
if (page != null){
Number pageNumber = page.getSingle(e);
if (pageNumber == null){
return null;
}
int page = pageNumber.intValue();
if ((page) > pages.size() || page < 1){
return null;
}
return new String[]{pages.get(page - 1)};
}else{
return pages.toArray(new String[pages.size()]);
}
}

@Override
public boolean isSingle() {
return page != null;
}

@Override
public Class<? extends String> getReturnType() {
return String.class;
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return "book pages of " + book.toString(e, debug);
}

@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
if (matchedPattern == 0 || matchedPattern == 1){
book = (Expression<ItemStack>) exprs[0];
}else{
if (matchedPattern == 2){
page =(Expression<Number>) exprs[0];
book = (Expression<ItemStack>) exprs[1];
}else{
book = (Expression<ItemStack>) exprs[0];
page = (Expression<Number>) exprs[1];
}
}
return true;
}
}
Loading