Skip to content

Commit

Permalink
Fix ExprBookAuthors performance Java 17 (#7023)
Browse files Browse the repository at this point in the history
* Fix ExprBookAuthors performance Java 17

* Update src/main/java/ch/njol/skript/expressions/ExprBookAuthor.java

Co-authored-by: Moderocky <admin@moderocky.com>

* Update ExprBookAuthor.java

* Update src/main/java/ch/njol/skript/expressions/ExprBookAuthor.java

Co-authored-by: Patrick Miller <apickledwalrus@gmail.com>

* Update src/main/java/ch/njol/skript/expressions/ExprBookAuthor.java

Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>

---------

Co-authored-by: Moderocky <admin@moderocky.com>
Co-authored-by: Patrick Miller <apickledwalrus@gmail.com>
Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>
  • Loading branch information
4 people authored Sep 1, 2024
1 parent c8cd975 commit cf64d58
Showing 1 changed file with 23 additions and 50 deletions.
73 changes: 23 additions & 50 deletions src/main/java/ch/njol/skript/expressions/ExprBookAuthor.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/**
* 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 Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.expressions;

import ch.njol.skript.aliases.ItemType;
Expand All @@ -29,63 +11,54 @@

import org.bukkit.event.Event;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

@Name("Book Author")
@Description("The author of a book.")
@Examples({"on book sign:",
"\tmessage \"Book Title: %author of event-item%\""})
@Examples({
"on book sign:",
"\tmessage \"Book Title: %author of event-item%\""
})
@Since("2.2-dev31")
public class ExprBookAuthor extends SimplePropertyExpression<ItemType, String> {

static {
register(ExprBookAuthor.class, String.class, "[book] (author|writer|publisher)", "itemtypes");
}

@Nullable
@Override
public String convert(ItemType item) {
ItemMeta meta = item.getItemMeta();

if (meta instanceof BookMeta)
return ((BookMeta) meta).getAuthor();

return null;
return item.getItemMeta() instanceof BookMeta bookMeta ? bookMeta.getAuthor() : null;
}

@Nullable

@Override
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.SET || mode == ChangeMode.RESET || mode == ChangeMode.DELETE)
return CollectionUtils.array(String.class);
return null;
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
return switch (mode) {
case SET, RESET, DELETE -> CollectionUtils.array(String.class);
default -> null;
};
}

@SuppressWarnings("null")

@Override
public void change(Event e, @Nullable Object[] delta, ChangeMode mode) {
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
String author = delta == null ? null : (String) delta[0];

for (ItemType item : getExpr().getArray(e)) {
ItemMeta meta = item.getItemMeta();

if (meta instanceof BookMeta) {
((BookMeta) meta).setAuthor(author);
item.setItemMeta(meta);
for (ItemType item : getExpr().getArray(event)) {
if (item.getItemMeta() instanceof BookMeta bookMeta) {
bookMeta.setAuthor(author);
item.setItemMeta(bookMeta);
}
}
}

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

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

}

}

0 comments on commit cf64d58

Please sign in to comment.