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 ExprHoverList for 1.21+ #6988

Merged
merged 12 commits into from
Sep 1, 2024
127 changes: 74 additions & 53 deletions src/main/java/ch/njol/skript/expressions/ExprHoverList.java
Original file line number Diff line number Diff line change
@@ -1,72 +1,53 @@
/**
* 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 java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import com.destroystokyo.paper.event.server.PaperServerListPingEvent;
import com.destroystokyo.paper.profile.PlayerProfile;
import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Events;
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.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 com.destroystokyo.paper.event.server.PaperServerListPingEvent;
import com.destroystokyo.paper.profile.PlayerProfile;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Name("Hover List")
@Description({"The list when you hover on the player counts of the server in the server list.",
"This can be changed using texts or players in a <a href='events.html#server_list_ping'>server list ping</a> event only. " +
"Adding players to the list means adding the name of the players.",
"And note that, for example if there are 5 online players (includes <a href='#ExprOnlinePlayersCount'>fake online count</a>) " +
"in the server and the hover list is set to 3 values, Minecraft will show \"... and 2 more ...\" at end of the list."})
@Examples({"on server list ping:",
@Description({
"The list when you hover on the player counts of the server in the server list.",
"This can be changed using texts or players in a <a href='events.html#server_list_ping'>server list ping</a> event only. " +
"Adding players to the list means adding the name of the players.",
"And note that, for example if there are 5 online players (includes <a href='#ExprOnlinePlayersCount'>fake online count</a>) " +
"in the server and the hover list is set to 3 values, Minecraft will show \"... and 2 more ...\" at end of the list."
})
@Examples({
"on server list ping:",
"\tclear the hover list",
"\tadd \"&aWelcome to the &6Minecraft &aserver!\" to the hover list",
"\tadd \"\" to the hover list # A blank line",
"\tadd \"&cThere are &6%online players count% &conline players!\" to the hover list"})
"\tadd \"&cThere are &6%online players count% &conline players!\" to the hover list"
})
@Since("2.3")
@RequiredPlugins("Paper 1.12.2 or newer")
@Events("server list ping")
public class ExprHoverList extends SimpleExpression<String> {

static {
Skript.registerExpression(ExprHoverList.class, String.class, ExpressionType.SIMPLE,
"[the] [custom] [(player|server)] (hover|sample) ([message] list|message)",
"[the] [custom] player [(hover|sample)] list");
"[the] [custom] [(player|server)] (hover|sample) ([message] list|message)",
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
"[the] [custom] player [(hover|sample)] list");
}

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

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
Expand All @@ -82,13 +63,19 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye

@Override
@Nullable
public String[] get(Event e) {
if (!(e instanceof PaperServerListPingEvent))
public String[] get(Event event) {
if (!(event instanceof PaperServerListPingEvent))
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
return null;

return ((PaperServerListPingEvent) e).getPlayerSample().stream()
if (HAS_NEW_LISTED_PLAYER_INFO) {
return ((PaperServerListPingEvent) event).getListedPlayers().stream()
.map(PaperServerListPingEvent.ListedPlayerInfo::name)
.toArray(String[]::new);
} else {
return ((PaperServerListPingEvent) event).getPlayerSample().stream()
.map(PlayerProfile::getName)
.toArray(String[]::new);
}
}

@Override
Expand All @@ -111,24 +98,57 @@ public Class<?>[] acceptChange(ChangeMode mode) {

@SuppressWarnings("null")
@Override
public void change(Event e, @Nullable Object[] delta, ChangeMode mode) {
if (!(e instanceof PaperServerListPingEvent))
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
if (!(event instanceof PaperServerListPingEvent))
return;

if (HAS_NEW_LISTED_PLAYER_INFO) {
List<PaperServerListPingEvent.ListedPlayerInfo> values = new ArrayList<>();
if (mode != ChangeMode.DELETE && mode != ChangeMode.RESET) {
for (Object o : delta) {
if (o instanceof Player) {
Player player = (Player) o;
values.add(new PaperServerListPingEvent.ListedPlayerInfo(player.getName(), player.getUniqueId()));
} else {
values.add(new PaperServerListPingEvent.ListedPlayerInfo((String) o, UUID.randomUUID()));
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

List<PaperServerListPingEvent.ListedPlayerInfo> sample = ((PaperServerListPingEvent) event).getListedPlayers();
switch (mode) {
case SET:
sample.clear();
sample.addAll(values);
break;
case ADD:
sample.addAll(values);
break;
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
case REMOVE:
sample.removeAll(values);
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
break;
case DELETE:
case RESET:
sample.clear();
break;
}
return;
}

List<PlayerProfile> values = new ArrayList<>();
if (mode != ChangeMode.DELETE && mode != ChangeMode.RESET) {
for (Object o : delta) {
if (o instanceof Player) {
Player player = ((Player) o);
Player player = (Player) o;
values.add(Bukkit.createProfile(player.getUniqueId(), player.getName()));
} else {
values.add(Bukkit.createProfile(UUID.randomUUID(), (String) o));
}
}
}

List<PlayerProfile> sample = ((PaperServerListPingEvent) e).getPlayerSample();
switch (mode){
List<PlayerProfile> sample = ((PaperServerListPingEvent) event).getPlayerSample();
switch (mode) {
case SET:
sample.clear();
sample.addAll(values);
Expand All @@ -142,6 +162,7 @@ public void change(Event e, @Nullable Object[] delta, ChangeMode mode) {
case DELETE:
case RESET:
sample.clear();
break;
}
}

Expand All @@ -160,4 +181,4 @@ public String toString(@Nullable Event e, boolean debug) {
return "the hover list";
}

}
}