9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-30 20:39:10 +00:00

添加旧版本slimeworld支持

This commit is contained in:
XiaoMoMi
2025-04-25 03:25:47 +08:00
parent 338639c015
commit bee4ed2767
13 changed files with 317 additions and 23 deletions

View File

@@ -1,5 +1,6 @@
package net.momirealms.craftengine.bukkit.api.event;
import net.momirealms.craftengine.bukkit.plugin.user.BukkitServerPlayer;
import net.momirealms.craftengine.core.block.CustomBlock;
import net.momirealms.craftengine.core.block.ImmutableBlockState;
import org.bukkit.Location;
@@ -18,17 +19,23 @@ public class CustomBlockBreakEvent extends PlayerEvent implements Cancellable {
private final Location location;
private final Block bukkitBlock;
private boolean dropItems;
private final BukkitServerPlayer player;
public CustomBlockBreakEvent(@NotNull Player player,
public CustomBlockBreakEvent(@NotNull BukkitServerPlayer player,
@NotNull Location location,
@NotNull Block bukkitBlock,
@NotNull ImmutableBlockState state) {
super(player);
super(player.platformPlayer());
this.customBlock = state.owner().value();
this.state = state;
this.bukkitBlock = bukkitBlock;
this.location = location;
this.dropItems = true;
this.player = player;
}
public BukkitServerPlayer player() {
return player;
}
public boolean dropItems() {
@@ -44,11 +51,6 @@ public class CustomBlockBreakEvent extends PlayerEvent implements Cancellable {
return bukkitBlock;
}
@NotNull
public Player player() {
return getPlayer();
}
@NotNull
public CustomBlock customBlock() {
return this.customBlock;

View File

@@ -120,7 +120,7 @@ public class BlockEventListener implements Listener {
}
// trigger event
CustomBlockBreakEvent customBreakEvent = new CustomBlockBreakEvent(event.getPlayer(), location, block, state);
CustomBlockBreakEvent customBreakEvent = new CustomBlockBreakEvent(serverPlayer, location, block, state);
boolean isCancelled = EventUtils.fireAndCheckCancel(customBreakEvent);
if (isCancelled) {
event.setCancelled(true);
@@ -146,14 +146,10 @@ public class BlockEventListener implements Listener {
Item<ItemStack> itemInHand = serverPlayer.getItemInHand(InteractionHand.MAIN_HAND);
// do not drop if it's not the correct tool
BlockSettings settings = state.settings();
if (settings.requireCorrectTool()) {
if (itemInHand == null) return;
if (!settings.isCorrectTool(itemInHand.id()) &&
(!settings.respectToolComponent() || !FastNMS.INSTANCE.method$ItemStack$isCorrectToolForDrops(itemInHand.getLiteralObject(), state.customBlockState().handle()))) {
return;
}
if (!BlockStateUtils.isCorrectTool(state, itemInHand)) {
return;
}
// drop items
ContextHolder.Builder builder = ContextHolder.builder();
builder.withParameter(LootParameters.WORLD, world);

View File

@@ -0,0 +1,71 @@
package net.momirealms.craftengine.bukkit.compatibility.skript;
import ch.njol.skript.Skript;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.StringUtils;
import net.momirealms.craftengine.bukkit.api.event.CustomBlockBreakEvent;
import net.momirealms.craftengine.bukkit.api.event.CustomBlockPlaceEvent;
import net.momirealms.craftengine.bukkit.util.BlockStateUtils;
import net.momirealms.craftengine.core.entity.player.InteractionHand;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
@SuppressWarnings({"unchecked"})
public class EvtCustomBlock extends SkriptEvent {
public static void register() {
Skript.registerEvent("Break Custom Block", EvtCustomBlock.class, CustomBlockBreakEvent.class, "[customblock] (break[ing]|1¦min(e|ing)) [[of] %-strings%]")
.description("Called when a custom block is broken by a player. If you use 'on mine', only events where the broken block dropped something will call the trigger.");
Skript.registerEvent("Place Custom Block", EvtCustomBlock.class, CustomBlockBreakEvent.class, "[customblock] (plac(e|ing)|build[ing]) [[of] %-strings%]")
.description("Called when a player places a custom block.");
}
@Nullable
private Literal<String> blocks;
private String[] blockArray;
private boolean mine = false;
@Override
public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseResult parser) {
if (args[0] != null) {
blocks = ((Literal<String>) args[0]);
blockArray = blocks.getAll();
}
mine = parser.mark == 1;
return true;
}
@Override
public boolean check(Event event) {
if (mine && event instanceof CustomBlockBreakEvent customBlockBreakEvent) {
if (!BlockStateUtils.isCorrectTool(customBlockBreakEvent.blockState(), customBlockBreakEvent.player().getItemInHand(InteractionHand.MAIN_HAND))) {
return false;
}
}
if (blocks == null)
return true;
String blockType;
String blockState;
if (event instanceof CustomBlockBreakEvent customBlockBreakEvent) {
blockType = customBlockBreakEvent.customBlock().id().toString();
blockState = customBlockBreakEvent.blockState().toString();
} else if (event instanceof CustomBlockPlaceEvent customBlockPlaceEvent) {
blockType = customBlockPlaceEvent.customBlock().id().toString();
blockState = customBlockPlaceEvent.blockState().toString();
} else {
return false;
}
return Arrays.stream(blockArray).anyMatch(block -> StringUtils.equals(blockType, block, true) || StringUtils.equals(blockState, block, true));
}
@Override
public String toString(@Nullable Event event, boolean debug) {
return "break/place" + (blocks != null ? " of " + blocks.toString(event, debug) : "");
}
}

View File

@@ -6,6 +6,7 @@ import net.momirealms.craftengine.bukkit.api.event.CraftEngineReloadEvent;
import net.momirealms.craftengine.bukkit.block.BukkitBlockManager;
import net.momirealms.craftengine.bukkit.block.behavior.BukkitBlockBehaviors;
import net.momirealms.craftengine.bukkit.compatibility.papi.PlaceholderAPIUtils;
import net.momirealms.craftengine.bukkit.compatibility.skript.EvtCustomBlock;
import net.momirealms.craftengine.bukkit.entity.furniture.BukkitFurnitureManager;
import net.momirealms.craftengine.bukkit.entity.furniture.hitbox.BukkitHitBoxTypes;
import net.momirealms.craftengine.bukkit.font.BukkitFontManager;
@@ -165,6 +166,10 @@ public class BukkitCraftEngine extends CraftEngine {
PlaceholderAPIUtils.registerExpansions(this);
this.hasPlaceholderAPI = true;
}
// skript
if (this.isPluginEnabled("Skript")) {
EvtCustomBlock.register();
}
}
@Override

View File

@@ -2,10 +2,8 @@ package net.momirealms.craftengine.bukkit.util;
import net.momirealms.craftengine.bukkit.block.BukkitBlockManager;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.core.block.BlockStateParser;
import net.momirealms.craftengine.core.block.CustomBlock;
import net.momirealms.craftengine.core.block.ImmutableBlockState;
import net.momirealms.craftengine.core.block.PushReaction;
import net.momirealms.craftengine.core.block.*;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.util.Instrument;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MapColor;
@@ -14,6 +12,9 @@ import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.IdentityHashMap;
import java.util.List;
@@ -32,6 +33,18 @@ public class BlockStateUtils {
hasInit = true;
}
public static boolean isCorrectTool(@NotNull ImmutableBlockState state, @Nullable Item<ItemStack> itemInHand) {
BlockSettings settings = state.settings();
if (settings.requireCorrectTool()) {
if (itemInHand == null) return false;
if (!settings.isCorrectTool(itemInHand.id()) &&
(!settings.respectToolComponent() || !FastNMS.INSTANCE.method$ItemStack$isCorrectToolForDrops(itemInHand.getLiteralObject(), state.customBlockState().handle()))) {
return false;
}
}
return true;
}
public static List<Object> getAllBlockStates(String blockState) {
int index = blockState.indexOf('[');
if (index == -1) {

View File

@@ -1,5 +1,6 @@
package net.momirealms.craftengine.bukkit.world;
import net.momirealms.craftengine.bukkit.compatibility.legacy.slimeworld.LegacySlimeFormatStorageAdaptor;
import net.momirealms.craftengine.bukkit.compatibility.slimeworld.SlimeFormatStorageAdaptor;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
@@ -61,8 +62,23 @@ public class BukkitWorldManager implements WorldManager, Listener {
return;
} catch (ClassNotFoundException ignored) {
}
} else {
try {
Class.forName("com.infernalsuite.aswm.api.SlimePlugin");
LegacySlimeFormatStorageAdaptor adaptor = new LegacySlimeFormatStorageAdaptor(this, 1);
this.storageAdaptor = adaptor;
Bukkit.getPluginManager().registerEvents(adaptor, plugin.bootstrap());
} catch (ClassNotFoundException ignored) {
if (Bukkit.getPluginManager().isPluginEnabled("SlimeWorldPlugin")) {
LegacySlimeFormatStorageAdaptor adaptor = new LegacySlimeFormatStorageAdaptor(this, 2);
this.storageAdaptor = adaptor;
Bukkit.getPluginManager().registerEvents(adaptor, plugin.bootstrap());
}
}
}
if (this.storageAdaptor == null) {
this.storageAdaptor = new DefaultStorageAdaptor();
}
this.storageAdaptor = new DefaultStorageAdaptor();
}
@Override