mirror of
https://github.com/CarmJos/ScriptItems
synced 2026-06-04 19:58:48 +08:00
feat: 升级到 1.21,并提供folia支持
This commit is contained in:
@@ -143,6 +143,23 @@
|
|||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</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>
|
<dependency>
|
||||||
<groupId>me.clip</groupId>
|
<groupId>me.clip</groupId>
|
||||||
<artifactId>placeholderapi</artifactId>
|
<artifactId>placeholderapi</artifactId>
|
||||||
|
|||||||
@@ -12,6 +12,9 @@ import cc.carm.plugin.scriptitems.listener.ProtectListener;
|
|||||||
import cc.carm.plugin.scriptitems.manager.ItemsManager;
|
import cc.carm.plugin.scriptitems.manager.ItemsManager;
|
||||||
import org.bstats.bukkit.Metrics;
|
import org.bstats.bukkit.Metrics;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
public class Main extends EasyPlugin {
|
public class Main extends EasyPlugin {
|
||||||
|
|
||||||
@@ -108,4 +111,20 @@ public class Main extends EasyPlugin {
|
|||||||
return false;
|
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;
|
package cc.carm.plugin.scriptitems.item;
|
||||||
|
|
||||||
|
import cc.carm.plugin.scriptitems.Main;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
@@ -22,8 +23,13 @@ public class ScriptAction {
|
|||||||
return actionContent;
|
return actionContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean execute(Player player) {
|
public void execute(Player player) {
|
||||||
return getType().execute(player, getActionContent());
|
try {
|
||||||
|
getType().execute(player, getActionContent());
|
||||||
|
} catch (Exception ex) {
|
||||||
|
Main.severe("在为玩家执行 脚本动作 时发生错误: " + ex.getMessage());
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static @Nullable ScriptAction read(@Nullable String actionString) {
|
public static @Nullable ScriptAction read(@Nullable String actionString) {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package cc.carm.plugin.scriptitems.item;
|
package cc.carm.plugin.scriptitems.item;
|
||||||
|
|
||||||
|
import cc.carm.plugin.scriptitems.Main;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@@ -17,7 +18,20 @@ public class ScriptActionGroup {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void execute(Player player) {
|
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) {
|
public static @NotNull ScriptActionGroup read(@NotNull List<String> actionsString) {
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package cc.carm.plugin.scriptitems.item;
|
package cc.carm.plugin.scriptitems.item;
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.utils.MessageUtils;
|
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.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.Sound;
|
import org.bukkit.Sound;
|
||||||
@@ -9,9 +12,6 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.function.BiFunction;
|
|
||||||
|
|
||||||
public enum ScriptActionType {
|
public enum ScriptActionType {
|
||||||
|
|
||||||
@@ -20,37 +20,24 @@ public enum ScriptActionType {
|
|||||||
* 若内容以 “/" 开头,则会以玩家身份执行命令。
|
* 若内容以 “/" 开头,则会以玩家身份执行命令。
|
||||||
*/
|
*/
|
||||||
CHAT((player, string) -> {
|
CHAT((player, string) -> {
|
||||||
if (string == null) return true; //没有需要执行的
|
if (string == null) return; //没有需要执行的
|
||||||
List<String> finalContents = MessageUtils.setPlaceholders(player, Collections.singletonList(string));
|
String contents = MessageUtils.setPlaceholders(player, string);
|
||||||
boolean success = true;
|
if (contents == null || contents.isEmpty()) return;
|
||||||
for (String finalContent : finalContents) {
|
player.chat(contents);
|
||||||
try {
|
|
||||||
player.chat(finalContent);
|
|
||||||
} catch (Exception ex) {
|
|
||||||
success = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return success;
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 让玩家以OP的身份执行命令
|
* 让玩家以OP的身份执行命令
|
||||||
*/
|
*/
|
||||||
OP((player, string) -> {
|
OP((player, string) -> {
|
||||||
if (string == null) return true;
|
if (string == null) return;
|
||||||
List<String> finalCommands = MessageUtils.setPlaceholders(player, Collections.singletonList(string));
|
String cmd = MessageUtils.setPlaceholders(player, string);
|
||||||
boolean success = true;
|
if (cmd == null || cmd.isEmpty()) return;
|
||||||
|
|
||||||
boolean opBefore = player.isOp();
|
boolean opBefore = player.isOp();
|
||||||
player.setOp(true);
|
player.setOp(true);
|
||||||
for (String finalCommand : finalCommands) {
|
player.chat(cmd.startsWith("/") ? cmd : "/" + cmd);
|
||||||
try {
|
|
||||||
player.chat(finalCommand.startsWith("/") ? finalCommand : "/" + finalCommand);
|
|
||||||
} catch (Exception ex) {
|
|
||||||
success = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
player.setOp(opBefore);
|
player.setOp(opBefore);
|
||||||
return success;
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -58,27 +45,16 @@ public enum ScriptActionType {
|
|||||||
* 指令内容不需要以“/”开头。
|
* 指令内容不需要以“/”开头。
|
||||||
*/
|
*/
|
||||||
CONSOLE((player, string) -> {
|
CONSOLE((player, string) -> {
|
||||||
if (string == null) return true;
|
if (string == null) return;
|
||||||
List<String> finalCommands = MessageUtils.setPlaceholders(player, Collections.singletonList(string));
|
String cmd = MessageUtils.setPlaceholders(player, string);
|
||||||
boolean success = true;
|
if (cmd == null || cmd.isEmpty()) return;
|
||||||
for (String finalCommand : finalCommands) {
|
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.startsWith("/") ? cmd.substring(1) : cmd);
|
||||||
try {
|
|
||||||
String cmd = finalCommand.startsWith("/") ? finalCommand.substring(1) : finalCommand;
|
|
||||||
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd);
|
|
||||||
} catch (Exception ex) {
|
|
||||||
success = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return success;
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 向玩家发送消息。
|
* 向玩家发送消息。
|
||||||
*/
|
*/
|
||||||
MESSAGE((sender, messages) -> {
|
MESSAGE(MessageUtils::send),
|
||||||
MessageUtils.send(sender, messages);
|
|
||||||
return true;
|
|
||||||
}),
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 向玩家发送声音。
|
* 向玩家发送声音。
|
||||||
@@ -90,21 +66,15 @@ public enum ScriptActionType {
|
|||||||
* </ul>
|
* </ul>
|
||||||
*/
|
*/
|
||||||
SOUND((player, string) -> {
|
SOUND((player, string) -> {
|
||||||
if (string == null) return true;
|
if (string == null) return;
|
||||||
try {
|
String[] args = string.contains(":") ? string.split(":") : new String[]{string};
|
||||||
String[] args = string.contains(":") ? string.split(":") : new String[]{string};
|
Sound sound = XSound.of(args[0].toUpperCase()).map(XModule::get).orElse(null);
|
||||||
Sound sound = Arrays.stream(Sound.values())
|
|
||||||
.filter(s -> s.name().equals(args[0]))
|
|
||||||
.findFirst().orElse(null);
|
|
||||||
|
|
||||||
if (sound == null) return true;
|
if (sound == null) throw new IllegalArgumentException("不存在该声音类型: " + args[0]);
|
||||||
float volume = args.length > 1 ? Float.parseFloat(args[1]) : 1F;
|
float volume = args.length > 1 ? Float.parseFloat(args[1]) : 1F;
|
||||||
float pitch = args.length > 2 ? Float.parseFloat(args[2]) : 1F;
|
float pitch = args.length > 2 ? Float.parseFloat(args[2]) : 1F;
|
||||||
|
|
||||||
player.playSound(player.getLocation(), sound, volume, pitch);
|
player.playSound(player.getLocation(), sound, volume, pitch);
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
return true; // 声音放不放无关紧要
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -114,23 +84,23 @@ public enum ScriptActionType {
|
|||||||
if (player.getInventory().getItemInMainHand().getType() != Material.AIR) {
|
if (player.getInventory().getItemInMainHand().getType() != Material.AIR) {
|
||||||
int current = player.getInventory().getItemInMainHand().getAmount();
|
int current = player.getInventory().getItemInMainHand().getAmount();
|
||||||
player.getInventory().getItemInMainHand().setAmount(current - 1);
|
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;
|
this.executor = executor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BiFunction<Player, String, Boolean> getExecutor() {
|
public @NotNull ScriptExecutor getExecutor() {
|
||||||
return executor;
|
return executor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean execute(@NotNull Player player, @Nullable String content) {
|
public void execute(@NotNull Player player, @Nullable String content) throws Exception {
|
||||||
return getExecutor().apply(player, content);
|
getExecutor().execute(player, content);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ScriptActionType read(String string) {
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user