Skip to content

Commit

Permalink
UpDate (#7175)
Browse files Browse the repository at this point in the history
* init commit

* add test

* give up

* extend date

* remove deprecated method usage

* remove unnecessary methods

* hmm

* fix comparisons

* update hashcode

* Fix serialisation equality issue.

Authored-by: Moderocky <admin@moderocky.com>

---------

Co-authored-by: Moderocky <admin@moderocky.com>
  • Loading branch information
Efnilite and Moderocky authored Dec 31, 2024
1 parent 03a506f commit 70cbf82
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 83 deletions.
34 changes: 33 additions & 1 deletion src/main/java/ch/njol/skript/classes/data/SkriptClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import ch.njol.skript.util.visual.VisualEffects;
import ch.njol.yggdrasil.Fields;

import java.io.NotSerializableException;
import java.io.StreamCorruptedException;
import java.util.Iterator;
import java.util.Locale;
Expand Down Expand Up @@ -352,7 +353,38 @@ 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<>()));
.serializer(new Serializer<Date>() {

@Override
public Fields serialize(Date date) {
Fields fields = new Fields();
fields.putPrimitive("time", date.getTime());
return fields;
}


@Override
protected Date deserialize(Fields fields)
throws StreamCorruptedException {
long time = fields.getPrimitive("time", long.class);
return new Date(time);
}

@Override
public void deserialize(Date date, Fields fields) {
throw new IllegalStateException("Cannot deserialize to an existing date object.");
}

@Override
public boolean mustSyncDeserialization() {
return false;
}

@Override
protected boolean canBeInstantiated() {
return false;
}
}));

Classes.registerClass(new ClassInfo<>(Direction.class, "direction")
.user("directions?")
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ch/njol/skript/command/ScriptCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,11 @@ public void setRemainingMilliseconds(UUID uuid, Event event, long milliseconds)

public long getElapsedMilliseconds(UUID uuid, Event event) {
Date lastUsage = getLastUsage(uuid, event);
return lastUsage == null ? 0 : new Date().getTimestamp() - lastUsage.getTimestamp();
return lastUsage == null ? 0 : Date.now().getTime() - lastUsage.getTime();
}

public void setElapsedMilliSeconds(UUID uuid, Event event, long milliseconds) {
Date date = new Date();
Date date = Date.now();
date.subtract(new Timespan(milliseconds));
setLastUsage(uuid, event, date);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/conditions/CondDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public boolean check(final Event e) {
final long now = System.currentTimeMillis();
return date.check(e,
date -> delta.check(e,
timespan -> now - date.getTimestamp() >= timespan.getAs(Timespan.TimePeriod.MILLISECOND)
timespan -> now - date.getTime() >= timespan.getAs(Timespan.TimePeriod.MILLISECOND)
), isNegated());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected String[] get(Event e, Date[] source) {
return get(source, new Getter<String, Date>() {
@Override
public String get(Date date) {
return format.format(new java.util.Date(date.getTimestamp()));
return format.format(date);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ExprUnixTicks extends SimplePropertyExpression<Date, Number> {
@Override
@Nullable
public Number convert(Date f) {
return f.getTimestamp() / 1000.0;
return f.getTime() / 1000.0;
}

@Override
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/ch/njol/skript/registrations/Classes.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import ch.njol.skript.command.Commands;
import ch.njol.skript.entity.EntityData;
import ch.njol.skript.util.Date;
import ch.njol.skript.util.Utils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -749,9 +750,15 @@ private static byte[] getYggdrasilStart(final ClassInfo<?> c) throws NotSerializ
assert r[i] == start[i] : o + " (" + ci.getC().getName() + "); " + Arrays.toString(start) + ", " + Arrays.toString(r);
final byte[] r2 = new byte[r.length - start.length];
System.arraycopy(r, start.length, r2, 0, r2.length);

Object d;
assert equals(o, d = deserialize(ci, new ByteArrayInputStream(r2))) : o + " (" + o.getClass() + ") != " + d + " (" + (d == null ? null : d.getClass()) + "): " + Arrays.toString(r);

if (o instanceof Date date)
System.out.println(date.getTime());

Object d = deserialize(ci, new ByteArrayInputStream(r2));
if (d instanceof Date date)
System.out.println(date.getTime());

assert equals(o, d) : o + " (" + o.getClass() + ") != " + d + " (" + (d == null ? null : d.getClass()) + "): " + Arrays.toString(r);

return new SerializedVariable.Value(ci.getCodeName(), r2);
} catch (final IOException e) { // shouldn't happen
Expand Down
159 changes: 85 additions & 74 deletions src/main/java/ch/njol/skript/util/Date.java
Original file line number Diff line number Diff line change
@@ -1,124 +1,135 @@
package ch.njol.skript.util;

import java.util.TimeZone;

import org.jetbrains.annotations.Nullable;

import ch.njol.skript.SkriptConfig;
import ch.njol.yggdrasil.YggdrasilSerializable;
import org.jetbrains.annotations.Nullable;

import java.util.TimeZone;

public class Date extends java.util.Date implements YggdrasilSerializable {

/**
* @author Peter Güttinger
*/
public class Date implements Comparable<Date>, YggdrasilSerializable {

/**
* Timestamp. Should always be in computer time/UTC/GMT+0.
*/
private long timestamp;

public Date() {
this(System.currentTimeMillis());
}

public Date(final long timestamp) {
this.timestamp = timestamp;
}

public Date(final long timestamp, final TimeZone zone) {
final long offset = zone.getOffset(timestamp);
this.timestamp = timestamp - offset;
}

/**
* Get a new Date with the current time
*
* @return New date with the current time
*/
public static Date now() {
return new Date(System.currentTimeMillis());
return new Date();
}

public Timespan difference(final Date other) {
return new Timespan(Math.abs(timestamp - other.timestamp));

/**
* Converts a {@link java.util.Date} to a {@link Date}.
*
* @param date The {@link java.util.Date} to convert.
* @return The converted date.
*/
public static Date fromJavaDate(java.util.Date date) {
if (date instanceof Date ours)
return ours;
return new Date(date.getTime());
}

@Override
public int compareTo(final @Nullable Date other) {
final long d = other == null ? timestamp : timestamp - other.timestamp;
return d < 0 ? -1 : d > 0 ? 1 : 0;

/**
* Creates a new Date with the current time.
*/
public Date() {
super();
}

@Override
public String toString() {
return SkriptConfig.formatDate(timestamp);

/**
* Creates a new Date with the provided timestamp.
*
* @param timestamp The timestamp in milliseconds.
*/
public Date(long timestamp) {
super(timestamp);
}

/**
* Get the timestamp of this date
* Creates a new Date with the provided timestamp and timezone.
*
* @return The timestamp in milliseconds
* @param timestamp The timestamp in milliseconds.
* @param zone The timezone to use.
*/
public long getTimestamp() {
return timestamp;
public Date(long timestamp, TimeZone zone) {
super(timestamp - zone.getOffset(timestamp));
}

/**
* Add a {@link Timespan} to this date
*
* @param span Timespan to add
* @param other Timespan to add
*/
public void add(final Timespan span) {
timestamp += span.getAs(Timespan.TimePeriod.MILLISECOND);
public void add(Timespan other) {
setTime(getTime() + other.getAs(Timespan.TimePeriod.MILLISECOND));
}

/**
* Subtract a {@link Timespan} from this date
*
* @param span Timespan to subtract
* @param other Timespan to subtract
*/
public void subtract(Timespan other) {
setTime(getTime() - other.getAs(Timespan.TimePeriod.MILLISECOND));
}

/**
* Returns the difference between this date and another date as a {@link Timespan}.
*
* @param other The other date.
* @return The difference between the provided dates as a {@link Timespan}.
*/
public void subtract(final Timespan span) {
timestamp -= span.getAs(Timespan.TimePeriod.MILLISECOND);
public Timespan difference(Date other) {
return new Timespan(Math.abs(getTime() - other.getTime()));
}

/**
* Get a new instance of this Date with the added timespan
*
* @param span Timespan to add to this Date
* @param other Timespan to add to this Date
* @return New Date with the added timespan
*/
public Date plus(Timespan span) {
return new Date(timestamp + span.getAs(Timespan.TimePeriod.MILLISECOND));
public Date plus(Timespan other) {
return new Date(getTime() + other.getAs(Timespan.TimePeriod.MILLISECOND));
}

/**
* Get a new instance of this Date with the subtracted timespan
*
* @param span Timespan to subtract from this Date
* @param other Timespan to subtract from this Date
* @return New Date with the subtracted timespan
*/
public Date minus(Timespan span) {
return new Date(timestamp - span.getAs(Timespan.TimePeriod.MILLISECOND));
public Date minus(Timespan other) {
return new Date(getTime() - other.getAs(Timespan.TimePeriod.MILLISECOND));
}

/**
* @deprecated Use {@link #getTime()} instead.
*/
@Deprecated(forRemoval = true)
public long getTimestamp() {
return getTime();
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (timestamp ^ (timestamp >>> 32));
return result;
return 31 + Long.hashCode(getTime());
}

@Override
public boolean equals(final @Nullable Object obj) {
if (this == obj)
return true;
public boolean equals(@Nullable Object obj) {
if (obj == null)
return false;
if (!(obj instanceof Date))
if (!(obj instanceof java.util.Date other))
return false;
final Date other = (Date) obj;
return timestamp == other.timestamp;
if (this == obj)
return true;
return getTime() == other.getTime();
}


@Override
public String toString() {
return SkriptConfig.formatDate(getTime());
}

}
29 changes: 29 additions & 0 deletions src/test/java/org/skriptlang/skript/test/tests/utils/DateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.skriptlang.skript.test.tests.utils;

import ch.njol.skript.util.Date;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class DateTest {

@Test
public void testNow() {
assertEquals(System.currentTimeMillis(), Date.now().getTime());
}

@Test
public void testFromJavaDate() {
java.util.Date javaDate = new java.util.Date();
Date date = Date.fromJavaDate(javaDate);
assertEquals(javaDate.getTime(), date.getTime());
}

@Test
public void testEquals() {
Date date1 = new Date(1000);
java.util.Date date2 = new java.util.Date(1000);
assertEquals(date1, date2);
}

}

0 comments on commit 70cbf82

Please sign in to comment.