Skip to content

Commit 0d8b9b5

Browse files
Merge pull request #7 from TechnicallyCoded/dev
Support Java 1.8 while retaining gradle builds & a direct folia api dependency on it's sub-module.
2 parents 208607e + 318ca67 commit 0d8b9b5

23 files changed

Lines changed: 458 additions & 183 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ bin/
4545
/.idea/
4646
/gradle/
4747
gradlew
48-
gradlew.bat
48+
gradlew.bat
49+
/target/

build.gradle

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,69 @@
1-
plugins {
2-
id 'java'
1+
buildscript {
2+
ext {
3+
revision = '0.2.4'
4+
}
35
}
46

5-
group 'com.tcoded'
6-
version '0.2.3'
7+
allprojects {
8+
apply plugin: 'java'
79

8-
java {
9-
sourceCompatibility = JavaVersion.VERSION_17
10-
targetCompatibility = JavaVersion.VERSION_17
11-
}
10+
version = '0.2.5'
11+
12+
repositories {
13+
14+
mavenCentral()
15+
16+
maven {
17+
url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
18+
}
19+
20+
maven {
21+
url 'https://hub.spigotmc.org/nexus/content/repositories/public/'
22+
}
23+
24+
maven {
25+
url = "https://repo.papermc.io/repository/maven-public/"
26+
}
27+
28+
maven {
29+
url = "https://nexuslite.gcnt.net/repos/paper/"
30+
}
31+
}
1232

13-
repositories {
14-
mavenCentral()
15-
maven {
16-
url 'https://repo.papermc.io/repository/maven-public/'
33+
java {
34+
sourceCompatibility = JavaVersion.VERSION_1_8
35+
targetCompatibility = JavaVersion.VERSION_1_8
36+
}
37+
38+
dependencies {
39+
implementation 'org.jetbrains:annotations:23.0.0'
40+
41+
testImplementation platform('org.junit:junit-bom:5.9.1')
42+
testImplementation 'org.junit.jupiter:junit-jupiter'
43+
}
44+
45+
test {
46+
useJUnitPlatform()
1747
}
1848
}
1949

20-
dependencies {
21-
implementation 'dev.folia:folia-api:1.19.4-R0.1-SNAPSHOT'
22-
implementation 'org.jetbrains:annotations:23.0.0'
50+
group 'com.tcoded'
51+
52+
def baseName
2353

24-
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
25-
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
54+
subprojects.each { subproject -> evaluationDependsOn(subproject.path)}
55+
tasks.register('allJar', Jar) {
56+
dependsOn 'compileJava'
57+
dependsOn 'jar'
58+
dependsOn subprojects.tasks['build']
59+
baseName = 'allJar'
60+
subprojects.each { subproject ->
61+
from subproject.configurations.archives.allArtifacts.files.collect {
62+
zipTree(it)
63+
}
64+
}
2665
}
2766

28-
test {
29-
useJUnitPlatform()
67+
artifacts {
68+
archives allJar
3069
}

common/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
group = 'com.tcoded.folialib'
2+
3+
dependencies {
4+
compileOnly 'org.spigotmc:spigot-api:1.8.8-R0.1-SNAPSHOT'
5+
6+
implementation(project(":platform:common"))
7+
}

src/main/java/com/tcoded/folialib/FoliaLib.java renamed to common/src/main/java/com/tcoded/folialib/FoliaLib.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.tcoded.folialib.impl.*;
55
import org.bukkit.plugin.java.JavaPlugin;
66

7+
import java.lang.reflect.InvocationTargetException;
8+
79
public class FoliaLib {
810

911
private final JavaPlugin plugin;
@@ -36,11 +38,42 @@ public FoliaLib(JavaPlugin plugin) {
3638
// Apply the implementation based on the type
3739
this.implementationType = foundType;
3840
switch (foundType) {
39-
case FOLIA -> this.implementation = new FoliaImplementation(this);
40-
case PAPER -> this.implementation = new PaperImplementation(this);
41-
case SPIGOT -> this.implementation = new SpigotImplementation(this);
42-
default -> this.implementation = new UnsupportedImplementation(this);
41+
case FOLIA:
42+
this.implementation = this.createServerImpl("FoliaImplementation");
43+
break;
44+
case PAPER:
45+
this.implementation = this.createServerImpl("PaperImplementation");
46+
break;
47+
case SPIGOT:
48+
this.implementation = this.createServerImpl("SpigotImplementation");
49+
break;
50+
default:
51+
this.implementation = this.createServerImpl("UnsupportedImplementation");
52+
break;
53+
}
54+
55+
// Check for valid implementation
56+
if (this.implementation == null) {
57+
throw new IllegalStateException(
58+
"Failed to create server implementation. Please report this to the FoliaLib GitHub issues page. " +
59+
"Forks of server software may not all be supported. If you are using an unofficial fork, " +
60+
"please report this to the fork's developers first.");
61+
}
62+
}
63+
64+
private ServerImplementation createServerImpl(String implName) {
65+
String basePackage = "com.tcoded.folialib.impl.";
66+
67+
try {
68+
return (ServerImplementation) Class.forName(basePackage + implName)
69+
.getConstructor(this.getClass())
70+
.newInstance(this);
71+
} catch (InstantiationException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException |
72+
IllegalAccessException e) {
73+
e.printStackTrace();
4374
}
75+
76+
return null;
4477
}
4578

4679
@SuppressWarnings("unused")

src/main/java/com/tcoded/folialib/enums/ImplementationType.java renamed to common/src/main/java/com/tcoded/folialib/enums/ImplementationType.java

File renamed without changes.

src/main/java/com/tcoded/folialib/util/TimeConverter.java renamed to common/src/main/java/com/tcoded/folialib/util/TimeConverter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ public static long toTicks(long time, TimeUnit unit) {
88
return unit.toMillis(time) / 50;
99
}
1010

11+
public static long toMillis(long delay) {
12+
return delay * 50L;
13+
}
1114
}

platform/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
group = 'com.tcoded.folialib'

platform/common/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
group = 'com.tcoded.folialib.platform'
2+
3+
dependencies {
4+
compileOnly 'org.spigotmc:spigot-api:1.8.8-R0.1-SNAPSHOT'
5+
}

src/main/java/com/tcoded/folialib/enums/EntityTaskResult.java renamed to platform/common/src/main/java/com/tcoded/folialib/enums/EntityTaskResult.java

File renamed without changes.

src/main/java/com/tcoded/folialib/impl/ServerImplementation.java renamed to platform/common/src/main/java/com/tcoded/folialib/impl/ServerImplementation.java

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.tcoded.folialib.impl;
22

33
import com.tcoded.folialib.enums.EntityTaskResult;
4-
import com.tcoded.folialib.wrapper.WrappedTask;
4+
import com.tcoded.folialib.wrapper.task.WrappedTask;
55
import org.bukkit.Location;
66
import org.bukkit.entity.Entity;
77
import org.bukkit.entity.Player;
@@ -35,6 +35,16 @@ public interface ServerImplementation {
3535

3636
// ----- Run Later -----
3737

38+
/**
39+
* Folia: Synced with the server daylight cycle tick
40+
* Paper: Synced with the server main thread
41+
* Spigot: Synced with the server main thread
42+
* @param runnable Task to run
43+
* @param delay Delay before execution in ticks
44+
* @return WrappedTask instance
45+
*/
46+
WrappedTask runLater(Runnable runnable, long delay);
47+
3848
/**
3949
* Folia: Synced with the server daylight cycle tick
4050
* Paper: Synced with the server main thread
@@ -46,6 +56,16 @@ public interface ServerImplementation {
4656
*/
4757
WrappedTask runLater(Runnable runnable, long delay, TimeUnit unit);
4858

59+
/**
60+
* Folia: Async
61+
* Paper: Async
62+
* Spigot: Async
63+
* @param runnable Task to run
64+
* @param delay Delay before execution in ticks
65+
* @return WrappedTask instance
66+
*/
67+
WrappedTask runLaterAsync(Runnable runnable, long delay);
68+
4969
/**
5070
* Folia: Async
5171
* Paper: Async
@@ -59,6 +79,17 @@ public interface ServerImplementation {
5979

6080
// ----- Global Timers -----
6181

82+
/**
83+
* Folia: Synced with the server daylight cycle tick
84+
* Paper: Synced with the server main thread
85+
* Spigot: Synced with the server main thread
86+
* @param runnable Task to run
87+
* @param delay Delay before first execution in ticks
88+
* @param period Delay between executions in ticks
89+
* @return WrappedTask instance
90+
*/
91+
WrappedTask runTimer(Runnable runnable, long delay, long period);
92+
6293
/**
6394
* Folia: Synced with the server daylight cycle tick
6495
* Paper: Synced with the server main thread
@@ -71,6 +102,17 @@ public interface ServerImplementation {
71102
*/
72103
WrappedTask runTimer(Runnable runnable, long delay, long period, TimeUnit unit);
73104

105+
/**
106+
* Folia: Async
107+
* Paper: Async
108+
* Spigot: Async
109+
* @param runnable Task to run
110+
* @param delay Delay before first execution in ticks
111+
* @param period Delay between executions in ticks
112+
* @return WrappedTask instance
113+
*/
114+
WrappedTask runTimerAsync(Runnable runnable, long delay, long period);
115+
74116
/**
75117
* Folia: Async
76118
* Paper: Async
@@ -96,6 +138,17 @@ public interface ServerImplementation {
96138
*/
97139
CompletableFuture<Void> runAtLocation(Location location, Runnable runnable);
98140

141+
/**
142+
* Folia: Synced with the tick of the region of the chunk of the location
143+
* Paper: Synced with the server main thread
144+
* Spigot: Synced with the server main thread
145+
* @param location Location to run the task at
146+
* @param runnable Task to run
147+
* @param delay Delay before execution in ticks
148+
* @return WrappedTask instance
149+
*/
150+
WrappedTask runAtLocationLater(Location location, Runnable runnable, long delay);
151+
99152
/**
100153
* Folia: Synced with the tick of the region of the chunk of the location
101154
* Paper: Synced with the server main thread
@@ -108,6 +161,18 @@ public interface ServerImplementation {
108161
*/
109162
WrappedTask runAtLocationLater(Location location, Runnable runnable, long delay, TimeUnit unit);
110163

164+
/**
165+
* Folia: Synced with the tick of the region of the chunk of the location
166+
* Paper: Synced with the server main thread
167+
* Spigot: Synced with the server main thread
168+
* @param location Location to run the task at
169+
* @param runnable Task to run
170+
* @param delay Delay before first execution in ticks
171+
* @param period Delay between executions in ticks
172+
* @return WrappedTask instance
173+
*/
174+
WrappedTask runAtLocationTimer(Location location, Runnable runnable, long delay, long period);
175+
111176
/**
112177
* Folia: Synced with the tick of the region of the chunk of the location
113178
* Paper: Synced with the server main thread
@@ -144,6 +209,17 @@ public interface ServerImplementation {
144209
*/
145210
CompletableFuture<EntityTaskResult> runAtEntityWithFallback(Entity entity, Runnable runnable, Runnable fallback);
146211

212+
/**
213+
* Folia: Synced with the tick of the region of the entity (even if the entity moves)
214+
* Paper: Synced with the server main thread
215+
* Spigot: Synced with the server main thread
216+
* @param entity Entity to run the task at
217+
* @param runnable Task to run
218+
* @param delay Delay before execution in ticks
219+
* @return WrappedTask instance
220+
*/
221+
WrappedTask runAtEntityLater(Entity entity, Runnable runnable, long delay);
222+
147223
/**
148224
* Folia: Synced with the tick of the region of the entity (even if the entity moves)
149225
* Paper: Synced with the server main thread
@@ -156,6 +232,18 @@ public interface ServerImplementation {
156232
*/
157233
WrappedTask runAtEntityLater(Entity entity, Runnable runnable, long delay, TimeUnit unit);
158234

235+
/**
236+
* Folia: Synced with the tick of the region of the entity (even if the entity moves)
237+
* Paper: Synced with the server main thread
238+
* Spigot: Synced with the server main thread
239+
* @param entity Entity to run the task at
240+
* @param runnable Task to run
241+
* @param delay Delay before first execution in ticks
242+
* @param period Delay between executions in ticks
243+
* @return WrappedTask instance
244+
*/
245+
WrappedTask runAtEntityTimer(Entity entity, Runnable runnable, long delay, long period);
246+
159247
/**
160248
* Folia: Synced with the tick of the region of the entity (even if the entity moves)
161249
* Paper: Synced with the server main thread
@@ -181,23 +269,29 @@ public interface ServerImplementation {
181269
void cancelAllTasks();
182270

183271
/**
184-
* Get a player by name (approximately)
272+
* Get a player by name (approximately).
273+
* When using folia, this can be run sync or async. If this is run async on non-folia platforms, it will block
274+
* until the next tick to get the player safely.
185275
* @param name Name of the player
186-
* @return Player instance
276+
* @return Player instance or null if not found
187277
*/
188278
Player getPlayer(String name);
189279

190280
/**
191281
* Get a player by name (exactly)
282+
* When using folia, this can be run sync or async. If this is run async on non-folia platforms, it will block
283+
* until the next tick to get the player safely.
192284
* @param name Name of the player
193-
* @return Player instance
285+
* @return Player instance or null if not found
194286
*/
195287
Player getPlayerExact(String name);
196288

197289
/**
198290
* Get a player by UUID
291+
* When using folia, this can be run sync or async. If this is run async on non-folia platforms, it will block
292+
* until the next tick to get the player safely.
199293
* @param uuid UUID of the player
200-
* @return Player instance
294+
* @return Player instance or null if not found
201295
*/
202296
Player getPlayer(UUID uuid);
203297

0 commit comments

Comments
 (0)