latest
This commit is contained in:
@@ -6,8 +6,10 @@ import net.minecraft.sounds.SoundSource;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.neoforged.bus.api.SubscribeEvent;
|
||||
import net.neoforged.neoforge.common.NeoForge;
|
||||
import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
|
||||
@@ -28,6 +30,37 @@ public final class ThEWrenchHandler {
|
||||
NeoForge.EVENT_BUS.register(ThEWrenchHandler.class);
|
||||
}
|
||||
|
||||
/** 判定目标是否为本模组可拆卸方块(按命名空间判断,兼容未继承 ThETileBase 的工作台等)。 */
|
||||
public static boolean isDisassemblyTarget(Level level, BlockPos pos) {
|
||||
BlockState state = level.getBlockState(pos);
|
||||
return ThaumicEnergistics.MODID
|
||||
.equals(state.getBlock().builtInRegistryHolder().key().location().getNamespace());
|
||||
}
|
||||
|
||||
/**
|
||||
* ThE 方块扳手拆卸公共入口(普通 AE2 扳手事件与法杖 AE 扳手核心共用):
|
||||
* 服务端掉落方块物品并移除方块,客户端播放拆卸音效。
|
||||
* 返回 true 表示目标为 ThE 方块且拆卸已执行。
|
||||
*/
|
||||
public static boolean disassembleThEBlock(Player player, Level level, BlockPos pos) {
|
||||
if (!isDisassemblyTarget(level, pos)) {
|
||||
return false;
|
||||
}
|
||||
BlockEntity be = level.getBlockEntity(pos);
|
||||
if (be == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!level.isClientSide) {
|
||||
// ThE 方块掉落即自身(挖掘走 getDrops 覆写;扳手拆卸直接给方块物品,避免 loot table 为空)
|
||||
player.getInventory().placeItemBackInInventory(new ItemStack(be.getBlockState().getBlock()));
|
||||
level.removeBlock(pos, false);
|
||||
} else {
|
||||
level.playSound(player, pos, SoundEvents.ITEM_FRAME_REMOVE_ITEM, SoundSource.BLOCKS, 0.7F, 1.0F);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onRightClickBlock(PlayerInteractEvent.RightClickBlock event) {
|
||||
if (event.isCanceled() || event.getHand() != InteractionHand.MAIN_HAND) {
|
||||
@@ -44,22 +77,9 @@ public final class ThEWrenchHandler {
|
||||
|
||||
var level = event.getLevel();
|
||||
BlockPos pos = event.getPos();
|
||||
BlockState state = level.getBlockState(pos);
|
||||
// 只处理 ThE 方块(按命名空间判断,兼容未继承 ThETileBase 的工作台等)
|
||||
if (!ThaumicEnergistics.MODID.equals(state.getBlock().builtInRegistryHolder().key().location().getNamespace())) {
|
||||
return;
|
||||
}
|
||||
BlockEntity be = level.getBlockEntity(pos);
|
||||
if (be == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!level.isClientSide) {
|
||||
// ThE 方块掉落即自身(挖掘走 getDrops 覆写;扳手拆卸直接给方块物品,避免 loot table 为空)
|
||||
player.getInventory().placeItemBackInInventory(new ItemStack(be.getBlockState().getBlock()));
|
||||
level.removeBlock(pos, false);
|
||||
} else {
|
||||
level.playSound(player, pos, SoundEvents.ITEM_FRAME_REMOVE_ITEM, SoundSource.BLOCKS, 0.7F, 1.0F);
|
||||
if (!disassembleThEBlock(player, level, pos)) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.setCanceled(true);
|
||||
|
||||
Reference in New Issue
Block a user