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

Update server icons #7101

Open
wants to merge 6 commits into
base: dev/feature
Choose a base branch
from
Open
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
106 changes: 44 additions & 62 deletions src/main/java/ch/njol/skript/effects/EffLoadServerIcon.java
Original file line number Diff line number Diff line change
@@ -1,94 +1,76 @@
/**
* 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.effects;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.util.AsyncEffect;
import ch.njol.util.Kleenean;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.util.CachedServerIcon;
import org.jetbrains.annotations.Nullable;

import ch.njol.skript.ScriptLoader;
import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.RequiredPlugins;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.util.AsyncEffect;
import ch.njol.util.Kleenean;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Name("Load Server Icon")
@Description({"Loads server icons from the given files. You can get the loaded icon using the",
"<a href='expressions.html#ExprLastLoadedServerIcon'>last loaded server icon</a> expression.",
"Please note that the image must be 64x64 and the file path starts from the server folder.",})
@Examples({"on load:",
" clear {server-icons::*}",
" loop 5 times:",
" load server icon from file \"icons/%loop-number%.png\"",
" add the last loaded server icon to {server-icons::*}",
"",
"on server list ping:",
" set the icon to a random server icon out of {server-icons::*}"})
@Description({
"Loads server icons from the given files. You can get the loaded icon using the",
"<a href='expressions.html#ExprLastLoadedServerIcon'>last loaded server icon</a> expression.",
"Please note that the image must be 64x64 and the file path starts from the server folder.",
})
@Examples({
"on load:",
"\tclear {server-icons::*}",
"\tloop 5 times:",
"\t\tload server icon from file \"icons/%loop-number%.png\"",
"\t\tadd the last loaded server icon to {server-icons::*}",
"",
"on server list ping:",
"\tset the icon to a random server icon out of {server-icons::*}"
})
@Since("2.3")
@RequiredPlugins("Paper 1.12.2 or newer")
public class EffLoadServerIcon extends AsyncEffect {

private static final boolean SUPPORTS_SERVER_LIST_PING_EVENT =
Skript.classExists("com.destroystokyo.paper.event.server.PaperServerListPingEvent");

static {
Skript.registerEffect(EffLoadServerIcon.class, "load [the] server icon (from|of) [the] [image] [file] %string%");
}

private static final boolean PAPER_EVENT_EXISTS = Skript.classExists("com.destroystokyo.paper.event.server.PaperServerListPingEvent");

@SuppressWarnings("null")
private Expression<String> path;
private Expression<String> file;

@Nullable
public static CachedServerIcon lastLoaded = null;
public static @Nullable CachedServerIcon lastLoaded = null;

@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] expressions, int matchedPattern,
Kleenean isDelayed, ParseResult parseResult) {
getParser().setHasDelayBefore(Kleenean.TRUE);
if (!PAPER_EVENT_EXISTS) {

if (!SUPPORTS_SERVER_LIST_PING_EVENT) {
Skript.error("The load server icon effect requires Paper 1.12.2 or newer");
return false;
}
path = (Expression<String>) exprs[0];

file = (Expression<String>) expressions[0];
return true;
}

@Override
protected void execute(Event e) {
String pathString = path.getSingle(e);
String pathString = file.getSingle(e);
if (pathString == null)
return;
Path p = Paths.get(pathString);
if (Files.isRegularFile(p)) {

Path path = Paths.get(pathString);
if (Files.isRegularFile(path)) {
try {
lastLoaded = Bukkit.loadServerIcon(p.toFile());
lastLoaded = Bukkit.loadServerIcon(path.toFile());
} catch (NullPointerException | IllegalArgumentException ignored) {
} catch (Exception ex) {
Skript.exception(ex);
Expand All @@ -97,8 +79,8 @@ protected void execute(Event e) {
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return "load server icon from file " + path.toString(e, debug);
public String toString(@Nullable Event event, boolean debug) {
return "load server icon from file " + file.toString(event, debug);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,66 +1,47 @@
/**
* 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 org.bukkit.event.Event;
import org.bukkit.util.CachedServerIcon;
import org.jetbrains.annotations.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.RequiredPlugins;
import ch.njol.skript.doc.Since;
import ch.njol.skript.doc.*;
import ch.njol.skript.effects.EffLoadServerIcon;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
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.util.CachedServerIcon;
import org.jetbrains.annotations.Nullable;

@Name("Last Loaded Server Icon")
@Description({"Returns the last loaded server icon with the <a href='effects.html#EffLoadServerIcon'>load server icon</a> effect."})
@Description("Returns the last loaded server icon with the <a href='effects.html#EffLoadServerIcon'>" +
"load server icon</a> effect.")
@Examples("set {server-icon} to the last loaded server icon")
@Since("2.3")
@RequiredPlugins("Paper 1.12.2 or newer")
public class ExprLastLoadedServerIcon extends SimpleExpression<CachedServerIcon> {

private static final boolean SUPPORTS_SERVER_LIST_PING_EVENT =
Skript.classExists("com.destroystokyo.paper.event.server.PaperServerListPingEvent");

static {
Skript.registerExpression(ExprLastLoadedServerIcon.class, CachedServerIcon.class, ExpressionType.SIMPLE, "[the] [last[ly]] loaded server icon");
Skript.registerExpression(ExprLastLoadedServerIcon.class, CachedServerIcon.class,
ExpressionType.SIMPLE, "[the] [last[ly]] loaded server icon");
}

private static final boolean PAPER_EVENT_EXISTS = Skript.classExists("com.destroystokyo.paper.event.server.PaperServerListPingEvent");

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
if (!PAPER_EVENT_EXISTS) {
public boolean init(Expression<?>[] expressions, int matchedPattern,
Kleenean isDelayed, ParseResult parseResult) {
if (!SUPPORTS_SERVER_LIST_PING_EVENT) {
Skript.error("The last loaded server icon expression requires Paper 1.12.2+");
return false;
}

return true;
}

@Override
@Nullable
public CachedServerIcon[] get(Event e) {
public CachedServerIcon @Nullable [] get(Event event) {
return CollectionUtils.array(EffLoadServerIcon.lastLoaded);
}

Expand All @@ -75,7 +56,7 @@ public Class<? extends CachedServerIcon> getReturnType() {
}

@Override
public String toString(@Nullable Event e, boolean debug) {
public String toString(@Nullable Event event, boolean debug) {
return "the last loaded server icon";
}

Expand Down
Loading
Loading