Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复伐木附魔导致游戏进入死循环 #783

Merged
merged 2 commits into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.dubhe.anvilcraft.AnvilCraft;
import dev.dubhe.anvilcraft.init.ModEnchantments;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
Expand All @@ -12,14 +13,10 @@
import net.minecraft.world.item.enchantment.EnchantmentCategory;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class FellingEnchantment extends ModEnchantment {
Expand Down Expand Up @@ -66,21 +63,30 @@ public static void felling(@NotNull Player player, Level level, BlockPos pos, It
Map<Enchantment, Integer> enchantments = EnchantmentHelper.getEnchantments(tool);
if (!enchantments.containsKey(ModEnchantments.FELLING.get())) return;
int max = (enchantments.get(ModEnchantments.FELLING.get()) * AnvilCraft.config.fellingBlockPerLevel) - 1;
while (max > 0) {
for (BlockPos offset : OFFSET) {
BlockPos offsetPos = pos.offset(offset);
BlockState state = level.getBlockState(offsetPos);
if (!state.is(BlockTags.LOGS)) continue;
if (max-- < 0) break;
Block block = state.getBlock();
boolean bl = level.removeBlock(offsetPos, false);
if (bl) {
block.destroy(level, offsetPos, state);
}
if (player.isCreative()) continue;
BlockEntity entity = level.getBlockEntity(offsetPos);
block.playerDestroy(level, player, offsetPos, state, entity, tool);
FellingEnchantment.chainMine(level, player, pos, max, tool);
}

/**
* 连锁破坏原木
*
* @param level 世界
* @param sourceBlock 源方块坐标
* @param max 最大采集数量
*/
private static void chainMine(Level level, Player player, BlockPos sourceBlock, int max, ItemStack tool) {
BlockPos.breadthFirstTraversal(sourceBlock, Integer.MAX_VALUE, max, (blockPos, blockPosConsumer) -> {
for (Direction direction : Direction.values()) {
blockPosConsumer.accept(blockPos.relative(direction));
}
}, blockPos -> {
BlockState blockState = level.getBlockState(blockPos);
if (blockState.is(BlockTags.LOGS)) {
BlockEntity blockEntity = level.getBlockEntity(blockPos);
level.removeBlock(blockPos, false);
blockState.getBlock().playerDestroy(level, player, blockPos, blockState, blockEntity, tool);
return true;
}
}
return sourceBlock.equals(blockPos);
});
}
}
Loading