|
| 1 | +/** |
| 2 | + * This file is part of Skript. |
| 3 | + * |
| 4 | + * Skript is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * Skript is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with Skript. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + * Copyright Peter Güttinger, SkriptLang team and contributors |
| 18 | + */ |
| 19 | +package ch.njol.skript.expressions; |
| 20 | + |
| 21 | +import ch.njol.skript.Skript; |
| 22 | +import ch.njol.skript.classes.Changer.ChangeMode; |
| 23 | +import ch.njol.skript.doc.Description; |
| 24 | +import ch.njol.skript.doc.Examples; |
| 25 | +import ch.njol.skript.doc.Name; |
| 26 | +import ch.njol.skript.doc.Since; |
| 27 | +import ch.njol.skript.lang.Expression; |
| 28 | +import ch.njol.skript.lang.ExpressionType; |
| 29 | +import ch.njol.skript.lang.SkriptParser; |
| 30 | +import ch.njol.skript.lang.util.SimpleExpression; |
| 31 | +import ch.njol.util.Kleenean; |
| 32 | +import ch.njol.util.coll.CollectionUtils; |
| 33 | +import com.destroystokyo.paper.event.server.PaperServerListPingEvent; |
| 34 | +import org.bukkit.Bukkit; |
| 35 | +import org.bukkit.event.Event; |
| 36 | +import org.bukkit.event.server.ServerListPingEvent; |
| 37 | +import org.eclipse.jdt.annotation.Nullable; |
| 38 | + |
| 39 | +@Name("Get Player/Entity") |
| 40 | +@Description({"The count of max players. This can be changed in a <a href='events.html#server_list_ping'>server list ping</a> event only.", |
| 41 | + "'real max players' returns the real count of max players of the server always and can't be changed."}) |
| 42 | +@Examples({"set {_p} to player \"Notch\"", |
| 43 | + "set {_p} to player from uuid\"Notch\""}) |
| 44 | +@Since("INSERT VERSION") |
| 45 | +public class ExprGetPlayer extends SimpleExpression<Number> { |
| 46 | + |
| 47 | + static { |
| 48 | + Skript.registerExpression(ExprGetPlayer.class, Number.class, ExpressionType.PROPERTY, |
| 49 | + "[(1¦offline)]player ((with|from) [the] (uuid|name)|named) %string%", |
| 50 | + "entity (with|from) [the] uuid %string%"); |
| 51 | + } |
| 52 | + |
| 53 | + private static final boolean PAPER_EVENT_EXISTS = Skript.classExists("com.destroystokyo.paper.event.server.PaperServerListPingEvent"); |
| 54 | + |
| 55 | + private boolean isReal; |
| 56 | + |
| 57 | + @Override |
| 58 | + public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { |
| 59 | + boolean isServerPingEvent = getParser().isCurrentEvent(ServerListPingEvent.class) || |
| 60 | + (PAPER_EVENT_EXISTS && getParser().isCurrentEvent(PaperServerListPingEvent.class)); |
| 61 | + if (parseResult.mark == 2 && !isServerPingEvent) { |
| 62 | + Skript.error("The 'shown' max players count expression can't be used outside of a server list ping event"); |
| 63 | + return false; |
| 64 | + } |
| 65 | + isReal = (parseResult.mark == 0 && !isServerPingEvent) || parseResult.mark == 1; |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + @Nullable |
| 71 | + public Number[] get(Event e) { |
| 72 | + if (isReal) |
| 73 | + return CollectionUtils.array(Bukkit.getMaxPlayers()); |
| 74 | + else |
| 75 | + return CollectionUtils.array(((ServerListPingEvent) e).getMaxPlayers()); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + @Nullable |
| 80 | + public Class<?>[] acceptChange(ChangeMode mode) { |
| 81 | + if (!isReal) { |
| 82 | + if (getParser().getHasDelayBefore().isTrue()) { |
| 83 | + Skript.error("Can't change the fake max players count anymore after the server list ping event has already passed"); |
| 84 | + return null; |
| 85 | + } |
| 86 | + switch (mode) { |
| 87 | + case SET: |
| 88 | + case ADD: |
| 89 | + case REMOVE: |
| 90 | + case DELETE: |
| 91 | + case RESET: |
| 92 | + return CollectionUtils.array(Number.class); |
| 93 | + } |
| 94 | + } |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + @SuppressWarnings("null") |
| 99 | + @Override |
| 100 | + public void change(Event e, @Nullable Object[] delta, ChangeMode mode) { |
| 101 | + ServerListPingEvent event = (ServerListPingEvent) e; |
| 102 | + switch (mode) { |
| 103 | + case SET: |
| 104 | + event.setMaxPlayers(((Number) delta[0]).intValue()); |
| 105 | + break; |
| 106 | + case ADD: |
| 107 | + event.setMaxPlayers(event.getMaxPlayers() + ((Number) delta[0]).intValue()); |
| 108 | + break; |
| 109 | + case REMOVE: |
| 110 | + event.setMaxPlayers(event.getMaxPlayers() - ((Number) delta[0]).intValue()); |
| 111 | + break; |
| 112 | + case DELETE: |
| 113 | + case RESET: |
| 114 | + event.setMaxPlayers(Bukkit.getMaxPlayers()); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public boolean isSingle() { |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public Class<? extends Number> getReturnType() { |
| 125 | + return Number.class; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public String toString(@Nullable Event e, boolean debug) { |
| 130 | + return "the count of " + (isReal ? "real max players" : "max players"); |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments