9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-28 19:39:11 +00:00

Merge branch 'Xiao-MoMi:dev' into dev

This commit is contained in:
jhqwqmc
2025-03-26 09:51:03 +08:00
committed by GitHub
11 changed files with 176 additions and 53 deletions

View File

@@ -7,6 +7,8 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import dev.dejvokep.boostedyaml.YamlDocument;
import net.momirealms.craftengine.bukkit.block.worldedit.WorldEditCommandHelper;
import net.momirealms.craftengine.bukkit.block.worldedit.WorldEditBlockRegister;
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
import net.momirealms.craftengine.bukkit.plugin.injector.BukkitInjector;
import net.momirealms.craftengine.bukkit.plugin.network.PacketConsumers;
@@ -84,10 +86,14 @@ public class BukkitBlockManager extends AbstractBlockManager {
private final Map<Key, JsonElement> modBlockStates = new HashMap<>();
// Cached command suggestions
private final List<Suggestion> cachedSuggestions = new ArrayList<>();
// Cached Namespace
private final Set<String> namespacesInUse = new HashSet<>();
// Event listeners
private final BlockEventListener blockEventListener;
private final FallingBlockRemoveListener fallingBlockRemoveListener;
private WorldEditCommandHelper weCommandHelper;
public BukkitBlockManager(BukkitCraftEngine plugin) {
super(plugin);
this.plugin = plugin;
@@ -125,6 +131,19 @@ public class BukkitBlockManager extends AbstractBlockManager {
if (this.fallingBlockRemoveListener != null) {
Bukkit.getPluginManager().registerEvents(this.fallingBlockRemoveListener, plugin.bootstrap());
}
boolean hasWE = false;
// WorldEdit
if (this.plugin.isPluginEnabled("FastAsyncWorldEdit")) {
this.initFastAsyncWorldEditHook();
hasWE = true;
} else if (this.plugin.isPluginEnabled("WorldEdit")) {
this.initWorldEditHook();
hasWE = true;
}
if (hasWE) {
this.weCommandHelper = new WorldEditCommandHelper(this.plugin, this);
this.weCommandHelper.enable();
}
}
@Override
@@ -145,6 +164,7 @@ public class BukkitBlockManager extends AbstractBlockManager {
this.unload();
HandlerList.unregisterAll(this.blockEventListener);
if (this.fallingBlockRemoveListener != null) HandlerList.unregisterAll(this.fallingBlockRemoveListener);
if (this.weCommandHelper != null) this.weCommandHelper.disable();
}
@Override
@@ -172,7 +192,7 @@ public class BukkitBlockManager extends AbstractBlockManager {
public void initWorldEditHook() {
try {
for (Key newBlockId : this.blockRegisterOrder) {
WorldEditHook.register(newBlockId);
WorldEditBlockRegister.register(newBlockId);
}
} catch (Exception e) {
this.plugin.logger().warn("Failed to initialize world edit hook", e);
@@ -225,9 +245,11 @@ public class BukkitBlockManager extends AbstractBlockManager {
@Override
public void initSuggestions() {
this.cachedSuggestions.clear();
this.namespacesInUse.clear();
Set<String> states = new HashSet<>();
for (CustomBlock block : this.id2CraftEngineBlocks.values()) {
states.add(block.id().toString());
this.namespacesInUse.add(block.id().namespace());
for (ImmutableBlockState state : block.variantProvider().states()) {
states.add(state.toString());
}
@@ -237,6 +259,10 @@ public class BukkitBlockManager extends AbstractBlockManager {
}
}
public Set<String> namespacesInUse() {
return Collections.unmodifiableSet(namespacesInUse);
}
public ImmutableMap<Key, List<Integer>> blockAppearanceArranger() {
return blockAppearanceArranger;
}

View File

@@ -1,4 +1,4 @@
package net.momirealms.craftengine.bukkit.block;
package net.momirealms.craftengine.bukkit.block.worldedit;
import com.sk89q.worldedit.bukkit.BukkitBlockRegistry;
import com.sk89q.worldedit.util.concurrency.LazyReference;
@@ -9,7 +9,7 @@ import org.bukkit.Material;
import java.lang.reflect.Field;
public class WorldEditHook {
public class WorldEditBlockRegister {
private static final Field field$BlockType$blockMaterial;
static {

View File

@@ -0,0 +1,84 @@
package net.momirealms.craftengine.bukkit.block.worldedit;
import net.momirealms.craftengine.bukkit.block.BukkitBlockManager;
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
import net.momirealms.craftengine.bukkit.util.BlockStateUtils;
import net.momirealms.craftengine.core.block.BlockStateParser;
import net.momirealms.craftengine.core.block.ImmutableBlockState;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import java.util.*;
// TODO A better command suggestion system
public class WorldEditCommandHelper implements Listener {
private final BukkitBlockManager manager;
private final BukkitCraftEngine plugin;
public WorldEditCommandHelper(BukkitCraftEngine plugin, BukkitBlockManager manager) {
this.plugin = plugin;
this.manager = manager;
}
public void enable() {
Bukkit.getPluginManager().registerEvents(this, plugin.bootstrap());
}
public void disable() {
HandlerList.unregisterAll(this);
}
@EventHandler(priority = EventPriority.HIGH)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
String message = event.getMessage();
if (!message.startsWith("//")) return;
Set<String> cachedNamespaces = manager.namespacesInUse();
String[] args = message.split(" ");
boolean modified = false;
for (int i = 1; i < args.length; i++) {
String[] parts = args[i].split(",");
List<String> processedParts = new ArrayList<>(parts.length);
boolean partModified = false;
for (String part : parts) {
String processed = processIdentifier(part, cachedNamespaces);
partModified |= !part.equals(processed);
processedParts.add(processed);
}
if (partModified) {
args[i] = String.join(",", processedParts);
modified = true;
}
}
if (modified) {
event.setMessage(String.join(" ", args));
}
}
private String processIdentifier(String identifier, Set<String> cachedNamespaces) {
int colonIndex = identifier.indexOf(':');
if (colonIndex == -1) return identifier;
String namespace = identifier.substring(0, colonIndex);
if (!cachedNamespaces.contains(namespace)) return identifier;
ImmutableBlockState state = BlockStateParser.deserialize(identifier);
if (state == null) return identifier;
try {
return BlockStateUtils.getBlockOwnerIdFromState(
state.customBlockState().handle()
).toString();
} catch (NullPointerException e) {
return identifier;
}
}
}

View File

@@ -111,16 +111,6 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
return getCustomItem(id).map(it -> it.settings().fuelTime()).orElse(0);
}
@Override
public void load() {
super.load();
}
@Override
public void unload() {
super.unload();
}
@Override
public void disable() {
this.unload();

View File

@@ -4,6 +4,7 @@ import net.momirealms.antigrieflib.AntiGriefLib;
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.block.worldedit.WorldEditCommandHelper;
import net.momirealms.craftengine.bukkit.entity.furniture.BukkitFurnitureManager;
import net.momirealms.craftengine.bukkit.font.BukkitImageManager;
import net.momirealms.craftengine.bukkit.item.BukkitItemManager;
@@ -178,12 +179,6 @@ public class BukkitCraftEngine extends CraftEngine {
new ImageExpansion(this).register();
this.hasPlaceholderAPI = true;
}
// WorldEdit
if (this.isPluginEnabled("FastAsyncWorldEdit")) {
this.blockManager().initFastAsyncWorldEditHook();
} else if (this.isPluginEnabled("WorldEdit")) {
this.blockManager().initWorldEditHook();
}
}
@Override

View File

@@ -619,6 +619,9 @@ public class PacketConsumers {
if (furniture != null) {
user.furnitureView().computeIfAbsent(furniture.baseEntityId(), k -> new ArrayList<>()).addAll(furniture.subEntityIds());
user.sendPacket(furniture.spawnPacket(), false);
if (ConfigManager.hideBaseEntity()) {
event.setCancelled(true);
}
}
}
} catch (Exception e) {