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 a changer expression utility class #6340

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
66 changes: 36 additions & 30 deletions src/main/java/ch/njol/skript/effects/EffChange.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.logging.Level;

import ch.njol.skript.expressions.ExprParse;
import ch.njol.skript.expressions.base.ChangeExpression;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionList;
Expand Down Expand Up @@ -50,34 +51,34 @@
import ch.njol.skript.util.Utils;
import ch.njol.util.Kleenean;

/**
* @author Peter Güttinger
*/
@Name("Change: Set/Add/Remove/Delete/Reset")
@Description("A very general effect that can change many <a href='./expressions'>expressions</a>. Many expressions can only be set and/or deleted, while some can have things added to or removed from them.")
@Examples({"# set:",
"Set the player's display name to \"&lt;red&gt;%name of player%\"",
"set the block above the victim to lava",
"# add:",
"add 2 to the player's health # preferably use '<a href='#EffHealth'>heal</a>' for this",
"add argument to {blacklist::*}",
"give a diamond pickaxe of efficiency 5 to the player",
"increase the data value of the clicked block by 1",
"# remove:",
"remove 2 pickaxes from the victim",
"subtract 2.5 from {points::%uuid of player%}",
"# remove all:",
"remove every iron tool from the player",
"remove all minecarts from {entitylist::*}",
"# delete:",
"delete the block below the player",
"clear drops",
"delete {variable}",
"# reset:",
"reset walk speed of player",
"reset chunk at the targeted block"})
@Examples({
"# set:",
"Set the player's display name to \"&lt;red&gt;%name of player%\"",
"set the block above the victim to lava",
"# add:",
"add 2 to the player's health # preferably use '<a href='#EffHealth'>heal</a>' for this",
"add argument to {blacklist::*}",
"give a diamond pickaxe of efficiency 5 to the player",
"increase the data value of the clicked block by 1",
"# remove:",
"remove 2 pickaxes from the victim",
"subtract 2.5 from {points::%uuid of player%}",
"# remove all:",
"remove every iron tool from the player",
"remove all minecarts from {entitylist::*}",
"# delete:",
"delete the block below the player",
"clear drops",
"delete {variable}",
"# reset:",
"reset walk speed of player",
"reset chunk at the targeted block"
})
@Since("1.0 (set, add, remove, delete), 2.0 (remove all)")
public class EffChange extends Effect {

private static Patterns<ChangeMode> patterns = new Patterns<>(new Object[][] {
{"(add|give) %objects% to %~objects%", ChangeMode.ADD},
{"increase %~objects% by %objects%", ChangeMode.ADD},
Expand Down Expand Up @@ -278,20 +279,25 @@ else if (mode == ChangeMode.SET)
}
return true;
}

@Override
protected void execute(Event e) {
Object[] delta = changer == null ? null : changer.getArray(e);
@SuppressWarnings("unchecked")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't suppress warnings on a method, suppress them per statement.

protected void execute(Event event) {
Object[] delta = changer == null ? null : changer.getArray(event);
delta = changer == null ? delta : changer.beforeChange(changed, delta);

if ((delta == null || delta.length == 0) && (mode != ChangeMode.DELETE && mode != ChangeMode.RESET)) {
if (mode == ChangeMode.SET && changed.acceptChange(ChangeMode.DELETE) != null)
changed.change(e, null, ChangeMode.DELETE);
changed.change(event, null, ChangeMode.DELETE);
return;
}
changed.change(e, delta, mode);
if (changed instanceof ChangeExpression) {
((ChangeExpression<?, Object>) changed).changing(event, delta, mode);
} else {
changed.change(event, delta, mode);
}
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
final Expression<?> changer = this.changer;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* 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
*/
Comment on lines +1 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* 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.base;

import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong nullable


import com.google.common.reflect.TypeToken;

import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.coll.CollectionUtils;

/**
* Utility class to avoid dealing with casting the delta.
*
* @param <T> The Expression return type.
* @param <C> The changer type.
*/
public abstract class ChangeExpression<T, C> extends SimpleExpression<T> {

/**
* Execute a changing of the allowed ChangeMode on this expression.
*
* @param event The event where the expression is being called in.
* @param values The values to set the expression with.
* @param mode The ChangeMode that is being used on this expression.
*/
public abstract void changing(Event event, @Nullable C[] values, ChangeMode mode);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public abstract void changing(Event event, @Nullable C[] values, ChangeMode mode);
public abstract void changing(Event event, C @Nullable [] values, ChangeMode mode);


/**
* Override this method to define the change modes for this change expression.
* The change type will be the <C> generic unless {@link #acceptChange(ChangeMode)} is overridden.
* In that case, this method does not need to be overridden.
*
* @return An array of change modes.
*/
@Nullable
public ChangeMode[] getChangeModes() {
Comment on lines +54 to +55
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@Nullable
public ChangeMode[] getChangeModes() {
public ChangeMode @Nullable [] getChangeModes() {

return null;
}

@Override
@Nullable
@SuppressWarnings({ "serial", "unchecked" })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suppress on statements

public Class<?>[] acceptChange(ChangeMode mode) {
Comment on lines +60 to +62
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@Nullable
@SuppressWarnings({ "serial", "unchecked" })
public Class<?>[] acceptChange(ChangeMode mode) {
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {

if (!CollectionUtils.contains(getChangeModes(), mode))
return null;
return CollectionUtils.array((Class<? extends C>) new TypeToken<C>(getClass()){}.getRawType());
}

@Override
@SuppressWarnings("unchecked")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suppress on statement

public final void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
changing(event, (@Nullable C[]) delta, mode);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not really safe. We can't guarantee that it will be called from EffChange, and as a result, it may not be typed correctly. We'll need to check and create a properly typed array if necessary.

}
Comment on lines +70 to +72
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public final void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
changing(event, (@Nullable C[]) delta, mode);
}
public final void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
changing(event, (C @Nullable []) delta, mode);
}


}
Loading