如何自定义一个工作台

FledgeXu


版本信息
你使用的IDE:IntelliJ IDEA
你使用的IDE版本: 2019.3
Forge版本: 31.1.0
Minecraft版本: 1.15.2

错误情况简述
如何自定义一个工作台,直接继承原版工作台的方块,Gui 打开以后闪一下就消失。

相关代码
SpiritCraftingTableBlock.java

package com.otakusaikou.selfcultivationcraft.blocks;

import com.otakusaikou.selfcultivationcraft.blocks.helpers.BlockInitializationHelpers;
import com.otakusaikou.selfcultivationcraft.gui.SpiritCraftingTableContainer;
import com.otakusaikou.selfcultivationcraft.utils.Util;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.CraftingTableBlock;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.inventory.container.SimpleNamedContainerProvider;
import net.minecraft.inventory.container.WorkbenchContainer;
import net.minecraft.stats.Stats;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.IWorldPosCallable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;

public class SpiritCraftingTableBlock extends Block {
    private static final ITextComponent titleName = new TranslationTextComponent(Util.MOD_ID+".container.crafting");

    public SpiritCraftingTableBlock(String registryName) {
        super(Properties.create(Material.WOOD));
        BlockInitializationHelpers.blockInitializationHelper(this,registryName);

    }
    @Override
    public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
        if (!worldIn.isRemote) {
            player.openContainer(state.getContainer(worldIn, pos));
            //player.addStat(Stats.INTERACT_WITH_CRAFTING_TABLE);
        }
        return ActionResultType.SUCCESS;
    }
    @Override
    public INamedContainerProvider getContainer(BlockState state, World worldIn, BlockPos pos) {
        return new SimpleNamedContainerProvider((p_220270_2_, p_220270_3_, p_220270_4_) -> {
            return new WorkbenchContainer(p_220270_2_, p_220270_3_, IWorldPosCallable.of(worldIn, pos));
        }, titleName);
    }
}



FledgeXu


你的自己自定义一个Container才能实现这个功能。
打开会闪一下然后关闭的原因如下:

public boolean canInteractWith(PlayerEntity playerIn) {
      return isWithinUsableDistance(this.field_217070_e, playerIn, Blocks.CRAFTING_TABLE);
   }

WorkbenchContainer会检测与工作台的距离。


FledgeXu


该主题在最后一个回复创建后7天后自动关闭。不再允许新的回复。