Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support folia using Energie #42

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ compileTestJava.options.encoding = "UTF-8"

java {
toolchain {
languageVersion = JavaLanguageVersion.of(16)
languageVersion = JavaLanguageVersion.of(17)
}
}

Expand Down Expand Up @@ -65,10 +65,11 @@ dependencies {
implementation 'com.cyr1en:kiso-utils:1.8-SNAPSHOT'
implementation 'com.cyr1en:kiso-mc:1.8-SNAPSHOT'
implementation 'net.wesjd:anvilgui:1.9.2-SNAPSHOT'
implementation 'io.github.rapha149.signgui:signgui:2.2.2'
implementation 'de.rapha149.signgui:signgui:2.3.2'
implementation 'org.bstats:bstats-bukkit:3.0.0'
implementation "dev.jorel:commandapi-bukkit-shade:9.3.0"
implementation group: 'org.fusesource.jansi', name: 'jansi', version: '2.4.0'
implementation("com.github.Euphillya:Energie:1.1.4")

// Exclude these
implementation 'me.lucko:jar-relocator:1.7'
Expand Down Expand Up @@ -106,7 +107,7 @@ shadowJar {

relocate 'com.github.stefvanschie.inventoryframework', 'com.cyr1en.inventoryframework'
relocate 'net.wesjd.anvilgui', 'com.cyr1en.anvilgui'
relocate 'io.github.rapha149.signgui', 'com.cyr1en.signgui'
relocate 'de.rapha149.signgui', 'com.cyr1en.signgui'
relocate 'net.kyori.adventure.text.minimessage', 'com.cyr1en.minimessage'
relocate 'dev.jorel.commandapi', 'com.cyr1en.commandapi'
relocate 'org.fusesource.jansi', 'com.cyr1en.jansi'
Expand Down Expand Up @@ -141,7 +142,7 @@ tasks.register("lightJar", ShadowJar) {
}
relocate 'com.github.stefvanschie.inventoryframework', 'com.cyr1en.inventoryframework'
relocate 'net.wesjd.anvilgui', 'com.cyr1en.anvilgui'
relocate 'io.github.rapha149.signgui', 'com.cyr1en.signgui'
relocate 'de.rapha149.signgui', 'com.cyr1en.signgui'
relocate 'net.kyori.adventure.text.minimessage', 'com.cyr1en.minimessage'
relocate 'org.fusesource.jansi', 'com.cyr1en.jansi'
relocate 'dev.jorel.commandapi', 'com.cyr1en.commandapi'
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/com/cyr1en/commandprompter/CommandPrompter.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@
import com.cyr1en.commandprompter.unsafe.CommandMapHacker;
import com.cyr1en.commandprompter.unsafe.ModifiedCommandMap;
import com.cyr1en.commandprompter.unsafe.PvtFieldMutator;
import com.cyr1en.commandprompter.util.FoliaUpdateChecker;
import com.cyr1en.commandprompter.util.Util;
import com.cyr1en.commandprompter.util.Util.ServerType;
import com.cyr1en.kiso.mc.I18N;
import com.cyr1en.kiso.mc.UpdateChecker;
import com.cyr1en.kiso.utils.SRegex;
import fr.euphyllia.energie.Energie;
import fr.euphyllia.energie.model.Scheduler;
import fr.euphyllia.energie.model.SchedulerType;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
Expand All @@ -56,6 +60,7 @@
public class CommandPrompter extends JavaPlugin {

private static CommandPrompter instance;
private static Scheduler scheduler;

private ConfigurationManager configManager;
private CommandPrompterConfig config;
Expand All @@ -73,6 +78,8 @@ public class CommandPrompter extends JavaPlugin {

@Override
public void onEnable() {
instance = this;
scheduler = new Energie(this).getScheduler(Energie.SchedulerSoft.MINECRAFT);

new Metrics(this, 5359);
setupConfig();
Expand All @@ -96,10 +103,9 @@ public void onEnable() {
initPromptSystem();
setupCommands();

instance = this;
Bukkit.getPluginManager().registerEvents(new CommandSendListener(this), this);

Bukkit.getScheduler().runTaskLater(this, () -> {
scheduler.runDelayed(SchedulerType.SYNC, task -> {
hookContainer = new HookContainer(this);
hookContainer.initHooks();
headCache.registerFilters();
Expand Down Expand Up @@ -172,7 +178,7 @@ private void initCommandListener() {
return;
}
var delay = (long) config.modificationDelay();
Bukkit.getScheduler().runTaskLater(this, this::hackMap, delay);
scheduler.runDelayed(SchedulerType.SYNC, task -> this.hackMap(), delay);
}

private void hackMap() {
Expand Down Expand Up @@ -206,10 +212,10 @@ private void setupCommands() {
}

private void setupUpdater() {
updateChecker = new UpdateChecker(this, 47772);
updateChecker = Energie.isFolia() ? new FoliaUpdateChecker(this, 47772) : new UpdateChecker(this, 47772);
if (updateChecker.isDisabled())
return;
Bukkit.getServer().getScheduler().runTaskAsynchronously(this, () -> {
scheduler.runTask(SchedulerType.ASYNC, task -> {
if (updateChecker.newVersionAvailable())
logger.info(SRegex.ANSI_GREEN + "A new update is available! (" +
updateChecker.getCurrVersion().asString() + ")" + SRegex.ANSI_RESET);
Expand Down Expand Up @@ -264,6 +270,10 @@ public static CommandPrompter getInstance() {
return instance;
}

public static Scheduler getScheduler() {
return scheduler;
}

public CommandPrompterConfig getConfiguration() {
return config;
}
Expand Down
37 changes: 19 additions & 18 deletions src/main/java/com/cyr1en/commandprompter/api/Dispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
package com.cyr1en.commandprompter.api;

import com.cyr1en.commandprompter.CommandPrompter;
import fr.euphyllia.energie.Energie;
import fr.euphyllia.energie.model.SchedulerType;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;

/**
Expand All @@ -47,17 +49,12 @@ public class Dispatcher {
* Dispatches command by forcing a player to chat the command.
* This will allow plugins to support CommandPrompter.
*
* @param plugin Instance of plugin.
* @param sender command sender (in menu's, then the item clicker)
* @param command command that would be dispatched.
*/
public static void dispatchCommand(Plugin plugin, Player sender, String command) {
public static void dispatchCommand(Player sender, String command) {
final String checked = command.codePointAt(0) == 0x2F ? command : "/" + command;
new BukkitRunnable() {
public void run() {
sender.chat(checked);
}
}.runTask(plugin);
CommandPrompter.getScheduler().runTask(SchedulerType.SYNC, sender, task -> sender.chat(checked), null);
}

/**
Expand All @@ -67,11 +64,7 @@ public void run() {
*/
public static void dispatchConsole(final String command) {
final String checked = command.codePointAt(0) == 0x2F ? command.substring(1) : command;
new BukkitRunnable() {
public void run() {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), checked);
}
}.runTask(CommandPrompter.getInstance());
CommandPrompter.getScheduler().runTask(SchedulerType.SYNC, task -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), checked));
}

/**
Expand All @@ -91,7 +84,14 @@ public static void dispatchWithAttachment(Plugin plugin, Player sender, String c

logger.debug("Dispatching command with permission attachment");

var attachment = sender.addAttachment(plugin, ticks);
PermissionAttachment attachment;
if (!Energie.isFolia()) {
attachment = sender.addAttachment(plugin, ticks);
} else {
attachment = sender.addAttachment(plugin);
CommandPrompter.getScheduler().runDelayed(SchedulerType.SYNC, task -> attachment.remove(), ticks);
}

if (attachment == null) {
logger.err("Unable to create PermissionAttachment for " + sender.getName());
return;
Expand All @@ -103,9 +103,10 @@ public static void dispatchWithAttachment(Plugin plugin, Player sender, String c
}
attachment.getPermissible().recalculatePermissions();
final String checked = command.codePointAt(0) == 0x2F ? command.substring(1) : command;
Bukkit.dispatchCommand(sender, checked);
//dispatchCommand(plugin, sender, command);
sender.removeAttachment(attachment);
CommandPrompter.getScheduler().runTask(SchedulerType.SYNC, sender, task -> {
Bukkit.dispatchCommand(sender, checked);
//dispatchCommand(plugin, sender, command);
sender.removeAttachment(attachment);
}, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.cyr1en.commandprompter.hook.annotations.TargetPlugin;
import com.cyr1en.commandprompter.prompt.PromptContext;
import com.cyr1en.commandprompter.prompt.PromptManager;
import fr.euphyllia.energie.model.SchedulerType;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.events.CarbonChatEvent;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -58,6 +59,6 @@ public void handle(CarbonChatEvent event) {
msg = prompt.sanitizeInput() ? ds : msg;
}
var ctx = new PromptContext.Builder().setSender(player).setContent(msg).build();
Bukkit.getScheduler().runTask(getPlugin(), () -> promptManager.processPrompt(ctx));
CommandPrompter.getScheduler().runTask(SchedulerType.SYNC, task -> promptManager.processPrompt(ctx));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.cyr1en.commandprompter.prompt.prompts.PlayerUIPrompt;
import com.cyr1en.commandprompter.prompt.prompts.SignPrompt;
import com.cyr1en.commandprompter.util.Util;
import fr.euphyllia.energie.model.Scheduler;
import fr.euphyllia.energie.model.SchedulerType;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -61,13 +63,13 @@ public class PromptManager extends HashMap<String, Class<? extends Prompt>> {
private final CommandPrompter plugin;
private final PromptRegistry promptRegistry;
private final PromptParser promptParser;
private final BukkitScheduler scheduler;
private final Scheduler scheduler;

public PromptManager(CommandPrompter commandPrompter) {
this.plugin = commandPrompter;
this.promptRegistry = new PromptRegistry(plugin);
this.promptParser = new PromptParser(this);
this.scheduler = Bukkit.getScheduler();
this.scheduler = CommandPrompter.getScheduler();
}

public void registerPrompts() {
Expand All @@ -88,7 +90,7 @@ public Class<? extends Prompt> put(String key, Class<? extends Prompt> value) {
public void parse(PromptContext context) {
var queueHash = promptParser.parsePrompts(context);
var timeout = plugin.getConfiguration().promptTimeout();
scheduler.runTaskLater(plugin, () -> cancel(context.getSender(), queueHash), 20L * timeout);
scheduler.runDelayed(SchedulerType.SYNC, task -> cancel(context.getSender(), queueHash), 20L * timeout);
}

public void sendPrompt(CommandSender sender) {
Expand All @@ -102,7 +104,7 @@ public void sendPrompt(CommandSender sender) {

if (!queue.isEmpty()) {
var prompt = Objects.requireNonNull(queue.peek());
Bukkit.getScheduler().runTaskLater(plugin, prompt::sendPrompt, 2L);
scheduler.runDelayed(SchedulerType.SYNC, task -> prompt.sendPrompt(), 2L);
plugin.getPluginLogger().debug("Sent %s to %s", prompt.getClass().getSimpleName(), sender.getName());
} else if (queue.containsPCM()) {
// This means queue is empty but contains PCM. If it does, we just dispatch it.
Expand Down Expand Up @@ -177,7 +179,7 @@ private void dispatchQueue(CommandSender sender, PromptQueue queue) {
sender.setOp(false);
plugin.getPluginLogger().debug("Remove OP status");
// Redundancy for de-op
Bukkit.getScheduler().runTaskLater(plugin, () -> {
scheduler.runDelayed(SchedulerType.SYNC, task -> {
plugin.getPluginLogger().debug("Remove OP status (redundancy)");
sender.setOp(false);
}, 2L);
Expand Down Expand Up @@ -231,7 +233,7 @@ public void cancel(CommandSender sender, int queueHash) {
return;

if (pcm.delayTicks() > 0)
plugin.getServer().getScheduler().runTaskLater(plugin, () -> queue.execPCM(pcm, (Player) sender),
scheduler.runDelayed(SchedulerType.SYNC, task -> queue.execPCM(pcm, (Player) sender),
pcm.delayTicks());
else
queue.execPCM(pcm, (Player) sender);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.cyr1en.commandprompter.api.prompt.Prompt;
import com.cyr1en.commandprompter.util.MMUtil;
import com.cyr1en.kiso.utils.SRegex;
import fr.euphyllia.energie.model.SchedulerType;
import org.bukkit.entity.Player;

import java.util.Arrays;
Expand Down Expand Up @@ -107,15 +108,15 @@ public void dispatch(CommandPrompter plugin, Player sender) {
plugin.getConfiguration().permissionAttachmentTicks(),
plugin.getConfiguration().getPermissionAttachment(permissionAttachmentKey));
} else
Dispatcher.dispatchCommand(plugin, sender, getCompleteCommand());
Dispatcher.dispatchCommand(sender, getCompleteCommand());

if (!postCommandMetas.isEmpty())
postCommandMetas.forEach(pcm -> {
if (pcm.isOnCancel())
return;

if (pcm.delayTicks() > 0)
plugin.getServer().getScheduler().runTaskLater(plugin, () -> execPCM(pcm, sender),
CommandPrompter.getScheduler().runDelayed(SchedulerType.SYNC, task -> execPCM(pcm, sender),
pcm.delayTicks());
else
execPCM(pcm, sender);
Expand All @@ -138,7 +139,7 @@ void execPCM(PostCommandMeta postCommandMeta, Player sender) {
Dispatcher.dispatchConsole(command);
} else {
logger.debug("Dispatching PostCommand as player");
Dispatcher.dispatchCommand(CommandPrompter.getInstance(), sender, command);
Dispatcher.dispatchCommand(sender, command);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.cyr1en.commandprompter.prompt.PromptParser;
import com.cyr1en.commandprompter.unsafe.PvtFieldMutator;
import es.capitanpuerka.puerkaschat.manager.PuerkasFormat;
import fr.euphyllia.energie.model.SchedulerType;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
Expand Down Expand Up @@ -138,7 +139,7 @@ public void onResponse(Player player, String msg, Cancellable event) {
.setSender(player)
.setContent(msg).build();

Bukkit.getScheduler().runTask(plugin, () -> manager.processPrompt(ctx));
CommandPrompter.getScheduler().runTask(SchedulerType.SYNC, task -> manager.processPrompt(ctx));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.cyr1en.commandprompter.util.Util;
import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
import com.github.stefvanschie.inventoryframework.pane.PaginatedPane;
import fr.euphyllia.energie.model.SchedulerType;
import org.bukkit.Bukkit;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -211,7 +212,7 @@ public void sendPrompt() {
getPlugin().getPluginLogger().debug("Missing heads in cache, rebuilding before sending...");
headCache.reBuildCache().thenAccept(cache -> {
getPlugin().getPluginLogger().debug("Rebuilt cache!");
Bukkit.getScheduler().runTask(getPlugin(), () -> send(p));
CommandPrompter.getScheduler().runTask(SchedulerType.SYNC, task -> send(p));
});
} else {
send(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.cyr1en.commandprompter.prompt.PromptParser;
import com.cyr1en.commandprompter.util.Util;
import com.cyr1en.kiso.utils.FastStrings;
import io.github.rapha149.signgui.SignGUI;
import io.github.rapha149.signgui.SignGUIAction;
import de.rapha149.signgui.SignGUI;
import de.rapha149.signgui.SignGUIAction;
import org.bukkit.Material;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -47,6 +47,7 @@ else if (parts.size() > 4)
.setLines(finalParts.toArray(String[]::new))
.setType(mat)
.setHandler((p, r) -> process(finalParts, p, r.getLines()))
.callHandlerSynchronously(CommandPrompter.getInstance())
.build();

gui.open((Player) getContext().getSender());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import fr.euphyllia.energie.model.SchedulerType;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -227,7 +228,7 @@ public void onPlayerLogin(PlayerLoginEvent e) {
var cacheDelay = plugin.getPromptConfig().cacheDelay();
logger.debug("Caching Delay: %s", cacheDelay);

Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, () -> {
CommandPrompter.getScheduler().runDelayed(SchedulerType.ASYNC, task -> {
if (isVanished(e.getPlayer())) {
logger.debug("Player is vanished");
return;
Expand Down
Loading