Skip to content

Commit

Permalink
Arithmetic Rework (#5279)
Browse files Browse the repository at this point in the history
  • Loading branch information
UnderscoreTud authored Jan 1, 2024
1 parent 98abf5d commit 59948b5
Show file tree
Hide file tree
Showing 27 changed files with 1,365 additions and 428 deletions.
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 @@ -26,6 +26,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.DefaultOperations;
import ch.njol.skript.classes.data.JavaClasses;
import ch.njol.skript.classes.data.SkriptClasses;
import ch.njol.skript.command.Commands;
Expand Down Expand Up @@ -535,6 +536,7 @@ public void onEnable() {
new DefaultComparators();
new DefaultConverters();
new DefaultFunctions();
new DefaultOperations();

ChatMessages.registerListeners();

Expand Down
1 change: 1 addition & 0 deletions src/main/java/ch/njol/skript/classes/Arithmetic.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* @param <A> the type of the absolute value
* @param <R> the type of the relative value
*/
@Deprecated
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 @@ -37,6 +37,8 @@
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.skriptlang.skript.lang.arithmetic.Operator;
import org.skriptlang.skript.lang.arithmetic.Arithmetics;

/**
* @author Peter Güttinger
Expand Down Expand Up @@ -222,11 +224,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 @@ -387,16 +397,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
1 change: 1 addition & 0 deletions src/main/java/ch/njol/skript/classes/NumberArithmetic.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
/**
* @author Peter Güttinger
*/
@Deprecated
public class NumberArithmetic implements Arithmetic<Number, Number> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
/**
* @author bi0qaw
*/
@Deprecated
public class VectorArithmethic implements Arithmetic<Vector, Vector> {

@Override
Expand Down
119 changes: 119 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/DefaultOperations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* 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.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.Arithmetics;
import org.skriptlang.skript.lang.arithmetic.Operator;

public class DefaultOperations {

static {
// Number - Number
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) && right.longValue() >= 0)
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);

// Vector - Vector
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.DIVISION, Vector.class, (left, right) -> left.clone().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, Vector::new);

// Vector - Number
// Number - Vector
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, 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);
});

// Timespan - Timespan
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, Timespan::new);

// Timespan - Number
// Number - Timespan
Arithmetics.registerOperation(Operator.MULTIPLICATION, Timespan.class, Number.class, (left, right) -> {
long scalar = right.longValue();
if (scalar < 0)
return null;
return new Timespan(left.getMilliSeconds() * scalar);
}, (left, right) -> {
long scalar = left.longValue();
if (scalar < 0)
return null;
return new Timespan(scalar * right.getMilliSeconds());
});
Arithmetics.registerOperation(Operator.DIVISION, Timespan.class, Number.class, (left, right) -> {
long scalar = right.longValue();
if (scalar <= 0)
return null;
return new Timespan(left.getMilliSeconds() / scalar);
});

// Date - 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);

}

}
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
67 changes: 2 additions & 65 deletions src/main/java/ch/njol/skript/classes/data/SkriptClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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 @@ -315,38 +314,7 @@ public String toString(final Timespan t, final int flags) {
public String toVariableNameString(final Timespan o) {
return "timespan:" + o.getMilliSeconds();
}
}).serializer(new YggdrasilSerializer<>())
.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();
}
}));
}).serializer(new YggdrasilSerializer<>()));

// TODO remove
Classes.registerClass(new ClassInfo<>(Timeperiod.class, "timeperiod")
Expand Down Expand Up @@ -408,38 +376,7 @@ public String toVariableNameString(final Timeperiod o) {
"subtract a day from {_yesterday}",
"# now {_yesterday} represents the date 24 hours before now")
.since("1.4")
.serializer(new YggdrasilSerializer<>())
.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();
}
}));
.serializer(new YggdrasilSerializer<>()));

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

0 comments on commit 59948b5

Please sign in to comment.