|
|
@@ -4,21 +4,13 @@ import com.husj.block.entity.DisassemblerBlockEntity;
|
|
|
import com.mojang.serialization.MapCodec;
|
|
|
import net.minecraft.block.*;
|
|
|
import net.minecraft.block.entity.BlockEntity;
|
|
|
-import net.minecraft.block.entity.BlockEntityTicker;
|
|
|
-import net.minecraft.block.entity.BlockEntityType;
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
-import net.minecraft.screen.NamedScreenHandlerFactory;
|
|
|
-import net.minecraft.screen.ScreenHandlerContext;
|
|
|
-import net.minecraft.screen.SimpleNamedScreenHandlerFactory;
|
|
|
-import net.minecraft.text.Text;
|
|
|
import net.minecraft.util.ActionResult;
|
|
|
import net.minecraft.util.hit.BlockHitResult;
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
import net.minecraft.world.World;
|
|
|
-import org.jetbrains.annotations.Nullable;
|
|
|
|
|
|
public class DisassemblerBlock extends BlockWithEntity {
|
|
|
- private static final Text TITLE = Text.translatable("container.husj.disassembler");
|
|
|
|
|
|
public DisassemblerBlock(Settings settings) {
|
|
|
super(settings);
|
|
|
@@ -42,37 +34,40 @@ public class DisassemblerBlock extends BlockWithEntity {
|
|
|
@Override
|
|
|
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
|
|
|
if (!world.isClient()) {
|
|
|
- NamedScreenHandlerFactory screenHandlerFactory = state.createScreenHandlerFactory(world, pos);
|
|
|
- if (screenHandlerFactory != null) {
|
|
|
- player.openHandledScreen(screenHandlerFactory);
|
|
|
- }
|
|
|
- }
|
|
|
- return ActionResult.SUCCESS;
|
|
|
- }
|
|
|
+ // 获取玩家主手的物品
|
|
|
+ net.minecraft.item.ItemStack heldItem = player.getMainHandStack();
|
|
|
|
|
|
- @Override
|
|
|
- @Nullable
|
|
|
- public NamedScreenHandlerFactory createScreenHandlerFactory(BlockState state, World world, BlockPos pos) {
|
|
|
- BlockEntity blockEntity = world.getBlockEntity(pos);
|
|
|
- if (blockEntity instanceof DisassemblerBlockEntity) {
|
|
|
- return new SimpleNamedScreenHandlerFactory((syncId, inventory, player) ->
|
|
|
- new com.husj.screen.DisassemblerScreenHandler(syncId, inventory, ScreenHandlerContext.create(world, pos)), TITLE);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
+ // 检查是否可以拆解
|
|
|
+ if (!heldItem.isEmpty() && com.husj.recipe.DisassemblerRecipes.hasRecipe(heldItem)) {
|
|
|
+ // 获取拆解结果
|
|
|
+ net.minecraft.item.ItemStack result = com.husj.recipe.DisassemblerRecipes.getRecipeResult(heldItem);
|
|
|
|
|
|
- @Nullable
|
|
|
- @Override
|
|
|
- public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) {
|
|
|
- if (world.isClient()) {
|
|
|
- return null;
|
|
|
+ if (!result.isEmpty()) {
|
|
|
+ // 在方块上方生成掉落物
|
|
|
+ net.minecraft.entity.ItemEntity itemEntity = new net.minecraft.entity.ItemEntity(
|
|
|
+ world,
|
|
|
+ pos.getX() + 0.5,
|
|
|
+ pos.getY() + 1.0,
|
|
|
+ pos.getZ() + 0.5,
|
|
|
+ result.copy());
|
|
|
+ itemEntity.setToDefaultPickupDelay();
|
|
|
+ world.spawnEntity(itemEntity);
|
|
|
+
|
|
|
+ // 消耗玩家手中的物品
|
|
|
+ if (!player.isCreative()) {
|
|
|
+ heldItem.decrement(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 播放音效
|
|
|
+ world.playSound(null, pos, net.minecraft.sound.SoundEvents.BLOCK_ANVIL_USE,
|
|
|
+ net.minecraft.sound.SoundCategory.BLOCKS, 1.0F, 1.0F);
|
|
|
+
|
|
|
+ return ActionResult.SUCCESS;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- return validateTicker(type, com.husj.ModBlocks.DISASSEMBLER_BLOCK_ENTITY, DisassemblerBlockEntity::tick);
|
|
|
- }
|
|
|
|
|
|
- @Nullable
|
|
|
- protected static <E extends BlockEntity, A extends BlockEntity> BlockEntityTicker<A> validateTicker(
|
|
|
- BlockEntityType<A> givenType, BlockEntityType<E> expectedType, BlockEntityTicker<? super E> ticker) {
|
|
|
- return expectedType == givenType ? (BlockEntityTicker<A>) ticker : null;
|
|
|
+ return ActionResult.CONSUME;
|
|
|
}
|
|
|
+
|
|
|
}
|