Skip to content

Commit

Permalink
Folia Support (#46)
Browse files Browse the repository at this point in the history
* Folia Support

* Unused import.

* Changelog and min. version bump.

---------

Co-authored-by: LlmDl <[email protected]>
  • Loading branch information
Warriorrrr and LlmDl authored May 9, 2023
1 parent 60168fa commit 945b66f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 19 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.palmergames.bukkit</groupId>
<artifactId>TownyChat</artifactId>
<packaging>jar</packaging>
<version>0.104</version>
<version>0.105</version>

<licenses>
<license>
Expand All @@ -26,7 +26,7 @@
<java.version>17</java.version>
<project.bukkitAPIVersion>1.16</project.bukkitAPIVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<towny.version>0.99.0.0</towny.version>
<towny.version>0.99.0.6</towny.version>
</properties>

<!-- For use with GitHub Package Registry -->
Expand Down Expand Up @@ -75,7 +75,7 @@
<dependency>
<groupId>com.github.TownyAdvanced</groupId>
<artifactId>Towny</artifactId>
<version>0.99.0.0</version>
<version>${towny.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
5 changes: 4 additions & 1 deletion resources/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,7 @@ v0.104:
- Update min. Towny version to 0.99.0.0.
- Update EssentialsX maven details.
- Translate spy message colour codes.
- Closes https://github.com/TownyAdvanced/Towny/issues/6634
- Closes https://github.com/TownyAdvanced/Towny/issues/6634
v0.105:
- Add Folia support, courtesy of Warrior with PR #46.
- Update min. Towny version to 0.99.0.6.
1 change: 1 addition & 0 deletions resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ description: >
TownyChat plugin which hooks into Towny.
depend: [Towny]
softdepend: [CraftIRC, PlaceholderAPI, dynmap, EssentialsDiscord]
folia-supported: true

############################################################
# +------------------------------------------------------+ #
Expand Down
35 changes: 24 additions & 11 deletions src/com/palmergames/bukkit/TownyChat/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import com.palmergames.bukkit.TownyChat.tasks.onLoadedTask;
import com.palmergames.bukkit.TownyChat.util.EssentialsIntegration;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.scheduling.TaskScheduler;
import com.palmergames.bukkit.towny.scheduling.impl.BukkitTaskScheduler;
import com.palmergames.bukkit.towny.scheduling.impl.FoliaTaskScheduler;
import com.palmergames.bukkit.util.Version;
import com.palmergames.util.FileMgmt;

Expand Down Expand Up @@ -51,21 +54,25 @@ public class Chat extends JavaPlugin {

protected PluginManager pm;
private static Chat chat = null;
private final TaskScheduler scheduler;
private Towny towny = null;
private DynmapAPI dynMap = null;
private Essentials essentials = null;

private static Version requiredTownyVersion = Version.fromString("0.99.0.0");
private static Version requiredTownyVersion = Version.fromString("0.99.0.6");
public static boolean usingPlaceholderAPI = false;
public static boolean usingEssentialsDiscord = false;
boolean chatConfigError = false;
boolean channelsConfigError = false;
private static ConcurrentMap<UUID, Channel> playerChannelMap;

public Chat() {
chat = this;
this.scheduler = isFoliaClassPresent() ? new FoliaTaskScheduler(this) : new BukkitTaskScheduler(this);
}

@Override
public void onEnable() {

chat = this;
pm = getServer().getPluginManager();
channelsConfig = new ChannelConfigurationHandler(this);
channels = new ChannelsHolder(this);
Expand All @@ -89,14 +96,7 @@ public void onEnable() {
* This executes the task with a 1 tick delay avoiding the bukkit depends bug.
* TODO: What bug is this referencing? This goes back to the first version of TownyChat.
*/
if (getServer().getScheduler().scheduleSyncDelayedTask(this, new onLoadedTask(this), 1) == -1) {
/*
* We either failed to find Towny or the Scheduler failed to
* register the task.
*/
disableWithMessage("Could not schedule onLoadedTask.");
return;
}
scheduler.run(new onLoadedTask(this));

getCommand("townychat").setExecutor(new TownyChatCommand(this));
getCommand("channel").setExecutor(new ChannelCommand(this));
Expand Down Expand Up @@ -300,4 +300,17 @@ public Channel getPlayerChannel(Player player) {
public void setPlayerChannel(Player player, Channel channel) {
playerChannelMap.put(player.getUniqueId(), channel);
}

public TaskScheduler getScheduler() {
return this.scheduler;
}

private static boolean isFoliaClassPresent() {
try {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.palmergames.bukkit.TownyChat.events.PlayerJoinChatChannelEvent;
import com.palmergames.bukkit.towny.TownyMessaging;
import com.palmergames.bukkit.towny.object.Translatable;
import com.palmergames.bukkit.towny.scheduling.TaskScheduler;
import com.palmergames.bukkit.util.Colors;
import com.palmergames.util.StringMgmt;

Expand Down Expand Up @@ -75,8 +76,9 @@ public boolean execute(CommandSender commandSender, String label, String[] args)
final String msg = message;

// https://www.spigotmc.org/threads/plugins-triggering-commands-async.31815/
if (!Bukkit.isPrimaryThread()) {
Bukkit.getScheduler().runTask(plugin, () -> player.chat(msg));
TaskScheduler scheduler = plugin.getScheduler();
if (!scheduler.isEntityThread(player)) {
scheduler.run(player, () -> player.chat(msg));
} else {
player.chat(msg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import me.clip.placeholderapi.PlaceholderAPI;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -39,7 +38,7 @@ public TownyChatPlayerListener(Chat instance) {
@EventHandler(priority = EventPriority.LOW)
public void onPlayerJoin(final PlayerJoinEvent event) {

Bukkit.getScheduler().runTaskLater(plugin, () -> loginPlayer(event.getPlayer()), 2l);
plugin.getScheduler().runLater(event.getPlayer(), () -> loginPlayer(event.getPlayer()), 2L);

}

Expand Down

0 comments on commit 945b66f

Please sign in to comment.