下面列出了怎么用net.minecraft.inventory.container.INamedContainerProvider的API类实例代码及写法,或者点击链接到github查看源代码。
public static void openContainer(ServerPlayerEntity player, INamedContainerProvider containerProvider, Consumer<MCDataOutput> packetConsumer) {
if (player.world.isRemote()) {
return;
}
player.closeContainer();
player.getNextWindowId();
int containerId = player.currentWindowId;
Container container = containerProvider.createMenu(containerId, player.inventory, player);
ContainerType<?> type = container.getType();
PacketCustom packet = new PacketCustom(CCLNetwork.NET_CHANNEL, C_OPEN_CONTAINER);
packet.writeRegistryIdUnsafe(ForgeRegistries.CONTAINERS, type);
packet.writeVarInt(containerId);
packet.writeTextComponent(containerProvider.getDisplayName());
packetConsumer.accept(packet);
packet.sendToPlayer(player);
player.openContainer = container;
player.openContainer.addListener(player);
MinecraftForge.EVENT_BUS.post(new PlayerContainerEvent.Open(player, container));
}
@Override
public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult result) {
if (!world.isRemote) {
TileEntity tileEntity = world.getTileEntity(pos);
if (tileEntity instanceof INamedContainerProvider) {
NetworkHooks.openGui((ServerPlayerEntity) player, (INamedContainerProvider) tileEntity, tileEntity.getPos());
} else {
throw new IllegalStateException("Our named container provider is missing!");
}
return ActionResultType.SUCCESS;
}
return ActionResultType.SUCCESS;
}
@Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult blockRayTraceResult)
{
if (worldIn.isRemote)
return ActionResultType.SUCCESS;
TileEntity tileEntity = worldIn.getTileEntity(pos);
if (!(tileEntity instanceof INamedContainerProvider))
return ActionResultType.FAIL;
NetworkHooks.openGui((ServerPlayerEntity) player, (INamedContainerProvider) tileEntity);
return ActionResultType.SUCCESS;
}
@Deprecated
@Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult blockRayTraceResult)
{
if (worldIn.isRemote)
return ActionResultType.SUCCESS;
TileEntity tileEntity = worldIn.getTileEntity(pos);
if (!(tileEntity instanceof INamedContainerProvider))
return ActionResultType.FAIL;
NetworkHooks.openGui((ServerPlayerEntity) player, (INamedContainerProvider) tileEntity);
return ActionResultType.SUCCESS;
}
public static void openContainer(ServerPlayerEntity player, INamedContainerProvider containerProvider) {
openContainer(player, containerProvider, e -> {
});
}