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

Arithmetic Rework #5279

Merged
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
765ccd0
arithmetic rework
UnderscoreTud Dec 13, 2022
b188e3f
Merge remote-tracking branch 'origin/master'
UnderscoreTud Dec 13, 2022
25ac826
Merge branch 'SkriptLang:master' into feature/arithmetic-rework
UnderscoreTud Dec 13, 2022
c82fe2c
check if skript is accepting registrations for default values and dif…
UnderscoreTud Dec 13, 2022
486cd83
Merge remote-tracking branch 'origin/feature/arithmetic-rework' into …
UnderscoreTud Dec 13, 2022
0ebbd1f
deprecate ExprVectorArithmetic (kept for backwards compatibility)
UnderscoreTud Dec 13, 2022
e4194b9
move operation registration to `Arithmetics` class
UnderscoreTud Dec 26, 2022
46d8401
Merge branch 'SkriptLang:master' into feature/arithmetic-rework
UnderscoreTud Dec 26, 2022
5e95538
fix error
UnderscoreTud Dec 26, 2022
6d5efc8
Merge remote-tracking branch 'origin/feature/arithmetic-rework' into …
UnderscoreTud Dec 26, 2022
b384ba8
fix errors
UnderscoreTud Dec 26, 2022
d8e8077
fixed double spaces
UnderscoreTud Dec 26, 2022
230a4e3
requested changes
UnderscoreTud Dec 27, 2022
8784960
Requested Changes
UnderscoreTud Dec 27, 2022
82030e2
re-add the simply() method back to ExprArithmetic
UnderscoreTud Jan 14, 2023
ea8fca8
Merge branch 'master' into feature/arithmetic-rework
UnderscoreTud Jan 21, 2023
2988aa2
fix errors and add @MarkedForRemoval annotations to old arithmetic cl…
UnderscoreTud Jan 21, 2023
83e7bb5
fix error
UnderscoreTud Jan 21, 2023
5778e5a
Merge branch 'master' into feature/arithmetic-rework
TheLimeGlass Feb 1, 2023
fe8e754
changes
UnderscoreTud Feb 15, 2023
96a9b5e
Merge branch 'master' into feature/arithmetic-rework
UnderscoreTud Feb 16, 2023
e2be82d
fix cast
UnderscoreTud Feb 27, 2023
1222c0d
Merge branch 'master' into feature/arithmetic-rework
UnderscoreTud Apr 13, 2023
4882f87
Replace `@MarkedForRemoval` annotations with `@ScheduledForRemoval`
UnderscoreTud Apr 13, 2023
a051cfd
Merge branch 'master' into feature/arithmetic-rework
UnderscoreTud Jun 20, 2023
dee800e
Make arithmetics work with converters
UnderscoreTud Jun 20, 2023
c58ec98
Merge branch 'master' into feature/arithmetic-rework
UnderscoreTud Aug 11, 2023
eee8973
Add Timespan - Number arithmetics
UnderscoreTud Aug 11, 2023
fbc2125
Merge remote-tracking branch 'origin/dev/feature' into feature/arithm…
UnderscoreTud Sep 21, 2023
6265671
Deprecate ExprVectorArithmetic
UnderscoreTud Sep 21, 2023
a9f54d5
Merge branch 'dev/feature' into feature/arithmetic-rework
UnderscoreTud Sep 22, 2023
fd544b9
Overall improvements to everything
UnderscoreTud Sep 23, 2023
2dfdd9a
More overall fixes and improvements to everything
UnderscoreTud Sep 23, 2023
f2a98bc
Forgot to return, oops!
UnderscoreTud Sep 23, 2023
fbfbe24
Merge branch 'dev/feature' into feature/arithmetic-rework
UnderscoreTud Sep 23, 2023
c9378bd
Merge branch 'dev/feature' into feature/arithmetic-rework
UnderscoreTud Oct 2, 2023
b6bcdde
Resolve conflicts
UnderscoreTud Oct 2, 2023
ac0f4ec
Improve OperationInfo#getConverted
UnderscoreTud Oct 2, 2023
1e9eac7
Improve operation lookup
UnderscoreTud Oct 3, 2023
774e474
Merge branch 'dev/feature' into feature/arithmetic-rework
Moderocky Oct 14, 2023
db6f21b
Merge branch 'dev/feature' into feature/arithmetic-rework
UnderscoreTud Oct 30, 2023
623cb8e
Required Changes
UnderscoreTud Nov 13, 2023
d2fdfd8
Requested Changes
UnderscoreTud Nov 30, 2023
39f532f
Make runtime lookup more lenient
UnderscoreTud Nov 30, 2023
63ee6a1
Make Timespan - Number division operation non-commutative
UnderscoreTud Dec 30, 2023
2862c01
Merge branch 'dev/feature' into feature/arithmetic-rework
UnderscoreTud Dec 30, 2023
b01d278
Fix merge conflicts
UnderscoreTud Dec 30, 2023
59e4628
Apply suggestions from code review
UnderscoreTud Dec 31, 2023
1aa8470
Requested Changes
UnderscoreTud Dec 31, 2023
2d58fb5
Add tests
UnderscoreTud Jan 1, 2024
33186b2
Merge branch 'dev/feature' into feature/arithmetic-rework
UnderscoreTud Jan 1, 2024
f0d490d
Resolve conflicts
UnderscoreTud Jan 1, 2024
85154cd
Fix condition in test
UnderscoreTud Jan 1, 2024
06e92c8
Apply suggestions from code review
APickledWalrus Jan 1, 2024
8e881db
Fix occasional test failures
APickledWalrus Jan 1, 2024
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
2 changes: 2 additions & 0 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import ch.njol.skript.classes.data.DefaultComparators;
import ch.njol.skript.classes.data.DefaultConverters;
import ch.njol.skript.classes.data.DefaultFunctions;
import ch.njol.skript.classes.data.DefaultOperators;
import ch.njol.skript.classes.data.JavaClasses;
import ch.njol.skript.classes.data.SkriptClasses;
import ch.njol.skript.command.Commands;
Expand Down Expand Up @@ -527,6 +528,7 @@ public void onEnable() {
new DefaultComparators();
new DefaultConverters();
new DefaultFunctions();
new DefaultOperators();

ChatMessages.registerListeners();

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/ch/njol/skript/classes/Arithmetic.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@
*/
package ch.njol.skript.classes;

import ch.njol.skript.util.MarkedForRemoval;

/**
* Represents arithmetic for certain two types. Multiplication, division and
* power of methods are optional and may throw UnsupportedOperationExceptions.
* @param <A> the type of the absolute value
* @param <R> the type of the relative value
*/
@Deprecated
@MarkedForRemoval
public interface Arithmetic<A, R> {

public R difference(A first, A second);
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/ch/njol/skript/classes/ClassInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import ch.njol.skript.lang.util.SimpleLiteral;
import ch.njol.skript.localization.Noun;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.skriptlang.skript.lang.arithmetic.Operator;
import org.skriptlang.skript.lang.arithmetic.Arithmetics;

/**
* @author Peter Güttinger
Expand Down Expand Up @@ -190,11 +192,19 @@ public ClassInfo<T> changer(final Changer<? super T> changer) {
this.changer = changer;
return this;
}


@Deprecated
@SuppressWarnings("unchecked")
public <R> ClassInfo<T> math(final Class<R> relativeType, final Arithmetic<? super T, R> math) {
assert this.math == null;
this.math = math;
mathRelativeType = relativeType;
Arithmetics.registerOperation(Operator.ADDITION, c, relativeType, (left, right) -> (T) math.add(left, right));
Arithmetics.registerOperation(Operator.SUBTRACTION, c, relativeType, (left, right) -> (T) math.subtract(left, right));
Arithmetics.registerOperation(Operator.MULTIPLICATION, c, relativeType, (left, right) -> (T) math.multiply(left, right));
Arithmetics.registerOperation(Operator.DIVISION, c, relativeType, (left, right) -> (T) math.divide(left, right));
Arithmetics.registerOperation(Operator.EXPONENTIATION, c, relativeType, (left, right) -> (T) math.power(left, right));
Arithmetics.registerDifference(c, relativeType, math::difference);
return this;
}

Expand Down Expand Up @@ -348,16 +358,19 @@ public Class<?> getSerializeAs() {
}

@Nullable
@Deprecated
public Arithmetic<? super T, ?> getMath() {
return math;
}

@Nullable
@Deprecated
public <R> Arithmetic<T, R> getRelativeMath() {
return (Arithmetic<T, R>) math;
}

@Nullable
@Deprecated
public Class<?> getMathRelativeType() {
return mathRelativeType;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/ch/njol/skript/classes/NumberArithmetic.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@
*/
package ch.njol.skript.classes;


import ch.njol.skript.util.MarkedForRemoval;

/**
* @author Peter Güttinger
*/
@Deprecated
@MarkedForRemoval
public class NumberArithmetic implements Arithmetic<Number, Number> {

@Override
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/ch/njol/skript/classes/VectorArithmethic.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
*/
package ch.njol.skript.classes;

import ch.njol.skript.util.MarkedForRemoval;
import org.bukkit.util.Vector;

/**
* @author bi0qaw
*/
@Deprecated
@MarkedForRemoval
public class VectorArithmethic implements Arithmetic<Vector, Vector> {

@Override
Expand Down
107 changes: 107 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/DefaultOperators.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* 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.classes.data;

import ch.njol.skript.Skript;
import ch.njol.skript.hooks.economy.classes.Money;
import ch.njol.skript.util.Date;
import ch.njol.skript.util.Timespan;
import ch.njol.skript.util.Utils;
import org.bukkit.util.Vector;
import org.skriptlang.skript.lang.arithmetic.Operator;
import org.skriptlang.skript.lang.arithmetic.Arithmetics;

public class DefaultOperators {

static {

Arithmetics.registerOperation(Operator.ADDITION, Number.class, (left, right) -> {
if (Utils.isInteger(left, right))
return left.longValue() + right.longValue();
return left.doubleValue() + right.doubleValue();
});
Arithmetics.registerOperation(Operator.SUBTRACTION, Number.class, (left, right) -> {
if (Utils.isInteger(left, right))
return left.longValue() - right.longValue();
return left.doubleValue() - right.doubleValue();
});
Arithmetics.registerOperation(Operator.MULTIPLICATION, Number.class, (left, right) -> {
if (Utils.isInteger(left, right))
return left.longValue() * right.longValue();
return left.doubleValue() * right.doubleValue();
});
Arithmetics.registerOperation(Operator.DIVISION, Number.class, (left, right) -> left.doubleValue() / right.doubleValue());
Arithmetics.registerOperation(Operator.EXPONENTIATION, Number.class, (left, right) -> {
if (Utils.isInteger(left, right))
return (long) Math.pow(left.longValue(), right.longValue());
return Math.pow(left.doubleValue(), right.doubleValue());
});
Arithmetics.registerDifference(Number.class, (left, right) -> {
if (Utils.isInteger(left, right))
return Math.abs(left.longValue() - right.longValue());
return Math.abs(left.doubleValue() - right.doubleValue());
});
Arithmetics.registerDefaultValue(Number.class, 0L);

Arithmetics.registerOperation(Operator.ADDITION, Vector.class, (left, right) -> left.clone().add(right));
Arithmetics.registerOperation(Operator.SUBTRACTION, Vector.class, (left, right) -> left.clone().subtract(right));
Arithmetics.registerOperation(Operator.MULTIPLICATION, Vector.class, (left, right) -> left.clone().multiply(right));
Arithmetics.registerOperation(Operator.MULTIPLICATION, Vector.class, Number.class, (left, right) -> left.clone().multiply(right.doubleValue()), (left, right) -> {
double number = left.doubleValue();
Vector leftVector = new Vector(number, number, number);
return leftVector.multiply(right);
});
Arithmetics.registerOperation(Operator.DIVISION, Vector.class, (left, right) -> left.clone().divide(right));
Arithmetics.registerOperation(Operator.DIVISION, Vector.class, Number.class, (left, right) -> {
double number = right.doubleValue();
Vector rightVector = new Vector(number, number, number);
return left.clone().divide(rightVector);
}, (left, right) -> {
double number = left.doubleValue();
Vector leftVector = new Vector(number, number, number);
return leftVector.divide(right);
});
Arithmetics.registerDifference(Vector.class,
(left, right) -> new Vector(Math.abs(left.getX() - right.getX()), Math.abs(left.getY() - right.getY()), Math.abs(left.getZ() - right.getZ())));
Arithmetics.registerDefaultValue(Vector.class, new Vector());

Arithmetics.registerOperation(Operator.ADDITION, Timespan.class, (left, right) -> new Timespan(left.getMilliSeconds() + right.getMilliSeconds()));
Arithmetics.registerOperation(Operator.SUBTRACTION, Timespan.class, (left, right) -> new Timespan(Math.max(0, left.getMilliSeconds() - right.getMilliSeconds())));
Arithmetics.registerDifference(Timespan.class, (left, right) -> new Timespan(Math.abs(left.getMilliSeconds() - right.getMilliSeconds())));
Arithmetics.registerDefaultValue(Timespan.class, new Timespan());

Arithmetics.registerOperation(Operator.ADDITION, Date.class, Timespan.class, Date::plus);
Arithmetics.registerOperation(Operator.SUBTRACTION, Date.class, Timespan.class, Date::minus);
Arithmetics.registerDifference(Date.class, Timespan.class, Date::difference);

Arithmetics.registerOperation(Operator.ADDITION, Money.class, (left, right) -> new Money(left.getAmount() + right.getAmount()));
Arithmetics.registerOperation(Operator.SUBTRACTION, Money.class, (left, right) -> new Money(left.getAmount() - right.getAmount()));
Arithmetics.registerOperation(Operator.MULTIPLICATION, Money.class, (left, right) -> new Money(left.getAmount() * right.getAmount()));
Arithmetics.registerOperation(Operator.DIVISION, Money.class, (left, right) -> new Money(left.getAmount() / right.getAmount()));
Arithmetics.registerDifference(Money.class, (left, right) -> {
double result = Math.abs(left.getAmount() - right.getAmount());
if (result < Skript.EPSILON)
return new Money(0);
return new Money(result);
});
Arithmetics.registerDefaultValue(Money.class, new Money(0));

}

}
15 changes: 7 additions & 8 deletions src/main/java/ch/njol/skript/classes/data/JavaClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import ch.njol.skript.SkriptConfig;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.NumberArithmetic;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.classes.Serializer;
import ch.njol.skript.lang.ParseContext;
Expand Down Expand Up @@ -125,7 +124,7 @@ public Number deserialize(String s) {
public boolean mustSyncDeserialization() {
return false;
}
}).math(Number.class, new NumberArithmetic()));
}));

Classes.registerClass(new ClassInfo<>(Long.class, "long")
.user("int(eger)?s?")
Expand Down Expand Up @@ -184,7 +183,7 @@ public Long deserialize(String s) {
public boolean mustSyncDeserialization() {
return false;
}
}).math(Number.class, new NumberArithmetic()));
}));

Classes.registerClass(new ClassInfo<>(Integer.class, "integer")
.name(ClassInfo.NO_DOC)
Expand Down Expand Up @@ -241,7 +240,7 @@ public Integer deserialize(String s) {
public boolean mustSyncDeserialization() {
return false;
}
}).math(Number.class, new NumberArithmetic()));
}));

Classes.registerClass(new ClassInfo<>(Double.class, "double")
.name(ClassInfo.NO_DOC)
Expand Down Expand Up @@ -303,7 +302,7 @@ public Double deserialize(String s) {
public boolean mustSyncDeserialization() {
return false;
}
}).math(Number.class, new NumberArithmetic()));
}));

Classes.registerClass(new ClassInfo<>(Float.class, "float")
.name(ClassInfo.NO_DOC)
Expand Down Expand Up @@ -364,7 +363,7 @@ public Float deserialize(String s) {
public boolean mustSyncDeserialization() {
return false;
}
}).math(Number.class, new NumberArithmetic()));
}));

Classes.registerClass(new ClassInfo<>(Boolean.class, "boolean")
.user("booleans?")
Expand Down Expand Up @@ -486,7 +485,7 @@ public Short deserialize(String s) {
public boolean mustSyncDeserialization() {
return false;
}
}).math(Number.class, new NumberArithmetic()));
}));

Classes.registerClass(new ClassInfo<>(Byte.class, "byte")
.name(ClassInfo.NO_DOC)
Expand Down Expand Up @@ -543,7 +542,7 @@ public Byte deserialize(String s) {
public boolean mustSyncDeserialization() {
return false;
}
}).math(Number.class, new NumberArithmetic()));
}));

Classes.registerClass(new ClassInfo<>(String.class, "string")
.user("(text|string)s?")
Expand Down
62 changes: 0 additions & 62 deletions src/main/java/ch/njol/skript/classes/data/SkriptClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.bukkitutil.EnchantmentUtils;
import ch.njol.skript.bukkitutil.ItemUtils;
import ch.njol.skript.classes.Arithmetic;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.EnumSerializer;
Expand Down Expand Up @@ -338,37 +337,6 @@ public Timespan deserialize(final String s) {
public boolean mustSyncDeserialization() {
return false;
}
})
.math(Timespan.class, new Arithmetic<Timespan, Timespan>() {
@Override
public Timespan difference(final Timespan t1, final Timespan t2) {
return new Timespan(Math.abs(t1.getMilliSeconds() - t2.getMilliSeconds()));
}

@Override
public Timespan add(final Timespan value, final Timespan difference) {
return new Timespan(value.getMilliSeconds() + difference.getMilliSeconds());
}

@Override
public Timespan subtract(final Timespan value, final Timespan difference) {
return new Timespan(Math.max(0, value.getMilliSeconds() - difference.getMilliSeconds()));
}

@Override
public Timespan multiply(Timespan value, Timespan multiplier) {
throw new UnsupportedOperationException();
}

@Override
public Timespan divide(Timespan value, Timespan divider) {
throw new UnsupportedOperationException();
}

@Override
public Timespan power(Timespan value, Timespan exponent) {
throw new UnsupportedOperationException();
}
}));

// TODO remove
Expand Down Expand Up @@ -456,36 +424,6 @@ public Date deserialize(final String s) {
return null;
}
}
}).math(Timespan.class, new Arithmetic<Date, Timespan>() {
@Override
public Timespan difference(final Date first, final Date second) {
return first.difference(second);
}

@Override
public Date add(final Date value, final Timespan difference) {
return new Date(value.getTimestamp() + difference.getMilliSeconds());
}

@Override
public Date subtract(final Date value, final Timespan difference) {
return new Date(value.getTimestamp() - difference.getMilliSeconds());
}

@Override
public Date multiply(Date value, Timespan multiplier) {
throw new UnsupportedOperationException();
}

@Override
public Date divide(Date value, Timespan divider) {
throw new UnsupportedOperationException();
}

@Override
public Date power(Date value, Timespan exponent) {
throw new UnsupportedOperationException();
}
}));

Classes.registerClass(new ClassInfo<>(Direction.class, "direction")
Expand Down
Loading