Skip to content

Commit 1585513

Browse files
committed
Add Position, Scheduler.
1 parent 47186bc commit 1585513

File tree

10 files changed

+161
-71
lines changed

10 files changed

+161
-71
lines changed

commons-adventure/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ plugins {
77

88

99
dependencies {
10+
api(project(":commons-shared"))
1011
api("net.kyori:adventure-api:4.15.0")
1112
api("net.kyori:adventure-text-minimessage:4.15.0")
1213
api("net.kyori:adventure-text-serializer-legacy:4.15.0")

commons-shared-bukkit/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ plugins {
77

88

99
dependencies {
10+
api(project(":commons-shared"))
1011
api("org.spigotmc:spigot-api:1.19.4-R0.1-SNAPSHOT")
1112
api("net.kyori:adventure-api:4.15.0")
1213
}

commons-shared-bukkit/src/main/java/com/eternalcode/commons/shared/bukkit/position/Position.java

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,19 @@
11
package com.eternalcode.commons.shared.bukkit.position;
22

3-
import java.util.Objects;
43
import java.util.regex.Matcher;
54
import java.util.regex.Pattern;
65

76
/**
87
* Disclaimer - Bukkit {@link org.bukkit.Location} storage may cause a memory leak, because it is a wrapper for
98
* coordinates and {@link org.bukkit.World} reference. If you need to store location use {@link Position} and
109
* {@link PositionAdapter}.
11-
* */
12-
public final class Position {
10+
*/
11+
public record Position(double x, double y, double z, float yaw, float pitch, String world) {
1312

1413
public static final String NONE_WORLD = "__NONE__";
1514

16-
private static final Pattern PARSE_FORMAT = Pattern.compile("Position\\{x=(?<x>-?[\\d.]+), y=(?<y>-?[\\d.]+), z=(?<z>-?[\\d.]+), yaw=(?<yaw>-?[\\d.]+), pitch=(?<pitch>-?[\\d.]+), world='(?<world>.+)'}");
17-
18-
private final double x;
19-
private final double y;
20-
private final double z;
21-
private final float yaw;
22-
private final float pitch;
23-
private final String world;
24-
25-
26-
public Position(double x, double y, double z, float yaw, float pitch, String world) {
27-
this.x = x;
28-
this.y = y;
29-
this.z = z;
30-
this.yaw = yaw;
31-
this.pitch = pitch;
32-
this.world = world;
33-
}
34-
35-
public String getWorld() {
36-
return this.world;
37-
}
38-
39-
public double getX() {
40-
return this.x;
41-
}
42-
43-
public double getY() {
44-
return this.y;
45-
}
46-
47-
public double getZ() {
48-
return this.z;
49-
}
50-
51-
public float getYaw() {
52-
return this.yaw;
53-
}
54-
55-
public float getPitch() {
56-
return this.pitch;
57-
}
15+
private static final Pattern PARSE_FORMAT = Pattern.compile(
16+
"Position\\{x=(?<x>-?[\\d.]+), y=(?<y>-?[\\d.]+), z=(?<z>-?[\\d.]+), yaw=(?<yaw>-?[\\d.]+), pitch=(?<pitch>-?[\\d.]+), world='(?<world>.+)'}");
5817

5918
public boolean isNoneWorld() {
6019
return this.world.equals(NONE_WORLD);
@@ -80,11 +39,6 @@ public boolean equals(Object o) {
8039
&& this.world.equals(position.world);
8140
}
8241

83-
@Override
84-
public int hashCode() {
85-
return Objects.hash(this.x, this.y, this.z, this.yaw, this.pitch, this.world);
86-
}
87-
8842
@Override
8943
public String toString() {
9044
return "Position{" +

commons-shared-bukkit/src/main/java/com/eternalcode/commons/shared/bukkit/position/PositionAdapter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ public static Position convert(Location location) {
2323
}
2424

2525
public static Location convert(Position position) {
26-
World world = Bukkit.getWorld(position.getWorld());
26+
World world = Bukkit.getWorld(position.world());
2727

2828
if (world == null) {
2929
throw new IllegalStateException("World is not defined");
3030
}
3131

3232
return new Location(
3333
world,
34-
position.getX(),
35-
position.getY(),
36-
position.getZ(),
37-
position.getYaw(),
38-
position.getPitch());
34+
position.x(),
35+
position.y(),
36+
position.z(),
37+
position.yaw(),
38+
position.pitch());
3939
}
4040
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.eternalcode.commons.shared.bukkit.scheduler;
2+
3+
import com.eternalcode.commons.shared.scheduler.Scheduler;
4+
import com.eternalcode.commons.shared.scheduler.Task;
5+
import java.util.concurrent.CompletableFuture;
6+
import org.bukkit.plugin.Plugin;
7+
import org.bukkit.scheduler.BukkitScheduler;
8+
9+
import java.time.Duration;
10+
import java.util.function.Supplier;
11+
12+
public class BukkitSchedulerImpl implements Scheduler {
13+
14+
private final Plugin plugin;
15+
private final BukkitScheduler rootScheduler;
16+
17+
public BukkitSchedulerImpl(Plugin plugin) {
18+
this.plugin = plugin;
19+
this.rootScheduler = plugin.getServer().getScheduler();
20+
}
21+
22+
@Override
23+
public Task sync(Runnable task) {
24+
return new BukkitTaskImpl(this.rootScheduler.runTask(this.plugin, task));
25+
}
26+
27+
@Override
28+
public Task async(Runnable task) {
29+
return new BukkitTaskImpl(this.rootScheduler.runTaskAsynchronously(this.plugin, task));
30+
}
31+
32+
@Override
33+
public Task laterSync(Runnable task, Duration delay) {
34+
return new BukkitTaskImpl(this.rootScheduler.runTaskLater(this.plugin, task, this.toTick(delay)));
35+
}
36+
37+
@Override
38+
public Task laterAsync(Runnable task, Duration delay) {
39+
return new BukkitTaskImpl(this.rootScheduler.runTaskLaterAsynchronously(this.plugin, task, this.toTick(delay)));
40+
}
41+
42+
@Override
43+
public Task timerSync(Runnable task, Duration delay, Duration period) {
44+
return new BukkitTaskImpl(this.rootScheduler.runTaskTimer(this.plugin, task, this.toTick(delay), this.toTick(period)));
45+
}
46+
47+
@Override
48+
public Task timerAsync(Runnable task, Duration delay, Duration period) {
49+
return new BukkitTaskImpl(this.rootScheduler.runTaskTimerAsynchronously(this.plugin, task, this.toTick(delay), this.toTick(period)));
50+
}
51+
52+
@Override
53+
public <T> CompletableFuture<T> completeSync(Supplier<T> task) {
54+
CompletableFuture<T> completable = new CompletableFuture<>();
55+
this.rootScheduler.runTask(this.plugin, () -> completable.complete(task.get()));
56+
return completable;
57+
}
58+
59+
@Override
60+
public <T> CompletableFuture<T> completeAsync(Supplier<T> task) {
61+
CompletableFuture<T> completable = new CompletableFuture<>();
62+
this.rootScheduler.runTaskAsynchronously(this.plugin, () -> completable.complete(task.get()));
63+
return completable;
64+
}
65+
66+
private long toTick(Duration duration) {
67+
return duration.toMillis() / 50L;
68+
}
69+
70+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.eternalcode.commons.shared.bukkit.scheduler;
2+
3+
import com.eternalcode.commons.shared.scheduler.Task;
4+
import org.bukkit.scheduler.BukkitTask;
5+
6+
class BukkitTaskImpl implements Task {
7+
8+
private final BukkitTask rootTask;
9+
10+
BukkitTaskImpl(BukkitTask rootTask) {
11+
this.rootTask = rootTask;
12+
}
13+
14+
@Override
15+
public void cancel() {
16+
this.rootTask.cancel();
17+
}
18+
19+
@Override
20+
public boolean isCanceled() {
21+
return this.rootTask.isCancelled();
22+
}
23+
24+
@Override
25+
public boolean isAsync() {
26+
return !this.rootTask.isSync();
27+
}
28+
29+
}

commons-shared-bukkit/test/com/eternalcode/commons/shared/bukkit/position/PositionAdapterTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ void testConvertLocationToPosition() {
2727

2828
Position position = PositionAdapter.convert(location);
2929

30-
assertEquals(1, position.getX());
31-
assertEquals(2, position.getY());
32-
assertEquals(3, position.getZ());
33-
assertEquals(WORLD_NAME, position.getWorld());
30+
assertEquals(1, position.x());
31+
assertEquals(2, position.y());
32+
assertEquals(3, position.z());
33+
assertEquals(WORLD_NAME, position.world());
3434
}
3535

3636
@Test

commons-shared-bukkit/test/com/eternalcode/commons/shared/bukkit/position/PositionTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,32 @@ void setUp() {
1616

1717
@Test
1818
void getWorld() {
19-
assertEquals("world", position.getWorld());
19+
assertEquals("world", position.world());
2020
}
2121

2222
@Test
2323
void getX() {
24-
assertEquals(1.0d, position.getX());
24+
assertEquals(1.0d, position.x());
2525
}
2626

2727
@Test
2828
void getY() {
29-
assertEquals(2.0d, position.getY());
29+
assertEquals(2.0d, position.y());
3030
}
3131

3232
@Test
3333
void getZ() {
34-
assertEquals(3.0d, position.getZ());
34+
assertEquals(3.0d, position.z());
3535
}
3636

3737
@Test
3838
void getYaw() {
39-
assertEquals(4.0f, position.getYaw());
39+
assertEquals(4.0f, position.yaw());
4040
}
4141

4242
@Test
4343
void getPitch() {
44-
assertEquals(5.0f, position.getPitch());
44+
assertEquals(5.0f, position.pitch());
4545
}
4646

4747
@Test
@@ -55,14 +55,14 @@ void testEqualsAndHashCode() {
5555
Position position3 = new Position(1.0d, 2.0d, 3.1d, 4.0f, 5.0f, "world");
5656

5757
// Reflexive test
58-
assertTrue(position.equals(position));
58+
assertEquals(position, position);
5959

6060
// Symmetric test
61-
assertTrue(position.equals(position2));
62-
assertTrue(position2.equals(position));
61+
assertEquals(position, position2);
62+
assertEquals(position2, position);
6363

64-
assertFalse(position.equals(position3));
65-
assertFalse(position3.equals(position));
64+
assertNotEquals(position, position3);
65+
assertNotEquals(position3, position);
6666

6767
// hashCode
6868
assertEquals(position.hashCode(), position2.hashCode());
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.eternalcode.commons.shared.scheduler;
2+
3+
import java.time.Duration;
4+
import java.util.concurrent.CompletableFuture;
5+
import java.util.function.Supplier;
6+
7+
public interface Scheduler {
8+
9+
Task sync(Runnable task);
10+
11+
Task async(Runnable task);
12+
13+
Task laterSync(Runnable task, Duration delay);
14+
15+
Task laterAsync(Runnable task, Duration delay);
16+
17+
Task timerSync(Runnable task, Duration delay, Duration period);
18+
19+
Task timerAsync(Runnable task, Duration delay, Duration period);
20+
21+
<T> CompletableFuture<T> completeSync(Supplier<T> task);
22+
23+
<T> CompletableFuture<T> completeAsync(Supplier<T> task);
24+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.eternalcode.commons.shared.scheduler;
2+
3+
public interface Task {
4+
5+
void cancel();
6+
7+
boolean isCanceled();
8+
9+
boolean isAsync();
10+
11+
}

0 commit comments

Comments
 (0)