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 ExprSentCommands and EvtPlayerCommandSend #5948

Merged
merged 20 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
75 changes: 75 additions & 0 deletions src/main/java/ch/njol/skript/events/EvtPlayerCommandSend.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* 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.events;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerCommandSendEvent;
import org.eclipse.jdt.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collection;

public class EvtPlayerCommandSend extends SkriptEvent {

static {
Skript.registerEvent("Send Command List", EvtPlayerCommandSend.class, PlayerCommandSendEvent.class, "send[ing] [of] [server] command[s] list")
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
.description(
"Called when the server sends a list of commands to the player. This usually happens on join. The sent commands " +
"can be modified via the <a href='expressions.html#ExprSentCommands'>sent commands expression</a>.",
"Modifications will affect what commands show up for the player to tab complete. They will not affect what commands the player can actually run.",
"Adding new commands to the list is illegal behavior and will be ignored."
)
.examples(
"on send command list:",
"\tset command list to command list where [input does not contain \":\"]",
"\tremove \"help\" from command list"
)
.since("INSERT VERSION");
}

private final Collection<String> originalCommands = new ArrayList<>();

@Override
public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResult) {
return true;
}

@Override
public boolean check(Event event) {
originalCommands.clear();
originalCommands.addAll(((PlayerCommandSendEvent) event).getCommands());
return true;
}

public ImmutableCollection<String> getOriginalCommands() {
return ImmutableList.copyOf(originalCommands);
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "send server command list";
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}

sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}
160 changes: 160 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprSentCommands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/**
* 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.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.Since;
import ch.njol.skript.events.EvtPlayerCommandSend;
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 org.bukkit.event.Event;
import org.bukkit.event.player.PlayerCommandSendEvent;
import org.eclipse.jdt.annotation.Nullable;
import org.skriptlang.skript.lang.structure.Structure;

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

@Name("Sent Command List")
@Description({
"The commands that will be sent to the player in a <a href='events.html#send_command_list'>send commands to player event</a>.",
"Modifications will affect what commands show up for the player to tab complete. They will not affect what commands the player can actually run.",
"Adding new commands to the list is illegal behavior and will be ignored."
})
@Examples({
"on send command list:",
"\tset command list to command list where [input does not contain \":\"]",
"\tremove \"help\" from command list"
})
@Since("INSERT VERSION")
@Events("send command list")
public class ExprSentCommands extends SimpleExpression<String> {

static {
Skript.registerExpression(ExprSentCommands.class, String.class, ExpressionType.SIMPLE, "[the] [sent] [server] command[s] list");
}

private EvtPlayerCommandSend parent;
private Kleenean delay;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
if (!getParser().isCurrentEvent(PlayerCommandSendEvent.class))
return false;
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
Structure structure = getParser().getCurrentStructure();
if (!(structure instanceof EvtPlayerCommandSend))
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
return false;
delay = isDelayed;
parent = (EvtPlayerCommandSend) structure;
return true;
}

@Override
@Nullable
protected String[] get(Event event) {
if (delay != Kleenean.FALSE) {
Skript.error("Can't change the command list after the event has already passed");
return null;
}
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
if (!(event instanceof PlayerCommandSendEvent)) {
Skript.error("The 'command list' expression can only be used in a 'send command list' event");
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
return ((PlayerCommandSendEvent) event).getCommands().toArray(new String[0]);
}

@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
switch (mode) {
case REMOVE:
case REMOVE_ALL:
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
case DELETE:
case SET:
case RESET:
return new Class[]{String[].class};
case ADD:
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
default:
return null;
}
}

@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
if (!(event instanceof PlayerCommandSendEvent))
return;

Collection<String> commands = ((PlayerCommandSendEvent) event).getCommands();

// short circuit if we're just clearing the list
if (mode == ChangeMode.DELETE || mode == ChangeMode.REMOVE_ALL) {
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
commands.clear();
return;
}

List<String> deltaCommands = (delta != null && delta.length > 0) ? new ArrayList<>(Arrays.asList((String[]) delta)) : new ArrayList<>();
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
switch (mode) {
case REMOVE:
commands.removeAll(deltaCommands);
break;
case SET:
// remove all completely new commands, as adding new commands to the commands collection is illegal behaviour
List<String> newCommands = new ArrayList<>(deltaCommands);
newCommands.removeAll(parent.getOriginalCommands());
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
deltaCommands.removeAll(newCommands);
commands.clear();
commands.addAll(deltaCommands);
break;
case RESET:
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
commands.clear();
commands.addAll(parent.getOriginalCommands());
break;
case ADD:
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
default:
assert false;
break;
}
}

@Override
public boolean isSingle() {
return false;
}

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

@Override
public String toString(@Nullable Event event, boolean debug) {
return "the sent server command list";
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}

}