-
-
Notifications
You must be signed in to change notification settings - Fork 386
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
base: dev/feature
Are you sure you want to change the base?
Changes from 5 commits
ff991e3
051ef6c
1a13506
5898de2
3dd8f05
1f2b44d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||
package ch.njol.skript.expressions.base; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
import org.bukkit.event.Event; | ||||||||||||||||||||||||||||||||||||||
import org.eclipse.jdt.annotation.Nullable; | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||
* 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||
return null; | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||||||||||
@Nullable | ||||||||||||||||||||||||||||||||||||||
@SuppressWarnings({ "serial", "unchecked" }) | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||
if (!CollectionUtils.contains(getChangeModes(), mode)) | ||||||||||||||||||||||||||||||||||||||
return null; | ||||||||||||||||||||||||||||||||||||||
return CollectionUtils.array((Class<? extends C>) new TypeToken<C>(getClass()){}.getRawType()); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||||||||||
@SuppressWarnings("unchecked") | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
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.