Skip to content

Commit 210d022

Browse files
committed
bukkit-example plugin folia support
1 parent f375ca6 commit 210d022

File tree

18 files changed

+400
-118
lines changed

18 files changed

+400
-118
lines changed

bukkit-example-api/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ plugins {
33
id("apollo.shadow-conventions")
44
}
55

6+
java {
7+
javaTarget(21)
8+
}
9+
610
dependencies {
711
compileOnly(project(":extra:apollo-extra-adventure4"))
812
compileOnly(project(path = ":apollo-api", configuration = "bukkit"))
913
compileOnly(project(path = ":apollo-common", configuration = "shadow"))
1014

15+
compileOnly(libs.folia)
1116
implementation(project(":apollo-bukkit-example-common"))
1217
}

bukkit-example-api/src/main/java/com/lunarclient/apollo/example/api/module/ChatApiExample.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
import com.lunarclient.apollo.Apollo;
2727
import com.lunarclient.apollo.example.ApolloExamplePlugin;
2828
import com.lunarclient.apollo.example.module.impl.ChatExample;
29+
import com.lunarclient.apollo.example.util.ServerUtil;
2930
import com.lunarclient.apollo.module.chat.ChatModule;
3031
import com.lunarclient.apollo.recipients.Recipients;
32+
import java.util.concurrent.atomic.AtomicInteger;
3133
import net.kyori.adventure.text.Component;
3234
import net.kyori.adventure.text.format.NamedTextColor;
35+
import org.bukkit.Bukkit;
3336
import org.bukkit.scheduler.BukkitRunnable;
3437

3538
public class ChatApiExample extends ChatExample {
@@ -38,6 +41,14 @@ public class ChatApiExample extends ChatExample {
3841

3942
@Override
4043
public void displayLiveChatMessageExample() {
44+
if (ServerUtil.isFolia()) {
45+
this.runFoliaChatMessageTask();
46+
} else {
47+
this.runBukkitChatMessageTask();
48+
}
49+
}
50+
51+
private void runBukkitChatMessageTask() {
4152
BukkitRunnable runnable = new BukkitRunnable() {
4253

4354
private int countdown = 5;
@@ -63,7 +74,29 @@ public void run() {
6374
}
6475
};
6576

66-
runnable.runTaskTimer(ApolloExamplePlugin.getInstance(), 0L, 20L);
77+
runnable.runTaskTimer(ApolloExamplePlugin.getInstance(), 1L, 20L);
78+
}
79+
80+
private void runFoliaChatMessageTask() {
81+
AtomicInteger countdown = new AtomicInteger(5);
82+
83+
Bukkit.getGlobalRegionScheduler().runAtFixedRate(ApolloExamplePlugin.getInstance(), task -> {
84+
int seconds = countdown.getAndDecrement();
85+
if (seconds > 0) {
86+
ChatApiExample.this.chatModule.displayLiveChatMessage(Recipients.ofEveryone(),
87+
Component.text("Game starting in ", NamedTextColor.GREEN)
88+
.append(Component.text(seconds, NamedTextColor.BLUE)),
89+
13
90+
);
91+
} else {
92+
ChatApiExample.this.chatModule.displayLiveChatMessage(Recipients.ofEveryone(),
93+
Component.text("Game started! ", NamedTextColor.GREEN),
94+
13
95+
);
96+
97+
task.cancel();
98+
}
99+
}, 1L, 20L);
67100
}
68101

69102
@Override

bukkit-example-api/src/main/java/com/lunarclient/apollo/example/api/module/TeamApiExample.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.lunarclient.apollo.common.location.ApolloLocation;
3030
import com.lunarclient.apollo.example.ApolloExamplePlugin;
3131
import com.lunarclient.apollo.example.module.impl.TeamExample;
32+
import com.lunarclient.apollo.example.util.ServerUtil;
3233
import com.lunarclient.apollo.module.team.TeamMember;
3334
import com.lunarclient.apollo.module.team.TeamModule;
3435
import java.awt.Color;
@@ -37,6 +38,7 @@
3738
import java.util.Optional;
3839
import java.util.Set;
3940
import java.util.UUID;
41+
import java.util.concurrent.TimeUnit;
4042
import java.util.stream.Collectors;
4143
import net.kyori.adventure.text.Component;
4244
import net.kyori.adventure.text.format.NamedTextColor;
@@ -46,7 +48,6 @@
4648
import org.bukkit.event.EventHandler;
4749
import org.bukkit.event.Listener;
4850
import org.bukkit.event.player.PlayerQuitEvent;
49-
import org.bukkit.scheduler.BukkitRunnable;
5051

5152
public class TeamApiExample extends TeamExample implements Listener {
5253

@@ -56,7 +57,11 @@ public class TeamApiExample extends TeamExample implements Listener {
5657
private final Map<UUID, Team> teamsByPlayerUuid = Maps.newHashMap();
5758

5859
public TeamApiExample() {
59-
new TeamUpdateTask();
60+
if (ServerUtil.isFolia()) {
61+
this.runFoliaTeamUpdateTask();
62+
} else {
63+
this.runBukkitTeamUpdateTask();
64+
}
6065

6166
Bukkit.getPluginManager().registerEvents(this, ApolloExamplePlugin.getInstance());
6267
}
@@ -95,6 +100,18 @@ public void deleteTeam(UUID teamId) {
95100
}
96101
}
97102

103+
private void runBukkitTeamUpdateTask() {
104+
Bukkit.getScheduler().runTaskTimerAsynchronously(ApolloExamplePlugin.getInstance(), () -> {
105+
this.teamsByTeamId.values().forEach(Team::refresh);
106+
}, 1L, 1L);
107+
}
108+
109+
private void runFoliaTeamUpdateTask() {
110+
Bukkit.getAsyncScheduler().runAtFixedRate(ApolloExamplePlugin.getInstance(), task -> {
111+
this.teamsByTeamId.values().forEach(Team::refresh);
112+
}, 50L, 50L, TimeUnit.MILLISECONDS);
113+
}
114+
98115
public class Team {
99116

100117
private final UUID teamId;
@@ -175,19 +192,6 @@ public int hashCode() {
175192
}
176193
}
177194

178-
// Updates players location every 1 tick (50ms)
179-
public class TeamUpdateTask extends BukkitRunnable {
180-
181-
public TeamUpdateTask() {
182-
this.runTaskTimerAsynchronously(ApolloExamplePlugin.getInstance(), 1L, 1L);
183-
}
184-
185-
@Override
186-
public void run() {
187-
TeamApiExample.this.teamsByTeamId.values().forEach(Team::refresh);
188-
}
189-
}
190-
191195
@Override
192196
public void createTeam(Player player) {
193197
Optional<Team> teamOpt = this.getByPlayerUuid(player.getUniqueId());

bukkit-example-common/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ plugins {
33
id("apollo.shadow-conventions")
44
}
55

6+
java {
7+
javaTarget(21)
8+
}
9+
610
dependencies {
711
compileOnly(libs.bukkit)
812
compileOnly(libs.bukkit.api)

bukkit-example-common/src/main/java/com/lunarclient/apollo/example/module/impl/InventoryExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void inventoryModuleNMSExample(Player player) {
8787
inventory.setItem(29, ItemUtil.addTag(suggestCommandItem, "suggestCommand", "/apollo"));
8888

8989
ItemStack runCommandItem = ItemUtil.itemWithName(
90-
Material.BOOK_AND_QUILL,
90+
Material.ENCHANTED_BOOK,
9191
ChatColor.LIGHT_PURPLE.toString() + ChatColor.BOLD + "RUN COMMAND"
9292
);
9393

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of Apollo, licensed under the MIT License.
3+
*
4+
* Copyright (c) 2023 Moonsworth
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.lunarclient.apollo.example.util;
25+
26+
public final class ServerUtil {
27+
28+
public static boolean isFolia() {
29+
try {
30+
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
31+
return true;
32+
} catch (ClassNotFoundException e) {
33+
return false;
34+
}
35+
}
36+
37+
private ServerUtil() {
38+
}
39+
40+
}

bukkit-example-json/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ plugins {
33
id("apollo.shadow-conventions")
44
}
55

6+
java {
7+
javaTarget(21)
8+
}
9+
610
dependencies {
711
api(libs.bundles.adventure) {
812
exclude("org.checkerframework")
@@ -11,5 +15,6 @@ dependencies {
1115
}
1216

1317
compileOnly(libs.bukkit)
18+
compileOnly(libs.folia)
1419
implementation(project(":apollo-bukkit-example-common"))
1520
}

bukkit-example-json/src/main/java/com/lunarclient/apollo/example/json/module/ChatJsonExample.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,25 @@
2828
import com.lunarclient.apollo.example.json.util.AdventureUtil;
2929
import com.lunarclient.apollo.example.json.util.JsonPacketUtil;
3030
import com.lunarclient.apollo.example.module.impl.ChatExample;
31+
import com.lunarclient.apollo.example.util.ServerUtil;
32+
import java.util.concurrent.atomic.AtomicInteger;
3133
import net.kyori.adventure.text.Component;
3234
import net.kyori.adventure.text.format.NamedTextColor;
35+
import org.bukkit.Bukkit;
3336
import org.bukkit.scheduler.BukkitRunnable;
3437

3538
public class ChatJsonExample extends ChatExample {
3639

3740
@Override
3841
public void displayLiveChatMessageExample() {
42+
if (ServerUtil.isFolia()) {
43+
this.runFoliaChatMessageTask();
44+
} else {
45+
this.runBukkitChatMessageTask();
46+
}
47+
}
48+
49+
private void runBukkitChatMessageTask() {
3950
BukkitRunnable runnable = new BukkitRunnable() {
4051

4152
private int countdown = 5;
@@ -65,7 +76,34 @@ public void run() {
6576
}
6677
};
6778

68-
runnable.runTaskTimer(ApolloExamplePlugin.getInstance(), 0L, 20L);
79+
runnable.runTaskTimer(ApolloExamplePlugin.getInstance(), 1L, 20L);
80+
}
81+
82+
private void runFoliaChatMessageTask() {
83+
AtomicInteger countdown = new AtomicInteger(5);
84+
85+
Bukkit.getGlobalRegionScheduler().runAtFixedRate(ApolloExamplePlugin.getInstance(), task -> {
86+
JsonObject message = new JsonObject();
87+
message.addProperty("@type", "type.googleapis.com/lunarclient.apollo.chat.v1.DisplayLiveChatMessageMessage");
88+
message.addProperty("message_id", 13);
89+
90+
int seconds = countdown.getAndDecrement();
91+
if (seconds > 0) {
92+
message.addProperty("adventure_json_lines", AdventureUtil.toJson(
93+
Component.text("Game starting in ", NamedTextColor.GREEN)
94+
.append(Component.text(seconds, NamedTextColor.BLUE))
95+
));
96+
97+
JsonPacketUtil.broadcastPacket(message);
98+
} else {
99+
message.addProperty("adventure_json_lines", AdventureUtil.toJson(
100+
Component.text("Game started! ", NamedTextColor.GREEN)
101+
));
102+
103+
JsonPacketUtil.broadcastPacket(message);
104+
task.cancel();
105+
}
106+
}, 1L, 20L);
69107
}
70108

71109
@Override

bukkit-example-json/src/main/java/com/lunarclient/apollo/example/json/module/TeamJsonExample.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,32 @@
3232
import com.lunarclient.apollo.example.json.util.JsonPacketUtil;
3333
import com.lunarclient.apollo.example.json.util.JsonUtil;
3434
import com.lunarclient.apollo.example.module.impl.TeamExample;
35+
import com.lunarclient.apollo.example.util.ServerUtil;
3536
import java.awt.Color;
3637
import java.util.Map;
3738
import java.util.Optional;
3839
import java.util.Set;
3940
import java.util.UUID;
41+
import java.util.concurrent.TimeUnit;
4042
import net.kyori.adventure.text.Component;
4143
import net.kyori.adventure.text.format.NamedTextColor;
4244
import org.bukkit.Bukkit;
4345
import org.bukkit.entity.Player;
4446
import org.bukkit.event.EventHandler;
4547
import org.bukkit.event.Listener;
4648
import org.bukkit.event.player.PlayerQuitEvent;
47-
import org.bukkit.scheduler.BukkitRunnable;
4849

4950
public class TeamJsonExample extends TeamExample implements Listener {
5051

5152
private final Map<UUID, Team> teamsByTeamId = Maps.newHashMap();
5253
private final Map<UUID, Team> teamsByPlayerUuid = Maps.newHashMap();
5354

5455
public TeamJsonExample() {
55-
new TeamUpdateTask();
56+
if (ServerUtil.isFolia()) {
57+
this.runFoliaTeamUpdateTask();
58+
} else {
59+
this.runBukkitTeamUpdateTask();
60+
}
5661

5762
Bukkit.getPluginManager().registerEvents(this, ApolloExamplePlugin.getInstance());
5863
}
@@ -91,6 +96,18 @@ public void deleteTeam(UUID teamId) {
9196
}
9297
}
9398

99+
private void runBukkitTeamUpdateTask() {
100+
Bukkit.getScheduler().runTaskTimerAsynchronously(ApolloExamplePlugin.getInstance(), () -> {
101+
this.teamsByTeamId.values().forEach(Team::refresh);
102+
}, 1L, 1L);
103+
}
104+
105+
private void runFoliaTeamUpdateTask() {
106+
Bukkit.getAsyncScheduler().runAtFixedRate(ApolloExamplePlugin.getInstance(), task -> {
107+
this.teamsByTeamId.values().forEach(Team::refresh);
108+
}, 50L, 50L, TimeUnit.MILLISECONDS);
109+
}
110+
94111
public class Team {
95112

96113
private final UUID teamId;
@@ -118,7 +135,6 @@ public void removeMember(Player player) {
118135

119136
private JsonObject createTeamMember(Player member) {
120137
JsonObject message = new JsonObject();
121-
message.addProperty("@type", "type.googleapis.com/lunarclient.apollo.team.v1.TeamMember");
122138
message.add("player_uuid", JsonUtil.createUuidObject(member.getUniqueId()));
123139
message.addProperty("adventure_json_player_name", AdventureUtil.toJson(
124140
Component.text()
@@ -173,19 +189,6 @@ public int hashCode() {
173189
}
174190
}
175191

176-
// Updates players location every 1 tick (50ms)
177-
public class TeamUpdateTask extends BukkitRunnable {
178-
179-
public TeamUpdateTask() {
180-
this.runTaskTimerAsynchronously(ApolloExamplePlugin.getInstance(), 1L, 1L);
181-
}
182-
183-
@Override
184-
public void run() {
185-
TeamJsonExample.this.teamsByTeamId.values().forEach(Team::refresh);
186-
}
187-
}
188-
189192
@Override
190193
public void createTeam(Player player) {
191194
Optional<Team> teamOpt = this.getByPlayerUuid(player.getUniqueId());

bukkit-example-json/src/main/resources/plugin.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version: 1.1.7
44
author: Moonsworth
55
softdepend: [ Apollo-Bukkit ]
66
api-version: 1.13
7+
folia-supported: true
78

89
commands:
910
autotexthotkey:

0 commit comments

Comments
 (0)