|
5 | 5 | import ch.njol.skript.localization.GeneralWords;
|
6 | 6 | import ch.njol.skript.localization.Language;
|
7 | 7 | import ch.njol.skript.localization.Noun;
|
| 8 | +import ch.njol.util.Math2; |
8 | 9 | import ch.njol.util.NonNullPair;
|
9 | 10 | import ch.njol.util.coll.CollectionUtils;
|
10 | 11 | import ch.njol.yggdrasil.YggdrasilSerializable;
|
@@ -254,6 +255,36 @@ public Duration getDuration() {
|
254 | 255 | return Duration.ofMillis(millis);
|
255 | 256 | }
|
256 | 257 |
|
| 258 | + /** |
| 259 | + * Safely adds the specified timespan to this timespan, handling potential overflow. |
| 260 | + * @param timespan The timespan to add to this timespan |
| 261 | + * @return a new Timespan object |
| 262 | + */ |
| 263 | + public Timespan add(Timespan timespan) { |
| 264 | + long millis = Math2.addClamped(this.millis, timespan.getAs(TimePeriod.MILLISECOND)); |
| 265 | + return new Timespan(millis); |
| 266 | + } |
| 267 | + |
| 268 | + /** |
| 269 | + * Safely subtracts the specified timespan from this timespan, handling potential underflow. |
| 270 | + * @param timespan The timespan to subtract from this timespan |
| 271 | + * @return a new Timespan object |
| 272 | + */ |
| 273 | + public Timespan subtract(Timespan timespan) { |
| 274 | + long millis = Math.max(0, this.millis - timespan.getAs(TimePeriod.MILLISECOND)); |
| 275 | + return new Timespan(millis); |
| 276 | + } |
| 277 | + |
| 278 | + /** |
| 279 | + * Calculates the difference between the specified timespan and this timespan. |
| 280 | + * @param timespan The timespan to get the difference of |
| 281 | + * @return a new Timespan object |
| 282 | + */ |
| 283 | + public Timespan difference(Timespan timespan) { |
| 284 | + long millis = Math.abs(this.millis - timespan.getAs(TimePeriod.MILLISECOND)); |
| 285 | + return new Timespan(millis); |
| 286 | + } |
| 287 | + |
257 | 288 | @Override
|
258 | 289 | public long get(TemporalUnit unit) {
|
259 | 290 | if (unit instanceof TimePeriod period)
|
|
0 commit comments