1
mirror of https://github.com/CarmJos/ScriptItems synced 2026-06-04 11:08:17 +08:00

feat: 升级到 1.21,并提供folia支持

This commit is contained in:
2025-12-14 02:31:30 +08:00
parent 55cdd3e207
commit 7ac7d249fc
6 changed files with 102 additions and 64 deletions
+17
View File
@@ -143,6 +143,23 @@
<scope>provided</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>io.papermc.paper</groupId>-->
<!-- <artifactId>paper-api</artifactId>-->
<!-- <version>1.21.10-R0.1-SNAPSHOT</version>-->
<!-- <scope>provided</scope>-->
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.bukkit</groupId>-->
<!-- <artifactId>craftbukkit</artifactId>-->
<!-- </exclusion>-->
<!-- <exclusion>-->
<!-- <groupId>org.spigotmc</groupId>-->
<!-- <artifactId>spigot-api</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- </dependency>-->
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
@@ -12,6 +12,9 @@ import cc.carm.plugin.scriptitems.listener.ProtectListener;
import cc.carm.plugin.scriptitems.manager.ItemsManager;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import java.lang.reflect.Method;
public class Main extends EasyPlugin {
@@ -108,4 +111,20 @@ public class Main extends EasyPlugin {
return false;
}
}
public static void execute(Runnable task) {
if (isFolia()) {
try {
Method getSchedulerMethod = Main.getInstance().getServer().getClass().getMethod("getGlobalRegionScheduler");
Object scheduler = getSchedulerMethod.invoke(Main.getInstance().getServer());
Method executeMethod = scheduler.getClass().getMethod("execute", Plugin.class, Runnable.class);
executeMethod.invoke(scheduler, Main.getInstance(), task);
} catch (Exception e) {
Main.severe("Failed to execute command on Folia");
e.printStackTrace();
}
} else {
Main.getInstance().getScheduler().run(task);
}
}
}
@@ -1,5 +1,6 @@
package cc.carm.plugin.scriptitems.item;
import cc.carm.plugin.scriptitems.Main;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -22,8 +23,13 @@ public class ScriptAction {
return actionContent;
}
public boolean execute(Player player) {
return getType().execute(player, getActionContent());
public void execute(Player player) {
try {
getType().execute(player, getActionContent());
} catch (Exception ex) {
Main.severe("在为玩家执行 脚本动作 时发生错误: " + ex.getMessage());
ex.printStackTrace();
}
}
public static @Nullable ScriptAction read(@Nullable String actionString) {
@@ -1,5 +1,6 @@
package cc.carm.plugin.scriptitems.item;
import cc.carm.plugin.scriptitems.Main;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@@ -17,7 +18,20 @@ public class ScriptActionGroup {
}
public void execute(Player player) {
actions.forEach(action -> action.execute(player));
for (ScriptAction action : actions) { // Take actions first
if (action.getType() == ScriptActionType.TAKE) {
action.execute(player);
}
}
Main.execute(() -> {
for (ScriptAction action : actions) {
if (action.getType() != ScriptActionType.TAKE) {
action.execute(player);
}
}
});
}
public static @NotNull ScriptActionGroup read(@NotNull List<String> actionsString) {
@@ -1,6 +1,9 @@
package cc.carm.plugin.scriptitems.item;
import cc.carm.lib.easyplugin.utils.MessageUtils;
import cc.carm.plugin.scriptitems.utils.ScriptExecutor;
import com.cryptomorin.xseries.XSound;
import com.cryptomorin.xseries.base.XModule;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
@@ -9,9 +12,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
public enum ScriptActionType {
@@ -20,37 +20,24 @@ public enum ScriptActionType {
* 若内容以 “/" 开头,则会以玩家身份执行命令。
*/
CHAT((player, string) -> {
if (string == null) return true; //没有需要执行的
List<String> finalContents = MessageUtils.setPlaceholders(player, Collections.singletonList(string));
boolean success = true;
for (String finalContent : finalContents) {
try {
player.chat(finalContent);
} catch (Exception ex) {
success = false;
}
}
return success;
if (string == null) return; //没有需要执行的
String contents = MessageUtils.setPlaceholders(player, string);
if (contents == null || contents.isEmpty()) return;
player.chat(contents);
}),
/**
* 让玩家以OP的身份执行命令
*/
OP((player, string) -> {
if (string == null) return true;
List<String> finalCommands = MessageUtils.setPlaceholders(player, Collections.singletonList(string));
boolean success = true;
if (string == null) return;
String cmd = MessageUtils.setPlaceholders(player, string);
if (cmd == null || cmd.isEmpty()) return;
boolean opBefore = player.isOp();
player.setOp(true);
for (String finalCommand : finalCommands) {
try {
player.chat(finalCommand.startsWith("/") ? finalCommand : "/" + finalCommand);
} catch (Exception ex) {
success = false;
}
}
player.chat(cmd.startsWith("/") ? cmd : "/" + cmd);
player.setOp(opBefore);
return success;
}),
/**
@@ -58,27 +45,16 @@ public enum ScriptActionType {
* 指令内容不需要以“/”开头。
*/
CONSOLE((player, string) -> {
if (string == null) return true;
List<String> finalCommands = MessageUtils.setPlaceholders(player, Collections.singletonList(string));
boolean success = true;
for (String finalCommand : finalCommands) {
try {
String cmd = finalCommand.startsWith("/") ? finalCommand.substring(1) : finalCommand;
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd);
} catch (Exception ex) {
success = false;
}
}
return success;
if (string == null) return;
String cmd = MessageUtils.setPlaceholders(player, string);
if (cmd == null || cmd.isEmpty()) return;
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.startsWith("/") ? cmd.substring(1) : cmd);
}),
/**
* 向玩家发送消息。
*/
MESSAGE((sender, messages) -> {
MessageUtils.send(sender, messages);
return true;
}),
MESSAGE(MessageUtils::send),
/**
* 向玩家发送声音。
@@ -90,21 +66,15 @@ public enum ScriptActionType {
* </ul>
*/
SOUND((player, string) -> {
if (string == null) return true;
try {
String[] args = string.contains(":") ? string.split(":") : new String[]{string};
Sound sound = Arrays.stream(Sound.values())
.filter(s -> s.name().equals(args[0]))
.findFirst().orElse(null);
if (string == null) return;
String[] args = string.contains(":") ? string.split(":") : new String[]{string};
Sound sound = XSound.of(args[0].toUpperCase()).map(XModule::get).orElse(null);
if (sound == null) return true;
float volume = args.length > 1 ? Float.parseFloat(args[1]) : 1F;
float pitch = args.length > 2 ? Float.parseFloat(args[2]) : 1F;
if (sound == null) throw new IllegalArgumentException("不存在该声音类型: " + args[0]);
float volume = args.length > 1 ? Float.parseFloat(args[1]) : 1F;
float pitch = args.length > 2 ? Float.parseFloat(args[2]) : 1F;
player.playSound(player.getLocation(), sound, volume, pitch);
} catch (Exception ignored) {
}
return true; // 声音放不放无关紧要
player.playSound(player.getLocation(), sound, volume, pitch);
}),
/**
@@ -114,23 +84,23 @@ public enum ScriptActionType {
if (player.getInventory().getItemInMainHand().getType() != Material.AIR) {
int current = player.getInventory().getItemInMainHand().getAmount();
player.getInventory().getItemInMainHand().setAmount(current - 1);
return true;
} else {
throw new IllegalStateException("玩家手上没有物品可供拿取");
}
return false;
});
final BiFunction<@NotNull Player, @Nullable String, @NotNull Boolean> executor;
final @NotNull ScriptExecutor executor;
ScriptActionType(BiFunction<@NotNull Player, @Nullable String, @NotNull Boolean> executor) {
ScriptActionType(@NotNull ScriptExecutor executor) {
this.executor = executor;
}
public BiFunction<Player, String, Boolean> getExecutor() {
public @NotNull ScriptExecutor getExecutor() {
return executor;
}
public boolean execute(@NotNull Player player, @Nullable String content) {
return getExecutor().apply(player, content);
public void execute(@NotNull Player player, @Nullable String content) throws Exception {
getExecutor().execute(player, content);
}
public static ScriptActionType read(String string) {
@@ -0,0 +1,12 @@
package cc.carm.plugin.scriptitems.utils;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@FunctionalInterface
public interface ScriptExecutor {
void execute(@NotNull Player player, @Nullable String content) throws Exception;
}