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 changers to ExprBookPages #5025

Merged
merged 11 commits into from
Dec 28, 2022
162 changes: 115 additions & 47 deletions src/main/java/ch/njol/skript/expressions/ExprBookPages.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,94 +21,162 @@
import ch.njol.skript.Skript;
import ch.njol.skript.aliases.Aliases;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.classes.Changer.ChangeMode;
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.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 ch.njol.util.coll.CollectionUtils;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.eclipse.jdt.annotation.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Name("Book Pages")
@Description("The pages of a book.")
@Examples({"on book sign:",
"\tmessage \"Book Pages: %pages of event-item%\"",
"\tmessage \"Book Page 1: %page 1 of event-item%\""})
@Since("2.2-dev31")
public class ExprBookPages extends SimpleExpression<String> {

@Examples({
"on book sign:",
"\tmessage \"Book Pages: %pages of event-item%\"",
"\tmessage \"Book Page 1: %page 1 of event-item%\"",
"set page 1 of player's held item to \"Book writing\""
})
@Since("2.2-dev31, INSERT VERSION (changers)")
public class ExprBookPages extends PropertyExpression<ItemType, String> {

static {
Skript.registerExpression(ExprBookPages.class, String.class, ExpressionType.PROPERTY,
"[all] [the] [book] (pages|content) of %itemtypes%",
"%itemtypes%'s [book] (pages|content)",
"[book] page %number% of %itemtypes%",
"%itemtypes%'s [book] page %number%");
"[(all [[of] the]|the)] [book] (pages|content) of %itemtypes%",
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
"%itemtypes%'[s] [book] (pages|content)",
"[book] page %number% of %itemtypes%",
"%itemtypes%'[s] [book] page %number%");
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
}

private static final ItemType bookItem = Aliases.javaItemType("book with text");

@SuppressWarnings("null")
private Expression<ItemType> book;

@Nullable
private Expression<Number> page;

@SuppressWarnings("null")
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
@Nullable
@Override
protected String[] get(Event e) {
ItemStack itemStack = book.getSingle(e).getRandom();
if (itemStack == null || !bookItem.isOfType(itemStack))
return null;
List<String> pages = ((BookMeta) itemStack.getItemMeta()).getPages();
if (page != null){
Number pageNumber = page.getSingle(e);
if (pageNumber == null){
return null;
protected String[] get(Event event, ItemType[] source) {
List<String> pages = new ArrayList<>();
for (ItemType itemType : source) {
ItemMeta meta = itemType.getItemMeta();
if (!(meta instanceof BookMeta))
continue;
BookMeta bookMeta = (BookMeta) meta;
if (page == null) {
pages.addAll(bookMeta.getPages());
} else {
Number pageNumber = page.getSingle(event);
if (pageNumber == null)
continue;
int page = pageNumber.intValue();
if (page <= 0 || page > bookMeta.getPageCount())
continue;
pages.add(bookMeta.getPage(page));
}
int page = pageNumber.intValue();
if ((page) > pages.size() || page < 1){
}
return pages.toArray(new String[0]);
}

@Nullable
@Override
public Class<?>[] acceptChange(ChangeMode mode) {
switch (mode) {
case SET:
case RESET:
case DELETE:
return CollectionUtils.array(page == null ? String[].class : String.class);
case ADD:
return page == null ? CollectionUtils.array(String[].class) : null;
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
default:
return null;
}
return new String[]{pages.get(page - 1)};
}else{
return pages.toArray(new String[pages.size()]);
}
}

@Override
public boolean isSingle() {
return page != null;
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
ItemType[] itemTypes = getExpr().getArray(event);
int page = this.page == null ? -1 : this.page.getOptionalSingle(event).orElse(-1).intValue();
String[] newPages = delta == null ? null : new String[delta.length];
if (newPages != null)
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < delta.length; i++)
newPages[i] = delta[i] + "";
for (ItemType itemType : itemTypes) {
if (itemType == null)
continue;
ItemMeta meta = itemType.getItemMeta();
if (!(meta instanceof BookMeta))
continue;
BookMeta bookMeta = (BookMeta) meta;
List<String> pages = new ArrayList<>(bookMeta.getPages());
int pageCount = bookMeta.getPageCount();
switch (mode) {
case DELETE:
case RESET:
if (this.page == null) {
pages = Arrays.asList("");
} else {
if (page <= 0 || page > pageCount)
continue;
pages.remove(page - 1);
}
break;
case SET:
if (newPages.length == 0)
continue;
if (this.page == null) {
pages = Arrays.asList(newPages);
} else {
if (page <= 0)
continue;
while (pages.size() < page) {
pages.add("");
}
pages.set(page - 1, newPages[0]);
}
break;
case ADD:
pages.addAll(Arrays.asList(newPages));
break;
}
bookMeta.setPages(pages);
itemType.setItemMeta(bookMeta);
}
}

@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);
public String toString(@Nullable Event event, boolean debug) {
return "book pages of " + getExpr().toString(event, debug);
}

@SuppressWarnings({"unchecked", "null"})
@Override
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
if (matchedPattern == 0 || matchedPattern == 1){
book = (Expression<ItemType>) exprs[0];
}else{
if (matchedPattern == 2){
page =(Expression<Number>) exprs[0];
book = (Expression<ItemType>) exprs[1];
}else{
book = (Expression<ItemType>) exprs[0];
if (matchedPattern == 0 || matchedPattern == 1) {
setExpr((Expression<ItemType>) exprs[0]);
} else {
if (matchedPattern == 2) {
page = (Expression<Number>) exprs[0];
setExpr((Expression<ItemType>) exprs[1]);
} else {
setExpr((Expression<ItemType>) exprs[0]);
page = (Expression<Number>) exprs[1];
}
}
Expand Down